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