clang  3.9.0svn
SemaObjCProperty.cpp
Go to the documentation of this file.
00001 //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements semantic analysis for Objective C @property and
00011 //  @synthesize declarations.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Sema/SemaInternal.h"
00016 #include "clang/AST/ASTMutationListener.h"
00017 #include "clang/AST/DeclObjC.h"
00018 #include "clang/AST/ExprCXX.h"
00019 #include "clang/AST/ExprObjC.h"
00020 #include "clang/Basic/SourceManager.h"
00021 #include "clang/Lex/Lexer.h"
00022 #include "clang/Lex/Preprocessor.h"
00023 #include "clang/Sema/Initialization.h"
00024 #include "llvm/ADT/DenseSet.h"
00025 #include "llvm/ADT/SmallString.h"
00026 
00027 using namespace clang;
00028 
00029 //===----------------------------------------------------------------------===//
00030 // Grammar actions.
00031 //===----------------------------------------------------------------------===//
00032 
00033 /// getImpliedARCOwnership - Given a set of property attributes and a
00034 /// type, infer an expected lifetime.  The type's ownership qualification
00035 /// is not considered.
00036 ///
00037 /// Returns OCL_None if the attributes as stated do not imply an ownership.
00038 /// Never returns OCL_Autoreleasing.
00039 static Qualifiers::ObjCLifetime getImpliedARCOwnership(
00040                                ObjCPropertyDecl::PropertyAttributeKind attrs,
00041                                                 QualType type) {
00042   // retain, strong, copy, weak, and unsafe_unretained are only legal
00043   // on properties of retainable pointer type.
00044   if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
00045                ObjCPropertyDecl::OBJC_PR_strong |
00046                ObjCPropertyDecl::OBJC_PR_copy)) {
00047     return Qualifiers::OCL_Strong;
00048   } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
00049     return Qualifiers::OCL_Weak;
00050   } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
00051     return Qualifiers::OCL_ExplicitNone;
00052   }
00053 
00054   // assign can appear on other types, so we have to check the
00055   // property type.
00056   if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
00057       type->isObjCRetainableType()) {
00058     return Qualifiers::OCL_ExplicitNone;
00059   }
00060 
00061   return Qualifiers::OCL_None;
00062 }
00063 
00064 /// Check the internal consistency of a property declaration with
00065 /// an explicit ownership qualifier.
00066 static void checkPropertyDeclWithOwnership(Sema &S,
00067                                            ObjCPropertyDecl *property) {
00068   if (property->isInvalidDecl()) return;
00069 
00070   ObjCPropertyDecl::PropertyAttributeKind propertyKind
00071     = property->getPropertyAttributes();
00072   Qualifiers::ObjCLifetime propertyLifetime
00073     = property->getType().getObjCLifetime();
00074 
00075   assert(propertyLifetime != Qualifiers::OCL_None);
00076 
00077   Qualifiers::ObjCLifetime expectedLifetime
00078     = getImpliedARCOwnership(propertyKind, property->getType());
00079   if (!expectedLifetime) {
00080     // We have a lifetime qualifier but no dominating property
00081     // attribute.  That's okay, but restore reasonable invariants by
00082     // setting the property attribute according to the lifetime
00083     // qualifier.
00084     ObjCPropertyDecl::PropertyAttributeKind attr;
00085     if (propertyLifetime == Qualifiers::OCL_Strong) {
00086       attr = ObjCPropertyDecl::OBJC_PR_strong;
00087     } else if (propertyLifetime == Qualifiers::OCL_Weak) {
00088       attr = ObjCPropertyDecl::OBJC_PR_weak;
00089     } else {
00090       assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
00091       attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
00092     }
00093     property->setPropertyAttributes(attr);
00094     return;
00095   }
00096 
00097   if (propertyLifetime == expectedLifetime) return;
00098 
00099   property->setInvalidDecl();
00100   S.Diag(property->getLocation(),
00101          diag::err_arc_inconsistent_property_ownership)
00102     << property->getDeclName()
00103     << expectedLifetime
00104     << propertyLifetime;
00105 }
00106 
00107 /// \brief Check this Objective-C property against a property declared in the
00108 /// given protocol.
00109 static void
00110 CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
00111                              ObjCProtocolDecl *Proto,
00112                              llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
00113   // Have we seen this protocol before?
00114   if (!Known.insert(Proto).second)
00115     return;
00116 
00117   // Look for a property with the same name.
00118   DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
00119   for (unsigned I = 0, N = R.size(); I != N; ++I) {
00120     if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
00121       S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
00122       return;
00123     }
00124   }
00125 
00126   // Check this property against any protocols we inherit.
00127   for (auto *P : Proto->protocols())
00128     CheckPropertyAgainstProtocol(S, Prop, P, Known);
00129 }
00130 
00131 static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
00132   // In GC mode, just look for the __weak qualifier.
00133   if (S.getLangOpts().getGC() != LangOptions::NonGC) {
00134     if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
00135 
00136   // In ARC/MRC, look for an explicit ownership qualifier.
00137   // For some reason, this only applies to __weak.
00138   } else if (auto ownership = T.getObjCLifetime()) {
00139     switch (ownership) {
00140     case Qualifiers::OCL_Weak:
00141       return ObjCDeclSpec::DQ_PR_weak;
00142     case Qualifiers::OCL_Strong:
00143       return ObjCDeclSpec::DQ_PR_strong;
00144     case Qualifiers::OCL_ExplicitNone:
00145       return ObjCDeclSpec::DQ_PR_unsafe_unretained;
00146     case Qualifiers::OCL_Autoreleasing:
00147     case Qualifiers::OCL_None:
00148       return 0;
00149     }
00150     llvm_unreachable("bad qualifier");
00151   }
00152 
00153   return 0;
00154 }
00155 
00156 static const unsigned OwnershipMask =
00157   (ObjCPropertyDecl::OBJC_PR_assign |
00158    ObjCPropertyDecl::OBJC_PR_retain |
00159    ObjCPropertyDecl::OBJC_PR_copy   |
00160    ObjCPropertyDecl::OBJC_PR_weak   |
00161    ObjCPropertyDecl::OBJC_PR_strong |
00162    ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
00163 
00164 static unsigned getOwnershipRule(unsigned attr) {
00165   unsigned result = attr & OwnershipMask;
00166 
00167   // From an ownership perspective, assign and unsafe_unretained are
00168   // identical; make sure one also implies the other.
00169   if (result & (ObjCPropertyDecl::OBJC_PR_assign |
00170                 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
00171     result |= ObjCPropertyDecl::OBJC_PR_assign |
00172               ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
00173   }
00174 
00175   return result;
00176 }
00177 
00178 Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
00179                           SourceLocation LParenLoc,
00180                           FieldDeclarator &FD,
00181                           ObjCDeclSpec &ODS,
00182                           Selector GetterSel,
00183                           Selector SetterSel,
00184                           tok::ObjCKeywordKind MethodImplKind,
00185                           DeclContext *lexicalDC) {
00186   unsigned Attributes = ODS.getPropertyAttributes();
00187   FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
00188   TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
00189   QualType T = TSI->getType();
00190   if (!getOwnershipRule(Attributes)) {
00191     Attributes |= deducePropertyOwnershipFromType(*this, T);
00192   }
00193   bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
00194                       // default is readwrite!
00195                       !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
00196 
00197   // Proceed with constructing the ObjCPropertyDecls.
00198   ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
00199   ObjCPropertyDecl *Res = nullptr;
00200   if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
00201     if (CDecl->IsClassExtension()) {
00202       Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
00203                                            FD, GetterSel, SetterSel,
00204                                            isReadWrite,
00205                                            Attributes,
00206                                            ODS.getPropertyAttributes(),
00207                                            T, TSI, MethodImplKind);
00208       if (!Res)
00209         return nullptr;
00210     }
00211   }
00212 
00213   if (!Res) {
00214     Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
00215                              GetterSel, SetterSel, isReadWrite,
00216                              Attributes, ODS.getPropertyAttributes(),
00217                              T, TSI, MethodImplKind);
00218     if (lexicalDC)
00219       Res->setLexicalDeclContext(lexicalDC);
00220   }
00221 
00222   // Validate the attributes on the @property.
00223   CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
00224                               (isa<ObjCInterfaceDecl>(ClassDecl) ||
00225                                isa<ObjCProtocolDecl>(ClassDecl)));
00226 
00227   // Check consistency if the type has explicit ownership qualification.
00228   if (Res->getType().getObjCLifetime())
00229     checkPropertyDeclWithOwnership(*this, Res);
00230 
00231   llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
00232   if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
00233     // For a class, compare the property against a property in our superclass.
00234     bool FoundInSuper = false;
00235     ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
00236     while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
00237       DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
00238       for (unsigned I = 0, N = R.size(); I != N; ++I) {
00239         if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
00240           DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
00241           FoundInSuper = true;
00242           break;
00243         }
00244       }
00245       if (FoundInSuper)
00246         break;
00247       else
00248         CurrentInterfaceDecl = Super;
00249     }
00250 
00251     if (FoundInSuper) {
00252       // Also compare the property against a property in our protocols.
00253       for (auto *P : CurrentInterfaceDecl->protocols()) {
00254         CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
00255       }
00256     } else {
00257       // Slower path: look in all protocols we referenced.
00258       for (auto *P : IFace->all_referenced_protocols()) {
00259         CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
00260       }
00261     }
00262   } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
00263     // We don't check if class extension. Because properties in class extension
00264     // are meant to override some of the attributes and checking has already done
00265     // when property in class extension is constructed.
00266     if (!Cat->IsClassExtension())
00267       for (auto *P : Cat->protocols())
00268         CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
00269   } else {
00270     ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
00271     for (auto *P : Proto->protocols())
00272       CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
00273   }
00274 
00275   ActOnDocumentableDecl(Res);
00276   return Res;
00277 }
00278 
00279 static ObjCPropertyDecl::PropertyAttributeKind
00280 makePropertyAttributesAsWritten(unsigned Attributes) {
00281   unsigned attributesAsWritten = 0;
00282   if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
00283     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
00284   if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
00285     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
00286   if (Attributes & ObjCDeclSpec::DQ_PR_getter)
00287     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
00288   if (Attributes & ObjCDeclSpec::DQ_PR_setter)
00289     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
00290   if (Attributes & ObjCDeclSpec::DQ_PR_assign)
00291     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
00292   if (Attributes & ObjCDeclSpec::DQ_PR_retain)
00293     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
00294   if (Attributes & ObjCDeclSpec::DQ_PR_strong)
00295     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
00296   if (Attributes & ObjCDeclSpec::DQ_PR_weak)
00297     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
00298   if (Attributes & ObjCDeclSpec::DQ_PR_copy)
00299     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
00300   if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
00301     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
00302   if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
00303     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
00304   if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
00305     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
00306   if (Attributes & ObjCDeclSpec::DQ_PR_class)
00307     attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
00308   
00309   return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
00310 }
00311 
00312 static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, 
00313                                  SourceLocation LParenLoc, SourceLocation &Loc) {
00314   if (LParenLoc.isMacroID())
00315     return false;
00316   
00317   SourceManager &SM = Context.getSourceManager();
00318   std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
00319   // Try to load the file buffer.
00320   bool invalidTemp = false;
00321   StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
00322   if (invalidTemp)
00323     return false;
00324   const char *tokenBegin = file.data() + locInfo.second;
00325   
00326   // Lex from the start of the given location.
00327   Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
00328               Context.getLangOpts(),
00329               file.begin(), tokenBegin, file.end());
00330   Token Tok;
00331   do {
00332     lexer.LexFromRawLexer(Tok);
00333     if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
00334       Loc = Tok.getLocation();
00335       return true;
00336     }
00337   } while (Tok.isNot(tok::r_paren));
00338   return false;
00339   
00340 }
00341 
00342 /// Check for a mismatch in the atomicity of the given properties.
00343 static void checkAtomicPropertyMismatch(Sema &S,
00344                                         ObjCPropertyDecl *OldProperty,
00345                                         ObjCPropertyDecl *NewProperty,
00346                                         bool PropagateAtomicity) {
00347   // If the atomicity of both matches, we're done.
00348   bool OldIsAtomic =
00349     (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
00350       == 0;
00351   bool NewIsAtomic =
00352     (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
00353       == 0;
00354   if (OldIsAtomic == NewIsAtomic) return;
00355 
00356   // Determine whether the given property is readonly and implicitly
00357   // atomic.
00358   auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
00359     // Is it readonly?
00360     auto Attrs = Property->getPropertyAttributes();
00361     if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
00362 
00363     // Is it nonatomic?
00364     if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
00365 
00366     // Was 'atomic' specified directly?
00367     if (Property->getPropertyAttributesAsWritten() & 
00368           ObjCPropertyDecl::OBJC_PR_atomic)
00369       return false;
00370 
00371     return true;
00372   };
00373 
00374   // If we're allowed to propagate atomicity, and the new property did
00375   // not specify atomicity at all, propagate.
00376   const unsigned AtomicityMask =
00377     (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
00378   if (PropagateAtomicity &&
00379       ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
00380     unsigned Attrs = NewProperty->getPropertyAttributes();
00381     Attrs = Attrs & ~AtomicityMask;
00382     if (OldIsAtomic)
00383       Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
00384     else 
00385       Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
00386 
00387     NewProperty->overwritePropertyAttributes(Attrs);
00388     return;
00389   }
00390 
00391   // One of the properties is atomic; if it's a readonly property, and
00392   // 'atomic' wasn't explicitly specified, we're okay.
00393   if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
00394       (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
00395     return;
00396 
00397   // Diagnose the conflict.
00398   const IdentifierInfo *OldContextName;
00399   auto *OldDC = OldProperty->getDeclContext();
00400   if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
00401     OldContextName = Category->getClassInterface()->getIdentifier();
00402   else
00403     OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
00404 
00405   S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
00406     << NewProperty->getDeclName() << "atomic"
00407     << OldContextName;
00408   S.Diag(OldProperty->getLocation(), diag::note_property_declare);
00409 }
00410 
00411 ObjCPropertyDecl *
00412 Sema::HandlePropertyInClassExtension(Scope *S,
00413                                      SourceLocation AtLoc,
00414                                      SourceLocation LParenLoc,
00415                                      FieldDeclarator &FD,
00416                                      Selector GetterSel, Selector SetterSel,
00417                                      const bool isReadWrite,
00418                                      unsigned &Attributes,
00419                                      const unsigned AttributesAsWritten,
00420                                      QualType T,
00421                                      TypeSourceInfo *TSI,
00422                                      tok::ObjCKeywordKind MethodImplKind) {
00423   ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
00424   // Diagnose if this property is already in continuation class.
00425   DeclContext *DC = CurContext;
00426   IdentifierInfo *PropertyId = FD.D.getIdentifier();
00427   ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
00428   
00429   // We need to look in the @interface to see if the @property was
00430   // already declared.
00431   if (!CCPrimary) {
00432     Diag(CDecl->getLocation(), diag::err_continuation_class);
00433     return nullptr;
00434   }
00435 
00436   bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
00437                          (Attributes & ObjCDeclSpec::DQ_PR_class);
00438 
00439   // Find the property in the extended class's primary class or
00440   // extensions.
00441   ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
00442       PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
00443 
00444   // If we found a property in an extension, complain. 
00445   if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
00446     Diag(AtLoc, diag::err_duplicate_property);
00447     Diag(PIDecl->getLocation(), diag::note_property_declare);
00448     return nullptr;
00449   }
00450 
00451   // Check for consistency with the previous declaration, if there is one.
00452   if (PIDecl) {
00453     // A readonly property declared in the primary class can be refined
00454     // by adding a readwrite property within an extension.
00455     // Anything else is an error.
00456     if (!(PIDecl->isReadOnly() && isReadWrite)) {
00457       // Tailor the diagnostics for the common case where a readwrite
00458       // property is declared both in the @interface and the continuation.
00459       // This is a common error where the user often intended the original
00460       // declaration to be readonly.
00461       unsigned diag =
00462         (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
00463         (PIDecl->getPropertyAttributesAsWritten() &
00464            ObjCPropertyDecl::OBJC_PR_readwrite)
00465         ? diag::err_use_continuation_class_redeclaration_readwrite
00466         : diag::err_use_continuation_class;
00467       Diag(AtLoc, diag)
00468         << CCPrimary->getDeclName();
00469       Diag(PIDecl->getLocation(), diag::note_property_declare);
00470       return nullptr;
00471     }
00472 
00473     // Check for consistency of getters.
00474     if (PIDecl->getGetterName() != GetterSel) {
00475      // If the getter was written explicitly, complain.
00476       if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
00477         Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
00478           << PIDecl->getGetterName() << GetterSel;
00479         Diag(PIDecl->getLocation(), diag::note_property_declare);
00480       }
00481       
00482       // Always adopt the getter from the original declaration.
00483       GetterSel = PIDecl->getGetterName();
00484       Attributes |= ObjCDeclSpec::DQ_PR_getter;
00485     }
00486 
00487     // Check consistency of ownership.
00488     unsigned ExistingOwnership
00489       = getOwnershipRule(PIDecl->getPropertyAttributes());
00490     unsigned NewOwnership = getOwnershipRule(Attributes);
00491     if (ExistingOwnership && NewOwnership != ExistingOwnership) {
00492       // If the ownership was written explicitly, complain.
00493       if (getOwnershipRule(AttributesAsWritten)) {
00494         Diag(AtLoc, diag::warn_property_attr_mismatch);
00495         Diag(PIDecl->getLocation(), diag::note_property_declare);
00496       }
00497 
00498       // Take the ownership from the original property.
00499       Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
00500     }
00501 
00502     // If the redeclaration is 'weak' but the original property is not, 
00503     if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
00504         !(PIDecl->getPropertyAttributesAsWritten()
00505             & ObjCPropertyDecl::OBJC_PR_weak) &&
00506         PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
00507         PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
00508       Diag(AtLoc, diag::warn_property_implicitly_mismatched);
00509       Diag(PIDecl->getLocation(), diag::note_property_declare);
00510     }        
00511   }
00512 
00513   // Create a new ObjCPropertyDecl with the DeclContext being
00514   // the class extension.
00515   ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
00516                                                FD, GetterSel, SetterSel,
00517                                                isReadWrite,
00518                                                Attributes, AttributesAsWritten,
00519                                                T, TSI, MethodImplKind, DC);
00520 
00521   // If there was no declaration of a property with the same name in
00522   // the primary class, we're done.
00523   if (!PIDecl) {
00524     ProcessPropertyDecl(PDecl);
00525     return PDecl;
00526   }
00527 
00528   if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
00529     bool IncompatibleObjC = false;
00530     QualType ConvertedType;
00531     // Relax the strict type matching for property type in continuation class.
00532     // Allow property object type of continuation class to be different as long
00533     // as it narrows the object type in its primary class property. Note that
00534     // this conversion is safe only because the wider type is for a 'readonly'
00535     // property in primary class and 'narrowed' type for a 'readwrite' property
00536     // in continuation class.
00537     QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
00538     QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
00539     if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
00540         !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
00541         (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
00542                                   ConvertedType, IncompatibleObjC))
00543         || IncompatibleObjC) {
00544       Diag(AtLoc, 
00545           diag::err_type_mismatch_continuation_class) << PDecl->getType();
00546       Diag(PIDecl->getLocation(), diag::note_property_declare);
00547       return nullptr;
00548     }
00549   }
00550   
00551   // Check that atomicity of property in class extension matches the previous
00552   // declaration.
00553   checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
00554 
00555   // Make sure getter/setter are appropriately synthesized.
00556   ProcessPropertyDecl(PDecl);
00557   return PDecl;
00558 }
00559 
00560 ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
00561                                            ObjCContainerDecl *CDecl,
00562                                            SourceLocation AtLoc,
00563                                            SourceLocation LParenLoc,
00564                                            FieldDeclarator &FD,
00565                                            Selector GetterSel,
00566                                            Selector SetterSel,
00567                                            const bool isReadWrite,
00568                                            const unsigned Attributes,
00569                                            const unsigned AttributesAsWritten,
00570                                            QualType T,
00571                                            TypeSourceInfo *TInfo,
00572                                            tok::ObjCKeywordKind MethodImplKind,
00573                                            DeclContext *lexicalDC){
00574   IdentifierInfo *PropertyId = FD.D.getIdentifier();
00575 
00576   // Property defaults to 'assign' if it is readwrite, unless this is ARC
00577   // and the type is retainable.
00578   bool isAssign;
00579   if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
00580                     ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
00581     isAssign = true;
00582   } else if (getOwnershipRule(Attributes) || !isReadWrite) {
00583     isAssign = false;
00584   } else {
00585     isAssign = (!getLangOpts().ObjCAutoRefCount ||
00586                 !T->isObjCRetainableType());
00587   }
00588 
00589   // Issue a warning if property is 'assign' as default and its
00590   // object, which is gc'able conforms to NSCopying protocol
00591   if (getLangOpts().getGC() != LangOptions::NonGC &&
00592       isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
00593     if (const ObjCObjectPointerType *ObjPtrTy =
00594           T->getAs<ObjCObjectPointerType>()) {
00595       ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
00596       if (IDecl)
00597         if (ObjCProtocolDecl* PNSCopying =
00598             LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
00599           if (IDecl->ClassImplementsProtocol(PNSCopying, true))
00600             Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
00601     }
00602   }
00603 
00604   if (T->isObjCObjectType()) {
00605     SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
00606     StarLoc = getLocForEndOfToken(StarLoc);
00607     Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
00608       << FixItHint::CreateInsertion(StarLoc, "*");
00609     T = Context.getObjCObjectPointerType(T);
00610     SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
00611     TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
00612   }
00613 
00614   DeclContext *DC = cast<DeclContext>(CDecl);
00615   ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
00616                                                      FD.D.getIdentifierLoc(),
00617                                                      PropertyId, AtLoc, 
00618                                                      LParenLoc, T, TInfo);
00619 
00620   bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
00621                          (Attributes & ObjCDeclSpec::DQ_PR_class);
00622   // Class property and instance property can have the same name.
00623   if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
00624           DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
00625     Diag(PDecl->getLocation(), diag::err_duplicate_property);
00626     Diag(prevDecl->getLocation(), diag::note_property_declare);
00627     PDecl->setInvalidDecl();
00628   }
00629   else {
00630     DC->addDecl(PDecl);
00631     if (lexicalDC)
00632       PDecl->setLexicalDeclContext(lexicalDC);
00633   }
00634 
00635   if (T->isArrayType() || T->isFunctionType()) {
00636     Diag(AtLoc, diag::err_property_type) << T;
00637     PDecl->setInvalidDecl();
00638   }
00639 
00640   ProcessDeclAttributes(S, PDecl, FD.D);
00641 
00642   // Regardless of setter/getter attribute, we save the default getter/setter
00643   // selector names in anticipation of declaration of setter/getter methods.
00644   PDecl->setGetterName(GetterSel);
00645   PDecl->setSetterName(SetterSel);
00646   PDecl->setPropertyAttributesAsWritten(
00647                           makePropertyAttributesAsWritten(AttributesAsWritten));
00648 
00649   if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
00650     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
00651 
00652   if (Attributes & ObjCDeclSpec::DQ_PR_getter)
00653     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
00654 
00655   if (Attributes & ObjCDeclSpec::DQ_PR_setter)
00656     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
00657 
00658   if (isReadWrite)
00659     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
00660 
00661   if (Attributes & ObjCDeclSpec::DQ_PR_retain)
00662     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
00663 
00664   if (Attributes & ObjCDeclSpec::DQ_PR_strong)
00665     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
00666 
00667   if (Attributes & ObjCDeclSpec::DQ_PR_weak)
00668     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
00669 
00670   if (Attributes & ObjCDeclSpec::DQ_PR_copy)
00671     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
00672 
00673   if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
00674     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
00675 
00676   if (isAssign)
00677     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
00678 
00679   // In the semantic attributes, one of nonatomic or atomic is always set.
00680   if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
00681     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
00682   else
00683     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
00684 
00685   // 'unsafe_unretained' is alias for 'assign'.
00686   if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
00687     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
00688   if (isAssign)
00689     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
00690 
00691   if (MethodImplKind == tok::objc_required)
00692     PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
00693   else if (MethodImplKind == tok::objc_optional)
00694     PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
00695 
00696   if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
00697     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
00698 
00699   if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
00700     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
00701 
00702  if (Attributes & ObjCDeclSpec::DQ_PR_class)
00703     PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
00704 
00705   return PDecl;
00706 }
00707 
00708 static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
00709                                  ObjCPropertyDecl *property,
00710                                  ObjCIvarDecl *ivar) {
00711   if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
00712 
00713   QualType ivarType = ivar->getType();
00714   Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
00715 
00716   // The lifetime implied by the property's attributes.
00717   Qualifiers::ObjCLifetime propertyLifetime =
00718     getImpliedARCOwnership(property->getPropertyAttributes(),
00719                            property->getType());
00720 
00721   // We're fine if they match.
00722   if (propertyLifetime == ivarLifetime) return;
00723 
00724   // None isn't a valid lifetime for an object ivar in ARC, and
00725   // __autoreleasing is never valid; don't diagnose twice.
00726   if ((ivarLifetime == Qualifiers::OCL_None &&
00727        S.getLangOpts().ObjCAutoRefCount) ||
00728       ivarLifetime == Qualifiers::OCL_Autoreleasing)
00729     return;
00730 
00731   // If the ivar is private, and it's implicitly __unsafe_unretained
00732   // becaues of its type, then pretend it was actually implicitly
00733   // __strong.  This is only sound because we're processing the
00734   // property implementation before parsing any method bodies.
00735   if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
00736       propertyLifetime == Qualifiers::OCL_Strong &&
00737       ivar->getAccessControl() == ObjCIvarDecl::Private) {
00738     SplitQualType split = ivarType.split();
00739     if (split.Quals.hasObjCLifetime()) {
00740       assert(ivarType->isObjCARCImplicitlyUnretainedType());
00741       split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
00742       ivarType = S.Context.getQualifiedType(split);
00743       ivar->setType(ivarType);
00744       return;
00745     }
00746   }
00747 
00748   switch (propertyLifetime) {
00749   case Qualifiers::OCL_Strong:
00750     S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
00751       << property->getDeclName()
00752       << ivar->getDeclName()
00753       << ivarLifetime;
00754     break;
00755 
00756   case Qualifiers::OCL_Weak:
00757     S.Diag(ivar->getLocation(), diag::error_weak_property)
00758       << property->getDeclName()
00759       << ivar->getDeclName();
00760     break;
00761 
00762   case Qualifiers::OCL_ExplicitNone:
00763     S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
00764       << property->getDeclName()
00765       << ivar->getDeclName()
00766       << ((property->getPropertyAttributesAsWritten() 
00767            & ObjCPropertyDecl::OBJC_PR_assign) != 0);
00768     break;
00769 
00770   case Qualifiers::OCL_Autoreleasing:
00771     llvm_unreachable("properties cannot be autoreleasing");
00772 
00773   case Qualifiers::OCL_None:
00774     // Any other property should be ignored.
00775     return;
00776   }
00777 
00778   S.Diag(property->getLocation(), diag::note_property_declare);
00779   if (propertyImplLoc.isValid())
00780     S.Diag(propertyImplLoc, diag::note_property_synthesize);
00781 }
00782 
00783 /// setImpliedPropertyAttributeForReadOnlyProperty -
00784 /// This routine evaludates life-time attributes for a 'readonly'
00785 /// property with no known lifetime of its own, using backing
00786 /// 'ivar's attribute, if any. If no backing 'ivar', property's
00787 /// life-time is assumed 'strong'.
00788 static void setImpliedPropertyAttributeForReadOnlyProperty(
00789               ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
00790   Qualifiers::ObjCLifetime propertyLifetime = 
00791     getImpliedARCOwnership(property->getPropertyAttributes(),
00792                            property->getType());
00793   if (propertyLifetime != Qualifiers::OCL_None)
00794     return;
00795   
00796   if (!ivar) {
00797     // if no backing ivar, make property 'strong'.
00798     property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
00799     return;
00800   }
00801   // property assumes owenership of backing ivar.
00802   QualType ivarType = ivar->getType();
00803   Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
00804   if (ivarLifetime == Qualifiers::OCL_Strong)
00805     property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
00806   else if (ivarLifetime == Qualifiers::OCL_Weak)
00807     property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
00808   return;
00809 }
00810 
00811 /// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
00812 /// in inherited protocols with mismatched types. Since any of them can
00813 /// be candidate for synthesis.
00814 static void
00815 DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
00816                                         ObjCInterfaceDecl *ClassDecl,
00817                                         ObjCPropertyDecl *Property) {
00818   ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
00819   for (const auto *PI : ClassDecl->all_referenced_protocols()) {
00820     if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
00821       PDecl->collectInheritedProtocolProperties(Property, PropMap);
00822   }
00823   if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
00824     while (SDecl) {
00825       for (const auto *PI : SDecl->all_referenced_protocols()) {
00826         if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
00827           PDecl->collectInheritedProtocolProperties(Property, PropMap);
00828       }
00829       SDecl = SDecl->getSuperClass();
00830     }
00831   
00832   if (PropMap.empty())
00833     return;
00834   
00835   QualType RHSType = S.Context.getCanonicalType(Property->getType());
00836   bool FirsTime = true;
00837   for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
00838        I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
00839     ObjCPropertyDecl *Prop = I->second;
00840     QualType LHSType = S.Context.getCanonicalType(Prop->getType());
00841     if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
00842       bool IncompatibleObjC = false;
00843       QualType ConvertedType;
00844       if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
00845           || IncompatibleObjC) {
00846         if (FirsTime) {
00847           S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
00848             << Property->getType();
00849           FirsTime = false;
00850         }
00851         S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
00852           << Prop->getType();
00853       }
00854     }
00855   }
00856   if (!FirsTime && AtLoc.isValid())
00857     S.Diag(AtLoc, diag::note_property_synthesize);
00858 }
00859 
00860 /// Determine whether any storage attributes were written on the property.
00861 static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
00862                                        ObjCPropertyQueryKind QueryKind) {
00863   if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
00864 
00865   // If this is a readwrite property in a class extension that refines
00866   // a readonly property in the original class definition, check it as
00867   // well.
00868 
00869   // If it's a readonly property, we're not interested.
00870   if (Prop->isReadOnly()) return false;
00871 
00872   // Is it declared in an extension?
00873   auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
00874   if (!Category || !Category->IsClassExtension()) return false;
00875 
00876   // Find the corresponding property in the primary class definition.
00877   auto OrigClass = Category->getClassInterface();
00878   for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
00879     if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
00880       return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
00881   }
00882 
00883   // Look through all of the protocols.
00884   for (const auto *Proto : OrigClass->all_referenced_protocols()) {
00885     if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
00886             Prop->getIdentifier(), QueryKind))
00887       return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
00888   }
00889 
00890   return false;
00891 }
00892 
00893 /// ActOnPropertyImplDecl - This routine performs semantic checks and
00894 /// builds the AST node for a property implementation declaration; declared
00895 /// as \@synthesize or \@dynamic.
00896 ///
00897 Decl *Sema::ActOnPropertyImplDecl(Scope *S,
00898                                   SourceLocation AtLoc,
00899                                   SourceLocation PropertyLoc,
00900                                   bool Synthesize,
00901                                   IdentifierInfo *PropertyId,
00902                                   IdentifierInfo *PropertyIvar,
00903                                   SourceLocation PropertyIvarLoc,
00904                                   ObjCPropertyQueryKind QueryKind) {
00905   ObjCContainerDecl *ClassImpDecl =
00906     dyn_cast<ObjCContainerDecl>(CurContext);
00907   // Make sure we have a context for the property implementation declaration.
00908   if (!ClassImpDecl) {
00909     Diag(AtLoc, diag::error_missing_property_context);
00910     return nullptr;
00911   }
00912   if (PropertyIvarLoc.isInvalid())
00913     PropertyIvarLoc = PropertyLoc;
00914   SourceLocation PropertyDiagLoc = PropertyLoc;
00915   if (PropertyDiagLoc.isInvalid())
00916     PropertyDiagLoc = ClassImpDecl->getLocStart();
00917   ObjCPropertyDecl *property = nullptr;
00918   ObjCInterfaceDecl *IDecl = nullptr;
00919   // Find the class or category class where this property must have
00920   // a declaration.
00921   ObjCImplementationDecl *IC = nullptr;
00922   ObjCCategoryImplDecl *CatImplClass = nullptr;
00923   if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
00924     IDecl = IC->getClassInterface();
00925     // We always synthesize an interface for an implementation
00926     // without an interface decl. So, IDecl is always non-zero.
00927     assert(IDecl &&
00928            "ActOnPropertyImplDecl - @implementation without @interface");
00929 
00930     // Look for this property declaration in the @implementation's @interface
00931     property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
00932     if (!property) {
00933       Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
00934       return nullptr;
00935     }
00936     if (property->isClassProperty() && Synthesize) {
00937       Diag(PropertyLoc, diag::error_synthesize_on_class_property) << PropertyId;
00938       return nullptr;
00939     }
00940     unsigned PIkind = property->getPropertyAttributesAsWritten();
00941     if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
00942                    ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
00943       if (AtLoc.isValid())
00944         Diag(AtLoc, diag::warn_implicit_atomic_property);
00945       else
00946         Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
00947       Diag(property->getLocation(), diag::note_property_declare);
00948     }
00949     
00950     if (const ObjCCategoryDecl *CD =
00951         dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
00952       if (!CD->IsClassExtension()) {
00953         Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
00954         Diag(property->getLocation(), diag::note_property_declare);
00955         return nullptr;
00956       }
00957     }
00958     if (Synthesize&&
00959         (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
00960         property->hasAttr<IBOutletAttr>() &&
00961         !AtLoc.isValid()) {
00962       bool ReadWriteProperty = false;
00963       // Search into the class extensions and see if 'readonly property is
00964       // redeclared 'readwrite', then no warning is to be issued.
00965       for (auto *Ext : IDecl->known_extensions()) {
00966         DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
00967         if (!R.empty())
00968           if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
00969             PIkind = ExtProp->getPropertyAttributesAsWritten();
00970             if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
00971               ReadWriteProperty = true;
00972               break;
00973             }
00974           }
00975       }
00976       
00977       if (!ReadWriteProperty) {
00978         Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
00979             << property;
00980         SourceLocation readonlyLoc;
00981         if (LocPropertyAttribute(Context, "readonly", 
00982                                  property->getLParenLoc(), readonlyLoc)) {
00983           SourceLocation endLoc = 
00984             readonlyLoc.getLocWithOffset(strlen("readonly")-1);
00985           SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
00986           Diag(property->getLocation(), 
00987                diag::note_auto_readonly_iboutlet_fixup_suggest) <<
00988           FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
00989         }
00990       }
00991     }
00992     if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
00993       DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
00994         
00995   } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
00996     if (Synthesize) {
00997       Diag(AtLoc, diag::error_synthesize_category_decl);
00998       return nullptr;
00999     }
01000     IDecl = CatImplClass->getClassInterface();
01001     if (!IDecl) {
01002       Diag(AtLoc, diag::error_missing_property_interface);
01003       return nullptr;
01004     }
01005     ObjCCategoryDecl *Category =
01006     IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
01007 
01008     // If category for this implementation not found, it is an error which
01009     // has already been reported eralier.
01010     if (!Category)
01011       return nullptr;
01012     // Look for this property declaration in @implementation's category
01013     property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
01014     if (!property) {
01015       Diag(PropertyLoc, diag::error_bad_category_property_decl)
01016       << Category->getDeclName();
01017       return nullptr;
01018     }
01019   } else {
01020     Diag(AtLoc, diag::error_bad_property_context);
01021     return nullptr;
01022   }
01023   ObjCIvarDecl *Ivar = nullptr;
01024   bool CompleteTypeErr = false;
01025   bool compat = true;
01026   // Check that we have a valid, previously declared ivar for @synthesize
01027   if (Synthesize) {
01028     // @synthesize
01029     if (!PropertyIvar)
01030       PropertyIvar = PropertyId;
01031     // Check that this is a previously declared 'ivar' in 'IDecl' interface
01032     ObjCInterfaceDecl *ClassDeclared;
01033     Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
01034     QualType PropType = property->getType();
01035     QualType PropertyIvarType = PropType.getNonReferenceType();
01036 
01037     if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
01038                             diag::err_incomplete_synthesized_property,
01039                             property->getDeclName())) {
01040       Diag(property->getLocation(), diag::note_property_declare);
01041       CompleteTypeErr = true;
01042     }
01043 
01044     if (getLangOpts().ObjCAutoRefCount &&
01045         (property->getPropertyAttributesAsWritten() &
01046          ObjCPropertyDecl::OBJC_PR_readonly) &&
01047         PropertyIvarType->isObjCRetainableType()) {
01048       setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);    
01049     }
01050     
01051     ObjCPropertyDecl::PropertyAttributeKind kind 
01052       = property->getPropertyAttributes();
01053 
01054     bool isARCWeak = false;
01055     if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
01056       // Add GC __weak to the ivar type if the property is weak.
01057       if (getLangOpts().getGC() != LangOptions::NonGC) {
01058         assert(!getLangOpts().ObjCAutoRefCount);
01059         if (PropertyIvarType.isObjCGCStrong()) {
01060           Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
01061           Diag(property->getLocation(), diag::note_property_declare);
01062         } else {
01063           PropertyIvarType =
01064             Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
01065         }
01066 
01067       // Otherwise, check whether ARC __weak is enabled and works with
01068       // the property type.
01069       } else {
01070         if (!getLangOpts().ObjCWeak) {
01071           // Only complain here when synthesizing an ivar.
01072           if (!Ivar) {
01073             Diag(PropertyDiagLoc,
01074                  getLangOpts().ObjCWeakRuntime
01075                    ? diag::err_synthesizing_arc_weak_property_disabled
01076                    : diag::err_synthesizing_arc_weak_property_no_runtime);
01077             Diag(property->getLocation(), diag::note_property_declare);
01078           }
01079           CompleteTypeErr = true; // suppress later diagnostics about the ivar
01080         } else {
01081           isARCWeak = true;
01082           if (const ObjCObjectPointerType *ObjT =
01083                 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
01084             const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
01085             if (ObjI && ObjI->isArcWeakrefUnavailable()) {
01086               Diag(property->getLocation(),
01087                    diag::err_arc_weak_unavailable_property)
01088                 << PropertyIvarType;
01089               Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
01090                 << ClassImpDecl->getName();
01091             }
01092           }
01093         }
01094       }
01095     }
01096 
01097     if (AtLoc.isInvalid()) {
01098       // Check when default synthesizing a property that there is 
01099       // an ivar matching property name and issue warning; since this
01100       // is the most common case of not using an ivar used for backing
01101       // property in non-default synthesis case.
01102       ObjCInterfaceDecl *ClassDeclared=nullptr;
01103       ObjCIvarDecl *originalIvar = 
01104       IDecl->lookupInstanceVariable(property->getIdentifier(), 
01105                                     ClassDeclared);
01106       if (originalIvar) {
01107         Diag(PropertyDiagLoc, 
01108              diag::warn_autosynthesis_property_ivar_match)
01109         << PropertyId << (Ivar == nullptr) << PropertyIvar
01110         << originalIvar->getIdentifier();
01111         Diag(property->getLocation(), diag::note_property_declare);
01112         Diag(originalIvar->getLocation(), diag::note_ivar_decl);
01113       }
01114     }
01115     
01116     if (!Ivar) {
01117       // In ARC, give the ivar a lifetime qualifier based on the
01118       // property attributes.
01119       if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
01120           !PropertyIvarType.getObjCLifetime() &&
01121           PropertyIvarType->isObjCRetainableType()) {
01122 
01123         // It's an error if we have to do this and the user didn't
01124         // explicitly write an ownership attribute on the property.
01125         if (!hasWrittenStorageAttribute(property, QueryKind) &&
01126             !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
01127           Diag(PropertyDiagLoc,
01128                diag::err_arc_objc_property_default_assign_on_object);
01129           Diag(property->getLocation(), diag::note_property_declare);
01130         } else {
01131           Qualifiers::ObjCLifetime lifetime =
01132             getImpliedARCOwnership(kind, PropertyIvarType);
01133           assert(lifetime && "no lifetime for property?");
01134           
01135           Qualifiers qs;
01136           qs.addObjCLifetime(lifetime);
01137           PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);   
01138         }
01139       }
01140 
01141       Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
01142                                   PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
01143                                   PropertyIvarType, /*Dinfo=*/nullptr,
01144                                   ObjCIvarDecl::Private,
01145                                   (Expr *)nullptr, true);
01146       if (RequireNonAbstractType(PropertyIvarLoc,
01147                                  PropertyIvarType,
01148                                  diag::err_abstract_type_in_decl,
01149                                  AbstractSynthesizedIvarType)) {
01150         Diag(property->getLocation(), diag::note_property_declare);
01151         Ivar->setInvalidDecl();
01152       } else if (CompleteTypeErr)
01153           Ivar->setInvalidDecl();
01154       ClassImpDecl->addDecl(Ivar);
01155       IDecl->makeDeclVisibleInContext(Ivar);
01156 
01157       if (getLangOpts().ObjCRuntime.isFragile())
01158         Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
01159             << PropertyId;
01160       // Note! I deliberately want it to fall thru so, we have a
01161       // a property implementation and to avoid future warnings.
01162     } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
01163                !declaresSameEntity(ClassDeclared, IDecl)) {
01164       Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
01165       << property->getDeclName() << Ivar->getDeclName()
01166       << ClassDeclared->getDeclName();
01167       Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
01168       << Ivar << Ivar->getName();
01169       // Note! I deliberately want it to fall thru so more errors are caught.
01170     }
01171     property->setPropertyIvarDecl(Ivar);
01172 
01173     QualType IvarType = Context.getCanonicalType(Ivar->getType());
01174 
01175     // Check that type of property and its ivar are type compatible.
01176     if (!Context.hasSameType(PropertyIvarType, IvarType)) {
01177       if (isa<ObjCObjectPointerType>(PropertyIvarType) 
01178           && isa<ObjCObjectPointerType>(IvarType))
01179         compat =
01180           Context.canAssignObjCInterfaces(
01181                                   PropertyIvarType->getAs<ObjCObjectPointerType>(),
01182                                   IvarType->getAs<ObjCObjectPointerType>());
01183       else {
01184         compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
01185                                              IvarType)
01186                     == Compatible);
01187       }
01188       if (!compat) {
01189         Diag(PropertyDiagLoc, diag::error_property_ivar_type)
01190           << property->getDeclName() << PropType
01191           << Ivar->getDeclName() << IvarType;
01192         Diag(Ivar->getLocation(), diag::note_ivar_decl);
01193         // Note! I deliberately want it to fall thru so, we have a
01194         // a property implementation and to avoid future warnings.
01195       }
01196       else {
01197         // FIXME! Rules for properties are somewhat different that those
01198         // for assignments. Use a new routine to consolidate all cases;
01199         // specifically for property redeclarations as well as for ivars.
01200         QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
01201         QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
01202         if (lhsType != rhsType &&
01203             lhsType->isArithmeticType()) {
01204           Diag(PropertyDiagLoc, diag::error_property_ivar_type)
01205             << property->getDeclName() << PropType
01206             << Ivar->getDeclName() << IvarType;
01207           Diag(Ivar->getLocation(), diag::note_ivar_decl);
01208           // Fall thru - see previous comment
01209         }
01210       }
01211       // __weak is explicit. So it works on Canonical type.
01212       if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
01213            getLangOpts().getGC() != LangOptions::NonGC)) {
01214         Diag(PropertyDiagLoc, diag::error_weak_property)
01215         << property->getDeclName() << Ivar->getDeclName();
01216         Diag(Ivar->getLocation(), diag::note_ivar_decl);
01217         // Fall thru - see previous comment
01218       }
01219       // Fall thru - see previous comment
01220       if ((property->getType()->isObjCObjectPointerType() ||
01221            PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
01222           getLangOpts().getGC() != LangOptions::NonGC) {
01223         Diag(PropertyDiagLoc, diag::error_strong_property)
01224         << property->getDeclName() << Ivar->getDeclName();
01225         // Fall thru - see previous comment
01226       }
01227     }
01228     if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
01229         Ivar->getType().getObjCLifetime())
01230       checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
01231   } else if (PropertyIvar)
01232     // @dynamic
01233     Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
01234     
01235   assert (property && "ActOnPropertyImplDecl - property declaration missing");
01236   ObjCPropertyImplDecl *PIDecl =
01237   ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
01238                                property,
01239                                (Synthesize ?
01240                                 ObjCPropertyImplDecl::Synthesize
01241                                 : ObjCPropertyImplDecl::Dynamic),
01242                                Ivar, PropertyIvarLoc);
01243 
01244   if (CompleteTypeErr || !compat)
01245     PIDecl->setInvalidDecl();
01246 
01247   if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
01248     getterMethod->createImplicitParams(Context, IDecl);
01249     if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
01250         Ivar->getType()->isRecordType()) {
01251       // For Objective-C++, need to synthesize the AST for the IVAR object to be
01252       // returned by the getter as it must conform to C++'s copy-return rules.
01253       // FIXME. Eventually we want to do this for Objective-C as well.
01254       SynthesizedFunctionScope Scope(*this, getterMethod);
01255       ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
01256       DeclRefExpr *SelfExpr = 
01257         new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
01258                                   VK_LValue, PropertyDiagLoc);
01259       MarkDeclRefReferenced(SelfExpr);
01260       Expr *LoadSelfExpr =
01261         ImplicitCastExpr::Create(Context, SelfDecl->getType(),
01262                                  CK_LValueToRValue, SelfExpr, nullptr,
01263                                  VK_RValue);
01264       Expr *IvarRefExpr =
01265         new (Context) ObjCIvarRefExpr(Ivar,
01266                                       Ivar->getUsageType(SelfDecl->getType()),
01267                                       PropertyDiagLoc,
01268                                       Ivar->getLocation(),
01269                                       LoadSelfExpr, true, true);
01270       ExprResult Res = PerformCopyInitialization(
01271           InitializedEntity::InitializeResult(PropertyDiagLoc,
01272                                               getterMethod->getReturnType(),
01273                                               /*NRVO=*/false),
01274           PropertyDiagLoc, IvarRefExpr);
01275       if (!Res.isInvalid()) {
01276         Expr *ResExpr = Res.getAs<Expr>();
01277         if (ResExpr)
01278           ResExpr = MaybeCreateExprWithCleanups(ResExpr);
01279         PIDecl->setGetterCXXConstructor(ResExpr);
01280       }
01281     }
01282     if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
01283         !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
01284       Diag(getterMethod->getLocation(), 
01285            diag::warn_property_getter_owning_mismatch);
01286       Diag(property->getLocation(), diag::note_property_declare);
01287     }
01288     if (getLangOpts().ObjCAutoRefCount && Synthesize)
01289       switch (getterMethod->getMethodFamily()) {
01290         case OMF_retain:
01291         case OMF_retainCount:
01292         case OMF_release:
01293         case OMF_autorelease:
01294           Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
01295             << 1 << getterMethod->getSelector();
01296           break;
01297         default:
01298           break;
01299       }
01300   }
01301   if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
01302     setterMethod->createImplicitParams(Context, IDecl);
01303     if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
01304         Ivar->getType()->isRecordType()) {
01305       // FIXME. Eventually we want to do this for Objective-C as well.
01306       SynthesizedFunctionScope Scope(*this, setterMethod);
01307       ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
01308       DeclRefExpr *SelfExpr = 
01309         new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
01310                                   VK_LValue, PropertyDiagLoc);
01311       MarkDeclRefReferenced(SelfExpr);
01312       Expr *LoadSelfExpr =
01313         ImplicitCastExpr::Create(Context, SelfDecl->getType(),
01314                                  CK_LValueToRValue, SelfExpr, nullptr,
01315                                  VK_RValue);
01316       Expr *lhs =
01317         new (Context) ObjCIvarRefExpr(Ivar,
01318                                       Ivar->getUsageType(SelfDecl->getType()),
01319                                       PropertyDiagLoc,
01320                                       Ivar->getLocation(),
01321                                       LoadSelfExpr, true, true);
01322       ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
01323       ParmVarDecl *Param = (*P);
01324       QualType T = Param->getType().getNonReferenceType();
01325       DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
01326                                                    VK_LValue, PropertyDiagLoc);
01327       MarkDeclRefReferenced(rhs);
01328       ExprResult Res = BuildBinOp(S, PropertyDiagLoc, 
01329                                   BO_Assign, lhs, rhs);
01330       if (property->getPropertyAttributes() & 
01331           ObjCPropertyDecl::OBJC_PR_atomic) {
01332         Expr *callExpr = Res.getAs<Expr>();
01333         if (const CXXOperatorCallExpr *CXXCE = 
01334               dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
01335           if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
01336             if (!FuncDecl->isTrivial())
01337               if (property->getType()->isReferenceType()) {
01338                 Diag(PropertyDiagLoc, 
01339                      diag::err_atomic_property_nontrivial_assign_op)
01340                     << property->getType();
01341                 Diag(FuncDecl->getLocStart(), 
01342                      diag::note_callee_decl) << FuncDecl;
01343               }
01344       }
01345       PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
01346     }
01347   }
01348   
01349   if (IC) {
01350     if (Synthesize)
01351       if (ObjCPropertyImplDecl *PPIDecl =
01352           IC->FindPropertyImplIvarDecl(PropertyIvar)) {
01353         Diag(PropertyLoc, diag::error_duplicate_ivar_use)
01354         << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
01355         << PropertyIvar;
01356         Diag(PPIDecl->getLocation(), diag::note_previous_use);
01357       }
01358 
01359     if (ObjCPropertyImplDecl *PPIDecl
01360         = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
01361       Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
01362       Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
01363       return nullptr;
01364     }
01365     IC->addPropertyImplementation(PIDecl);
01366     if (getLangOpts().ObjCDefaultSynthProperties &&
01367         getLangOpts().ObjCRuntime.isNonFragile() &&
01368         !IDecl->isObjCRequiresPropertyDefs()) {
01369       // Diagnose if an ivar was lazily synthesdized due to a previous
01370       // use and if 1) property is @dynamic or 2) property is synthesized
01371       // but it requires an ivar of different name.
01372       ObjCInterfaceDecl *ClassDeclared=nullptr;
01373       ObjCIvarDecl *Ivar = nullptr;
01374       if (!Synthesize)
01375         Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
01376       else {
01377         if (PropertyIvar && PropertyIvar != PropertyId)
01378           Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
01379       }
01380       // Issue diagnostics only if Ivar belongs to current class.
01381       if (Ivar && Ivar->getSynthesize() && 
01382           declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
01383         Diag(Ivar->getLocation(), diag::err_undeclared_var_use) 
01384         << PropertyId;
01385         Ivar->setInvalidDecl();
01386       }
01387     }
01388   } else {
01389     if (Synthesize)
01390       if (ObjCPropertyImplDecl *PPIDecl =
01391           CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
01392         Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
01393         << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
01394         << PropertyIvar;
01395         Diag(PPIDecl->getLocation(), diag::note_previous_use);
01396       }
01397 
01398     if (ObjCPropertyImplDecl *PPIDecl =
01399         CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
01400       Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
01401       Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
01402       return nullptr;
01403     }
01404     CatImplClass->addPropertyImplementation(PIDecl);
01405   }
01406 
01407   return PIDecl;
01408 }
01409 
01410 //===----------------------------------------------------------------------===//
01411 // Helper methods.
01412 //===----------------------------------------------------------------------===//
01413 
01414 /// DiagnosePropertyMismatch - Compares two properties for their
01415 /// attributes and types and warns on a variety of inconsistencies.
01416 ///
01417 void
01418 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
01419                                ObjCPropertyDecl *SuperProperty,
01420                                const IdentifierInfo *inheritedName,
01421                                bool OverridingProtocolProperty) {
01422   ObjCPropertyDecl::PropertyAttributeKind CAttr =
01423     Property->getPropertyAttributes();
01424   ObjCPropertyDecl::PropertyAttributeKind SAttr =
01425     SuperProperty->getPropertyAttributes();
01426   
01427   // We allow readonly properties without an explicit ownership
01428   // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
01429   // to be overridden by a property with any explicit ownership in the subclass.
01430   if (!OverridingProtocolProperty &&
01431       !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
01432     ;
01433   else {
01434     if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
01435         && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
01436       Diag(Property->getLocation(), diag::warn_readonly_property)
01437         << Property->getDeclName() << inheritedName;
01438     if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
01439         != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
01440       Diag(Property->getLocation(), diag::warn_property_attribute)
01441         << Property->getDeclName() << "copy" << inheritedName;
01442     else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
01443       unsigned CAttrRetain =
01444         (CAttr &
01445          (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
01446       unsigned SAttrRetain =
01447         (SAttr &
01448          (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
01449       bool CStrong = (CAttrRetain != 0);
01450       bool SStrong = (SAttrRetain != 0);
01451       if (CStrong != SStrong)
01452         Diag(Property->getLocation(), diag::warn_property_attribute)
01453           << Property->getDeclName() << "retain (or strong)" << inheritedName;
01454     }
01455   }
01456 
01457   // Check for nonatomic; note that nonatomic is effectively
01458   // meaningless for readonly properties, so don't diagnose if the
01459   // atomic property is 'readonly'.
01460   checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
01461   if (Property->getSetterName() != SuperProperty->getSetterName()) {
01462     Diag(Property->getLocation(), diag::warn_property_attribute)
01463       << Property->getDeclName() << "setter" << inheritedName;
01464     Diag(SuperProperty->getLocation(), diag::note_property_declare);
01465   }
01466   if (Property->getGetterName() != SuperProperty->getGetterName()) {
01467     Diag(Property->getLocation(), diag::warn_property_attribute)
01468       << Property->getDeclName() << "getter" << inheritedName;
01469     Diag(SuperProperty->getLocation(), diag::note_property_declare);
01470   }
01471 
01472   QualType LHSType =
01473     Context.getCanonicalType(SuperProperty->getType());
01474   QualType RHSType =
01475     Context.getCanonicalType(Property->getType());
01476 
01477   if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
01478     // Do cases not handled in above.
01479     // FIXME. For future support of covariant property types, revisit this.
01480     bool IncompatibleObjC = false;
01481     QualType ConvertedType;
01482     if (!isObjCPointerConversion(RHSType, LHSType, 
01483                                  ConvertedType, IncompatibleObjC) ||
01484         IncompatibleObjC) {
01485         Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
01486         << Property->getType() << SuperProperty->getType() << inheritedName;
01487       Diag(SuperProperty->getLocation(), diag::note_property_declare);
01488     }
01489   }
01490 }
01491 
01492 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
01493                                             ObjCMethodDecl *GetterMethod,
01494                                             SourceLocation Loc) {
01495   if (!GetterMethod)
01496     return false;
01497   QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
01498   QualType PropertyIvarType = property->getType().getNonReferenceType();
01499   bool compat = Context.hasSameType(PropertyIvarType, GetterType);
01500   if (!compat) {
01501     const ObjCObjectPointerType *propertyObjCPtr = nullptr;
01502     const ObjCObjectPointerType *getterObjCPtr = nullptr;
01503     if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) && 
01504         (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
01505       compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
01506     else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType) 
01507               != Compatible) {
01508           Diag(Loc, diag::error_property_accessor_type)
01509             << property->getDeclName() << PropertyIvarType
01510             << GetterMethod->getSelector() << GetterType;
01511           Diag(GetterMethod->getLocation(), diag::note_declared_at);
01512           return true;
01513     } else {
01514       compat = true;
01515       QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
01516       QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
01517       if (lhsType != rhsType && lhsType->isArithmeticType())
01518         compat = false;
01519     }
01520   }
01521   
01522   if (!compat) {
01523     Diag(Loc, diag::warn_accessor_property_type_mismatch)
01524     << property->getDeclName()
01525     << GetterMethod->getSelector();
01526     Diag(GetterMethod->getLocation(), diag::note_declared_at);
01527     return true;
01528   }
01529 
01530   return false;
01531 }
01532 
01533 /// CollectImmediateProperties - This routine collects all properties in
01534 /// the class and its conforming protocols; but not those in its super class.
01535 static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
01536                                        ObjCContainerDecl::PropertyMap &PropMap,
01537                                        ObjCContainerDecl::PropertyMap &SuperPropMap,
01538                                        bool IncludeProtocols = true) {
01539 
01540   if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
01541     for (auto *Prop : IDecl->properties())
01542       PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
01543           Prop;
01544 
01545     // Collect the properties from visible extensions.
01546     for (auto *Ext : IDecl->visible_extensions())
01547       CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
01548 
01549     if (IncludeProtocols) {
01550       // Scan through class's protocols.
01551       for (auto *PI : IDecl->all_referenced_protocols())
01552         CollectImmediateProperties(PI, PropMap, SuperPropMap);
01553     }
01554   }
01555   if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
01556     for (auto *Prop : CATDecl->properties())
01557       PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
01558           Prop;
01559     if (IncludeProtocols) {
01560       // Scan through class's protocols.
01561       for (auto *PI : CATDecl->protocols())
01562         CollectImmediateProperties(PI, PropMap, SuperPropMap);
01563     }
01564   }
01565   else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
01566     for (auto *Prop : PDecl->properties()) {
01567       ObjCPropertyDecl *PropertyFromSuper =
01568           SuperPropMap[std::make_pair(Prop->getIdentifier(),
01569                                       Prop->isClassProperty())];
01570       // Exclude property for protocols which conform to class's super-class, 
01571       // as super-class has to implement the property.
01572       if (!PropertyFromSuper || 
01573           PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
01574         ObjCPropertyDecl *&PropEntry =
01575             PropMap[std::make_pair(Prop->getIdentifier(),
01576                                    Prop->isClassProperty())];
01577         if (!PropEntry)
01578           PropEntry = Prop;
01579       }
01580     }
01581     // scan through protocol's protocols.
01582     for (auto *PI : PDecl->protocols())
01583       CollectImmediateProperties(PI, PropMap, SuperPropMap);
01584   }
01585 }
01586 
01587 /// CollectSuperClassPropertyImplementations - This routine collects list of
01588 /// properties to be implemented in super class(s) and also coming from their
01589 /// conforming protocols.
01590 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
01591                                     ObjCInterfaceDecl::PropertyMap &PropMap) {
01592   if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
01593     ObjCInterfaceDecl::PropertyDeclOrder PO;
01594     while (SDecl) {
01595       SDecl->collectPropertiesToImplement(PropMap, PO);
01596       SDecl = SDecl->getSuperClass();
01597     }
01598   }
01599 }
01600 
01601 /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
01602 /// an ivar synthesized for 'Method' and 'Method' is a property accessor
01603 /// declared in class 'IFace'.
01604 bool
01605 Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
01606                                      ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
01607   if (!IV->getSynthesize())
01608     return false;
01609   ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
01610                                             Method->isInstanceMethod());
01611   if (!IMD || !IMD->isPropertyAccessor())
01612     return false;
01613   
01614   // look up a property declaration whose one of its accessors is implemented
01615   // by this method.
01616   for (const auto *Property : IFace->instance_properties()) {
01617     if ((Property->getGetterName() == IMD->getSelector() ||
01618          Property->getSetterName() == IMD->getSelector()) &&
01619         (Property->getPropertyIvarDecl() == IV))
01620       return true;
01621   }
01622   // Also look up property declaration in class extension whose one of its
01623   // accessors is implemented by this method.
01624   for (const auto *Ext : IFace->known_extensions())
01625     for (const auto *Property : Ext->instance_properties())
01626       if ((Property->getGetterName() == IMD->getSelector() ||
01627            Property->getSetterName() == IMD->getSelector()) &&
01628           (Property->getPropertyIvarDecl() == IV))
01629         return true;
01630   return false;
01631 }
01632 
01633 static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
01634                                          ObjCPropertyDecl *Prop) {
01635   bool SuperClassImplementsGetter = false;
01636   bool SuperClassImplementsSetter = false;
01637   if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
01638     SuperClassImplementsSetter = true;
01639 
01640   while (IDecl->getSuperClass()) {
01641     ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
01642     if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
01643       SuperClassImplementsGetter = true;
01644 
01645     if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
01646       SuperClassImplementsSetter = true;
01647     if (SuperClassImplementsGetter && SuperClassImplementsSetter)
01648       return true;
01649     IDecl = IDecl->getSuperClass();
01650   }
01651   return false;
01652 }
01653 
01654 /// \brief Default synthesizes all properties which must be synthesized
01655 /// in class's \@implementation.
01656 void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
01657                                        ObjCInterfaceDecl *IDecl) {
01658   
01659   ObjCInterfaceDecl::PropertyMap PropMap;
01660   ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
01661   IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
01662   if (PropMap.empty())
01663     return;
01664   ObjCInterfaceDecl::PropertyMap SuperPropMap;
01665   CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
01666   
01667   for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
01668     ObjCPropertyDecl *Prop = PropertyOrder[i];
01669     // Is there a matching property synthesize/dynamic?
01670     if (Prop->isInvalidDecl() ||
01671         Prop->isClassProperty() ||
01672         Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
01673       continue;
01674     // Property may have been synthesized by user.
01675     if (IMPDecl->FindPropertyImplDecl(
01676             Prop->getIdentifier(), Prop->getQueryKind()))
01677       continue;
01678     if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
01679       if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
01680         continue;
01681       if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
01682         continue;
01683     }
01684     if (ObjCPropertyImplDecl *PID =
01685         IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
01686       Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
01687         << Prop->getIdentifier();
01688       if (PID->getLocation().isValid())
01689         Diag(PID->getLocation(), diag::note_property_synthesize);
01690       continue;
01691     }
01692     ObjCPropertyDecl *PropInSuperClass =
01693         SuperPropMap[std::make_pair(Prop->getIdentifier(),
01694                                     Prop->isClassProperty())];
01695     if (ObjCProtocolDecl *Proto =
01696           dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
01697       // We won't auto-synthesize properties declared in protocols.
01698       // Suppress the warning if class's superclass implements property's
01699       // getter and implements property's setter (if readwrite property).
01700       // Or, if property is going to be implemented in its super class.
01701       if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
01702         Diag(IMPDecl->getLocation(),
01703              diag::warn_auto_synthesizing_protocol_property)
01704           << Prop << Proto;
01705         Diag(Prop->getLocation(), diag::note_property_declare);
01706       }
01707       continue;
01708     }
01709     // If property to be implemented in the super class, ignore.
01710     if (PropInSuperClass) {
01711       if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
01712           (PropInSuperClass->getPropertyAttributes() &
01713            ObjCPropertyDecl::OBJC_PR_readonly) &&
01714           !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
01715           !IDecl->HasUserDeclaredSetterMethod(Prop)) {
01716         Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
01717         << Prop->getIdentifier();
01718         Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
01719       }
01720       else {
01721         Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
01722         << Prop->getIdentifier();
01723         Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
01724         Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
01725       }
01726       continue;
01727     }
01728     // We use invalid SourceLocations for the synthesized ivars since they
01729     // aren't really synthesized at a particular location; they just exist.
01730     // Saying that they are located at the @implementation isn't really going
01731     // to help users.
01732     ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
01733       ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
01734                             true,
01735                             /* property = */ Prop->getIdentifier(),
01736                             /* ivar = */ Prop->getDefaultSynthIvarName(Context),
01737                             Prop->getLocation(), Prop->getQueryKind()));
01738     if (PIDecl) {
01739       Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
01740       Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
01741     }
01742   }
01743 }
01744 
01745 void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
01746   if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
01747     return;
01748   ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
01749   if (!IC)
01750     return;
01751   if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
01752     if (!IDecl->isObjCRequiresPropertyDefs())
01753       DefaultSynthesizeProperties(S, IC, IDecl);
01754 }
01755 
01756 static void DiagnoseUnimplementedAccessor(Sema &S,
01757                                           ObjCInterfaceDecl *PrimaryClass,
01758                                           Selector Method,
01759                                           ObjCImplDecl* IMPDecl,
01760                                           ObjCContainerDecl *CDecl,
01761                                           ObjCCategoryDecl *C,
01762                                           ObjCPropertyDecl *Prop,
01763                                           Sema::SelectorSet &SMap) {
01764   // When reporting on missing property setter/getter implementation in
01765   // categories, do not report when they are declared in primary class,
01766   // class's protocol, or one of it super classes. This is because,
01767   // the class is going to implement them.
01768   if (!SMap.count(Method) &&
01769       (PrimaryClass == nullptr ||
01770        !PrimaryClass->lookupPropertyAccessor(Method, C,
01771                                              Prop->isClassProperty()))) {
01772         S.Diag(IMPDecl->getLocation(),
01773                isa<ObjCCategoryDecl>(CDecl) ?
01774                diag::warn_setter_getter_impl_required_in_category :
01775                diag::warn_setter_getter_impl_required)
01776             << Prop->getDeclName() << Method;
01777         S.Diag(Prop->getLocation(),
01778              diag::note_property_declare);
01779         if (S.LangOpts.ObjCDefaultSynthProperties &&
01780             S.LangOpts.ObjCRuntime.isNonFragile())
01781           if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
01782             if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
01783             S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
01784       }
01785 }
01786 
01787 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
01788                                            ObjCContainerDecl *CDecl,
01789                                            bool SynthesizeProperties) {
01790   ObjCContainerDecl::PropertyMap PropMap;
01791   ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
01792 
01793   if (!SynthesizeProperties) {
01794     ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
01795     // Gather properties which need not be implemented in this class
01796     // or category.
01797     if (!IDecl)
01798       if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
01799         // For categories, no need to implement properties declared in
01800         // its primary class (and its super classes) if property is
01801         // declared in one of those containers.
01802         if ((IDecl = C->getClassInterface())) {
01803           ObjCInterfaceDecl::PropertyDeclOrder PO;
01804           IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
01805         }
01806       }
01807     if (IDecl)
01808       CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
01809     
01810     CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
01811   }
01812 
01813   // Scan the @interface to see if any of the protocols it adopts
01814   // require an explicit implementation, via attribute
01815   // 'objc_protocol_requires_explicit_implementation'.
01816   if (IDecl) {
01817     std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
01818 
01819     for (auto *PDecl : IDecl->all_referenced_protocols()) {
01820       if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
01821         continue;
01822       // Lazily construct a set of all the properties in the @interface
01823       // of the class, without looking at the superclass.  We cannot
01824       // use the call to CollectImmediateProperties() above as that
01825       // utilizes information from the super class's properties as well
01826       // as scans the adopted protocols.  This work only triggers for protocols
01827       // with the attribute, which is very rare, and only occurs when
01828       // analyzing the @implementation.
01829       if (!LazyMap) {
01830         ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
01831         LazyMap.reset(new ObjCContainerDecl::PropertyMap());
01832         CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
01833                                    /* IncludeProtocols */ false);
01834       }
01835       // Add the properties of 'PDecl' to the list of properties that
01836       // need to be implemented.
01837       for (auto *PropDecl : PDecl->properties()) {
01838         if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
01839                                       PropDecl->isClassProperty())])
01840           continue;
01841         PropMap[std::make_pair(PropDecl->getIdentifier(),
01842                                PropDecl->isClassProperty())] = PropDecl;
01843       }
01844     }
01845   }
01846 
01847   if (PropMap.empty())
01848     return;
01849 
01850   llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
01851   for (const auto *I : IMPDecl->property_impls())
01852     PropImplMap.insert(I->getPropertyDecl());
01853 
01854   SelectorSet InsMap;
01855   // Collect property accessors implemented in current implementation.
01856   for (const auto *I : IMPDecl->methods())
01857     InsMap.insert(I->getSelector());
01858   
01859   ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
01860   ObjCInterfaceDecl *PrimaryClass = nullptr;
01861   if (C && !C->IsClassExtension())
01862     if ((PrimaryClass = C->getClassInterface()))
01863       // Report unimplemented properties in the category as well.
01864       if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
01865         // When reporting on missing setter/getters, do not report when
01866         // setter/getter is implemented in category's primary class
01867         // implementation.
01868         for (const auto *I : IMP->methods())
01869           InsMap.insert(I->getSelector());
01870       }
01871 
01872   for (ObjCContainerDecl::PropertyMap::iterator
01873        P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
01874     ObjCPropertyDecl *Prop = P->second;
01875     // Is there a matching propery synthesize/dynamic?
01876     if (Prop->isInvalidDecl() ||
01877         Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
01878         PropImplMap.count(Prop) ||
01879         Prop->getAvailability() == AR_Unavailable)
01880       continue;
01881 
01882     // Diagnose unimplemented getters and setters.
01883     DiagnoseUnimplementedAccessor(*this,
01884           PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
01885     if (!Prop->isReadOnly())
01886       DiagnoseUnimplementedAccessor(*this,
01887                                     PrimaryClass, Prop->getSetterName(),
01888                                     IMPDecl, CDecl, C, Prop, InsMap);
01889   }
01890 }
01891 
01892 void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
01893   for (const auto *propertyImpl : impDecl->property_impls()) {
01894     const auto *property = propertyImpl->getPropertyDecl();
01895 
01896     // Warn about null_resettable properties with synthesized setters,
01897     // because the setter won't properly handle nil.
01898     if (propertyImpl->getPropertyImplementation()
01899           == ObjCPropertyImplDecl::Synthesize &&
01900         (property->getPropertyAttributes() &
01901          ObjCPropertyDecl::OBJC_PR_null_resettable) &&
01902         property->getGetterMethodDecl() &&
01903         property->getSetterMethodDecl()) {
01904       auto *getterMethod = property->getGetterMethodDecl();
01905       auto *setterMethod = property->getSetterMethodDecl();
01906       if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
01907           !impDecl->getInstanceMethod(getterMethod->getSelector())) {
01908         SourceLocation loc = propertyImpl->getLocation();
01909         if (loc.isInvalid())
01910           loc = impDecl->getLocStart();
01911 
01912         Diag(loc, diag::warn_null_resettable_setter)
01913           << setterMethod->getSelector() << property->getDeclName();
01914       }
01915     }
01916   }
01917 }
01918 
01919 void
01920 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
01921                                        ObjCInterfaceDecl* IDecl) {
01922   // Rules apply in non-GC mode only
01923   if (getLangOpts().getGC() != LangOptions::NonGC)
01924     return;
01925   ObjCContainerDecl::PropertyMap PM;
01926   for (auto *Prop : IDecl->properties())
01927     PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
01928   for (const auto *Ext : IDecl->known_extensions())
01929     for (auto *Prop : Ext->properties())
01930       PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
01931     
01932   for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
01933        I != E; ++I) {
01934     const ObjCPropertyDecl *Property = I->second;
01935     ObjCMethodDecl *GetterMethod = nullptr;
01936     ObjCMethodDecl *SetterMethod = nullptr;
01937     bool LookedUpGetterSetter = false;
01938 
01939     unsigned Attributes = Property->getPropertyAttributes();
01940     unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
01941 
01942     if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
01943         !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
01944       GetterMethod = Property->isClassProperty() ?
01945                      IMPDecl->getClassMethod(Property->getGetterName()) :
01946                      IMPDecl->getInstanceMethod(Property->getGetterName());
01947       SetterMethod = Property->isClassProperty() ?
01948                      IMPDecl->getClassMethod(Property->getSetterName()) :
01949                      IMPDecl->getInstanceMethod(Property->getSetterName());
01950       LookedUpGetterSetter = true;
01951       if (GetterMethod) {
01952         Diag(GetterMethod->getLocation(),
01953              diag::warn_default_atomic_custom_getter_setter)
01954           << Property->getIdentifier() << 0;
01955         Diag(Property->getLocation(), diag::note_property_declare);
01956       }
01957       if (SetterMethod) {
01958         Diag(SetterMethod->getLocation(),
01959              diag::warn_default_atomic_custom_getter_setter)
01960           << Property->getIdentifier() << 1;
01961         Diag(Property->getLocation(), diag::note_property_declare);
01962       }
01963     }
01964 
01965     // We only care about readwrite atomic property.
01966     if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
01967         !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
01968       continue;
01969     if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
01970             Property->getIdentifier(), Property->getQueryKind())) {
01971       if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
01972         continue;
01973       if (!LookedUpGetterSetter) {
01974         GetterMethod = Property->isClassProperty() ?
01975                        IMPDecl->getClassMethod(Property->getGetterName()) :
01976                        IMPDecl->getInstanceMethod(Property->getGetterName());
01977         SetterMethod = Property->isClassProperty() ?
01978                        IMPDecl->getClassMethod(Property->getSetterName()) :
01979                        IMPDecl->getInstanceMethod(Property->getSetterName());
01980       }
01981       if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
01982         SourceLocation MethodLoc =
01983           (GetterMethod ? GetterMethod->getLocation()
01984                         : SetterMethod->getLocation());
01985         Diag(MethodLoc, diag::warn_atomic_property_rule)
01986           << Property->getIdentifier() << (GetterMethod != nullptr)
01987           << (SetterMethod != nullptr);
01988         // fixit stuff.
01989         if (Property->getLParenLoc().isValid() &&
01990             !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
01991           // @property () ... case.
01992           SourceLocation AfterLParen =
01993             getLocForEndOfToken(Property->getLParenLoc());
01994           StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
01995                                                       : "nonatomic";
01996           Diag(Property->getLocation(),
01997                diag::note_atomic_property_fixup_suggest)
01998             << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
01999         } else if (Property->getLParenLoc().isInvalid()) {
02000           //@property id etc.
02001           SourceLocation startLoc = 
02002             Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
02003           Diag(Property->getLocation(),
02004                diag::note_atomic_property_fixup_suggest)
02005             << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
02006         }
02007         else
02008           Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
02009         Diag(Property->getLocation(), diag::note_property_declare);
02010       }
02011     }
02012   }
02013 }
02014 
02015 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
02016   if (getLangOpts().getGC() == LangOptions::GCOnly)
02017     return;
02018 
02019   for (const auto *PID : D->property_impls()) {
02020     const ObjCPropertyDecl *PD = PID->getPropertyDecl();
02021     if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
02022         !PD->isClassProperty() &&
02023         !D->getInstanceMethod(PD->getGetterName())) {
02024       ObjCMethodDecl *method = PD->getGetterMethodDecl();
02025       if (!method)
02026         continue;
02027       ObjCMethodFamily family = method->getMethodFamily();
02028       if (family == OMF_alloc || family == OMF_copy ||
02029           family == OMF_mutableCopy || family == OMF_new) {
02030         if (getLangOpts().ObjCAutoRefCount)
02031           Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
02032         else
02033           Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
02034 
02035         // Look for a getter explicitly declared alongside the property.
02036         // If we find one, use its location for the note.
02037         SourceLocation noteLoc = PD->getLocation();
02038         SourceLocation fixItLoc;
02039         for (auto *getterRedecl : method->redecls()) {
02040           if (getterRedecl->isImplicit())
02041             continue;
02042           if (getterRedecl->getDeclContext() != PD->getDeclContext())
02043             continue;
02044           noteLoc = getterRedecl->getLocation();
02045           fixItLoc = getterRedecl->getLocEnd();
02046         }
02047 
02048         Preprocessor &PP = getPreprocessor();
02049         TokenValue tokens[] = {
02050           tok::kw___attribute, tok::l_paren, tok::l_paren,
02051           PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
02052           PP.getIdentifierInfo("none"), tok::r_paren,
02053           tok::r_paren, tok::r_paren
02054         };
02055         StringRef spelling = "__attribute__((objc_method_family(none)))";
02056         StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
02057         if (!macroName.empty())
02058           spelling = macroName;
02059 
02060         auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
02061             << method->getDeclName() << spelling;
02062         if (fixItLoc.isValid()) {
02063           SmallString<64> fixItText(" ");
02064           fixItText += spelling;
02065           noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
02066         }
02067       }
02068     }
02069   }
02070 }
02071 
02072 void Sema::DiagnoseMissingDesignatedInitOverrides(
02073                                             const ObjCImplementationDecl *ImplD,
02074                                             const ObjCInterfaceDecl *IFD) {
02075   assert(IFD->hasDesignatedInitializers());
02076   const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
02077   if (!SuperD)
02078     return;
02079 
02080   SelectorSet InitSelSet;
02081   for (const auto *I : ImplD->instance_methods())
02082     if (I->getMethodFamily() == OMF_init)
02083       InitSelSet.insert(I->getSelector());
02084 
02085   SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
02086   SuperD->getDesignatedInitializers(DesignatedInits);
02087   for (SmallVector<const ObjCMethodDecl *, 8>::iterator
02088          I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
02089     const ObjCMethodDecl *MD = *I;
02090     if (!InitSelSet.count(MD->getSelector())) {
02091       bool Ignore = false;
02092       if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
02093         Ignore = IMD->isUnavailable();
02094       }
02095       if (!Ignore) {
02096         Diag(ImplD->getLocation(),
02097              diag::warn_objc_implementation_missing_designated_init_override)
02098           << MD->getSelector();
02099         Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
02100       }
02101     }
02102   }
02103 }
02104 
02105 /// AddPropertyAttrs - Propagates attributes from a property to the
02106 /// implicitly-declared getter or setter for that property.
02107 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
02108                              ObjCPropertyDecl *Property) {
02109   // Should we just clone all attributes over?
02110   for (const auto *A : Property->attrs()) {
02111     if (isa<DeprecatedAttr>(A) || 
02112         isa<UnavailableAttr>(A) || 
02113         isa<AvailabilityAttr>(A))
02114       PropertyMethod->addAttr(A->clone(S.Context));
02115   }
02116 }
02117 
02118 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
02119 /// have the property type and issue diagnostics if they don't.
02120 /// Also synthesize a getter/setter method if none exist (and update the
02121 /// appropriate lookup tables.
02122 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
02123   ObjCMethodDecl *GetterMethod, *SetterMethod;
02124   ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
02125   if (CD->isInvalidDecl())
02126     return;
02127 
02128   bool IsClassProperty = property->isClassProperty();
02129   GetterMethod = IsClassProperty ?
02130     CD->getClassMethod(property->getGetterName()) :
02131     CD->getInstanceMethod(property->getGetterName());
02132 
02133   // if setter or getter is not found in class extension, it might be
02134   // in the primary class.
02135   if (!GetterMethod)
02136     if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
02137       if (CatDecl->IsClassExtension())
02138         GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
02139                          getClassMethod(property->getGetterName()) :
02140                        CatDecl->getClassInterface()->
02141                          getInstanceMethod(property->getGetterName());
02142         
02143   SetterMethod = IsClassProperty ?
02144                  CD->getClassMethod(property->getSetterName()) :
02145                  CD->getInstanceMethod(property->getSetterName());
02146   if (!SetterMethod)
02147     if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
02148       if (CatDecl->IsClassExtension())
02149         SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
02150                           getClassMethod(property->getSetterName()) :
02151                        CatDecl->getClassInterface()->
02152                           getInstanceMethod(property->getSetterName());
02153   DiagnosePropertyAccessorMismatch(property, GetterMethod,
02154                                    property->getLocation());
02155 
02156   if (SetterMethod) {
02157     ObjCPropertyDecl::PropertyAttributeKind CAttr =
02158       property->getPropertyAttributes();
02159     if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
02160         Context.getCanonicalType(SetterMethod->getReturnType()) !=
02161             Context.VoidTy)
02162       Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
02163     if (SetterMethod->param_size() != 1 ||
02164         !Context.hasSameUnqualifiedType(
02165           (*SetterMethod->param_begin())->getType().getNonReferenceType(), 
02166           property->getType().getNonReferenceType())) {
02167       Diag(property->getLocation(),
02168            diag::warn_accessor_property_type_mismatch)
02169         << property->getDeclName()
02170         << SetterMethod->getSelector();
02171       Diag(SetterMethod->getLocation(), diag::note_declared_at);
02172     }
02173   }
02174 
02175   // Synthesize getter/setter methods if none exist.
02176   // Find the default getter and if one not found, add one.
02177   // FIXME: The synthesized property we set here is misleading. We almost always
02178   // synthesize these methods unless the user explicitly provided prototypes
02179   // (which is odd, but allowed). Sema should be typechecking that the
02180   // declarations jive in that situation (which it is not currently).
02181   if (!GetterMethod) {
02182     // No instance/class method of same name as property getter name was found.
02183     // Declare a getter method and add it to the list of methods
02184     // for this class.
02185     SourceLocation Loc = property->getLocation();
02186 
02187     // If the property is null_resettable, the getter returns nonnull.
02188     QualType resultTy = property->getType();
02189     if (property->getPropertyAttributes() &
02190         ObjCPropertyDecl::OBJC_PR_null_resettable) {
02191       QualType modifiedTy = resultTy;
02192       if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
02193         if (*nullability == NullabilityKind::Unspecified)
02194           resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
02195                                                modifiedTy, modifiedTy);
02196       }
02197     }
02198 
02199     GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
02200                              property->getGetterName(),
02201                              resultTy, nullptr, CD,
02202                              !IsClassProperty, /*isVariadic=*/false,
02203                              /*isPropertyAccessor=*/true,
02204                              /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
02205                              (property->getPropertyImplementation() ==
02206                               ObjCPropertyDecl::Optional) ?
02207                              ObjCMethodDecl::Optional :
02208                              ObjCMethodDecl::Required);
02209     CD->addDecl(GetterMethod);
02210 
02211     AddPropertyAttrs(*this, GetterMethod, property);
02212 
02213     if (property->hasAttr<NSReturnsNotRetainedAttr>())
02214       GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
02215                                                                      Loc));
02216     
02217     if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
02218       GetterMethod->addAttr(
02219         ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
02220     
02221     if (const SectionAttr *SA = property->getAttr<SectionAttr>())
02222       GetterMethod->addAttr(
02223           SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
02224                                       SA->getName(), Loc));
02225 
02226     if (getLangOpts().ObjCAutoRefCount)
02227       CheckARCMethodDecl(GetterMethod);
02228   } else
02229     // A user declared getter will be synthesize when @synthesize of
02230     // the property with the same name is seen in the @implementation
02231     GetterMethod->setPropertyAccessor(true);
02232   property->setGetterMethodDecl(GetterMethod);
02233 
02234   // Skip setter if property is read-only.
02235   if (!property->isReadOnly()) {
02236     // Find the default setter and if one not found, add one.
02237     if (!SetterMethod) {
02238       // No instance/class method of same name as property setter name was
02239       // found.
02240       // Declare a setter method and add it to the list of methods
02241       // for this class.
02242       SourceLocation Loc = property->getLocation();
02243 
02244       SetterMethod =
02245         ObjCMethodDecl::Create(Context, Loc, Loc,
02246                                property->getSetterName(), Context.VoidTy,
02247                                nullptr, CD, !IsClassProperty,
02248                                /*isVariadic=*/false,
02249                                /*isPropertyAccessor=*/true,
02250                                /*isImplicitlyDeclared=*/true,
02251                                /*isDefined=*/false,
02252                                (property->getPropertyImplementation() ==
02253                                 ObjCPropertyDecl::Optional) ?
02254                                 ObjCMethodDecl::Optional :
02255                                 ObjCMethodDecl::Required);
02256 
02257       // If the property is null_resettable, the setter accepts a
02258       // nullable value.
02259       QualType paramTy = property->getType().getUnqualifiedType();
02260       if (property->getPropertyAttributes() &
02261           ObjCPropertyDecl::OBJC_PR_null_resettable) {
02262         QualType modifiedTy = paramTy;
02263         if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
02264           if (*nullability == NullabilityKind::Unspecified)
02265             paramTy = Context.getAttributedType(AttributedType::attr_nullable,
02266                                                 modifiedTy, modifiedTy);
02267         }
02268       }
02269 
02270       // Invent the arguments for the setter. We don't bother making a
02271       // nice name for the argument.
02272       ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
02273                                                   Loc, Loc,
02274                                                   property->getIdentifier(),
02275                                                   paramTy,
02276                                                   /*TInfo=*/nullptr,
02277                                                   SC_None,
02278                                                   nullptr);
02279       SetterMethod->setMethodParams(Context, Argument, None);
02280 
02281       AddPropertyAttrs(*this, SetterMethod, property);
02282 
02283       CD->addDecl(SetterMethod);
02284       if (const SectionAttr *SA = property->getAttr<SectionAttr>())
02285         SetterMethod->addAttr(
02286             SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
02287                                         SA->getName(), Loc));
02288       // It's possible for the user to have set a very odd custom
02289       // setter selector that causes it to have a method family.
02290       if (getLangOpts().ObjCAutoRefCount)
02291         CheckARCMethodDecl(SetterMethod);
02292     } else
02293       // A user declared setter will be synthesize when @synthesize of
02294       // the property with the same name is seen in the @implementation
02295       SetterMethod->setPropertyAccessor(true);
02296     property->setSetterMethodDecl(SetterMethod);
02297   }
02298   // Add any synthesized methods to the global pool. This allows us to
02299   // handle the following, which is supported by GCC (and part of the design).
02300   //
02301   // @interface Foo
02302   // @property double bar;
02303   // @end
02304   //
02305   // void thisIsUnfortunate() {
02306   //   id foo;
02307   //   double bar = [foo bar];
02308   // }
02309   //
02310   if (!IsClassProperty) {
02311     if (GetterMethod)
02312       AddInstanceMethodToGlobalPool(GetterMethod);
02313     if (SetterMethod)
02314       AddInstanceMethodToGlobalPool(SetterMethod);
02315   }
02316 
02317   ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
02318   if (!CurrentClass) {
02319     if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
02320       CurrentClass = Cat->getClassInterface();
02321     else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
02322       CurrentClass = Impl->getClassInterface();
02323   }
02324   if (GetterMethod)
02325     CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
02326   if (SetterMethod)
02327     CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
02328 }
02329 
02330 void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
02331                                        SourceLocation Loc,
02332                                        unsigned &Attributes,
02333                                        bool propertyInPrimaryClass) {
02334   // FIXME: Improve the reported location.
02335   if (!PDecl || PDecl->isInvalidDecl())
02336     return;
02337   
02338   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
02339       (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
02340     Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02341     << "readonly" << "readwrite";
02342   
02343   ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
02344   QualType PropertyTy = PropertyDecl->getType();
02345 
02346   // Check for copy or retain on non-object types.
02347   if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
02348                     ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
02349       !PropertyTy->isObjCRetainableType() &&
02350       !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
02351     Diag(Loc, diag::err_objc_property_requires_object)
02352       << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
02353           Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
02354     Attributes &= ~(ObjCDeclSpec::DQ_PR_weak   | ObjCDeclSpec::DQ_PR_copy |
02355                     ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
02356     PropertyDecl->setInvalidDecl();
02357   }
02358 
02359   // Check for more than one of { assign, copy, retain }.
02360   if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
02361     if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
02362       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02363         << "assign" << "copy";
02364       Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
02365     }
02366     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
02367       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02368         << "assign" << "retain";
02369       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
02370     }
02371     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
02372       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02373         << "assign" << "strong";
02374       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
02375     }
02376     if (getLangOpts().ObjCAutoRefCount  &&
02377         (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
02378       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02379         << "assign" << "weak";
02380       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
02381     }
02382     if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
02383       Diag(Loc, diag::warn_iboutletcollection_property_assign);
02384   } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
02385     if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
02386       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02387         << "unsafe_unretained" << "copy";
02388       Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
02389     }
02390     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
02391       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02392         << "unsafe_unretained" << "retain";
02393       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
02394     }
02395     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
02396       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02397         << "unsafe_unretained" << "strong";
02398       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
02399     }
02400     if (getLangOpts().ObjCAutoRefCount  &&
02401         (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
02402       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02403         << "unsafe_unretained" << "weak";
02404       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
02405     }
02406   } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
02407     if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
02408       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02409         << "copy" << "retain";
02410       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
02411     }
02412     if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
02413       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02414         << "copy" << "strong";
02415       Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
02416     }
02417     if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
02418       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02419         << "copy" << "weak";
02420       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
02421     }
02422   }
02423   else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
02424            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
02425       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02426         << "retain" << "weak";
02427       Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
02428   }
02429   else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
02430            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
02431       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02432         << "strong" << "weak";
02433       Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
02434   }
02435 
02436   if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
02437     // 'weak' and 'nonnull' are mutually exclusive.
02438     if (auto nullability = PropertyTy->getNullability(Context)) {
02439       if (*nullability == NullabilityKind::NonNull)
02440         Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02441           << "nonnull" << "weak";
02442     }
02443   }
02444 
02445   if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
02446       (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
02447       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
02448         << "atomic" << "nonatomic";
02449       Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
02450   }
02451 
02452   // Warn if user supplied no assignment attribute, property is
02453   // readwrite, and this is an object type.
02454   if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
02455     if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
02456       // do nothing
02457     } else if (getLangOpts().ObjCAutoRefCount) {
02458       // With arc, @property definitions should default to strong when 
02459       // not specified.
02460       PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
02461     } else if (PropertyTy->isObjCObjectPointerType()) {
02462         bool isAnyClassTy = 
02463           (PropertyTy->isObjCClassType() || 
02464            PropertyTy->isObjCQualifiedClassType());
02465         // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
02466         // issue any warning.
02467         if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
02468           ;
02469         else if (propertyInPrimaryClass) {
02470           // Don't issue warning on property with no life time in class 
02471           // extension as it is inherited from property in primary class.
02472           // Skip this warning in gc-only mode.
02473           if (getLangOpts().getGC() != LangOptions::GCOnly)
02474             Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
02475 
02476           // If non-gc code warn that this is likely inappropriate.
02477           if (getLangOpts().getGC() == LangOptions::NonGC)
02478             Diag(Loc, diag::warn_objc_property_default_assign_on_object);
02479         }
02480     }
02481 
02482     // FIXME: Implement warning dependent on NSCopying being
02483     // implemented. See also:
02484     // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
02485     // (please trim this list while you are at it).
02486   }
02487 
02488   if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
02489       &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
02490       && getLangOpts().getGC() == LangOptions::GCOnly
02491       && PropertyTy->isBlockPointerType())
02492     Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
02493   else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
02494            !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
02495            !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
02496            PropertyTy->isBlockPointerType())
02497       Diag(Loc, diag::warn_objc_property_retain_of_block);
02498   
02499   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
02500       (Attributes & ObjCDeclSpec::DQ_PR_setter))
02501     Diag(Loc, diag::warn_objc_readonly_property_has_setter);
02502       
02503 }