clang API Documentation

DeclObjC.cpp
Go to the documentation of this file.
00001 //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
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 the Objective-C related Decl classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/AST/DeclObjC.h"
00015 #include "clang/AST/ASTContext.h"
00016 #include "clang/AST/ASTMutationListener.h"
00017 #include "clang/AST/Attr.h"
00018 #include "clang/AST/Stmt.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/SmallString.h"
00021 using namespace clang;
00022 
00023 //===----------------------------------------------------------------------===//
00024 // ObjCListBase
00025 //===----------------------------------------------------------------------===//
00026 
00027 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
00028   List = nullptr;
00029   if (Elts == 0) return;  // Setting to an empty list is a noop.
00030 
00031 
00032   List = new (Ctx) void*[Elts];
00033   NumElts = Elts;
00034   memcpy(List, InList, sizeof(void*)*Elts);
00035 }
00036 
00037 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, 
00038                            const SourceLocation *Locs, ASTContext &Ctx) {
00039   if (Elts == 0)
00040     return;
00041 
00042   Locations = new (Ctx) SourceLocation[Elts];
00043   memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
00044   set(InList, Elts, Ctx);
00045 }
00046 
00047 //===----------------------------------------------------------------------===//
00048 // ObjCInterfaceDecl
00049 //===----------------------------------------------------------------------===//
00050 
00051 void ObjCContainerDecl::anchor() { }
00052 
00053 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
00054 ///
00055 ObjCIvarDecl *
00056 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
00057   lookup_const_result R = lookup(Id);
00058   for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
00059        Ivar != IvarEnd; ++Ivar) {
00060     if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
00061       return ivar;
00062   }
00063   return nullptr;
00064 }
00065 
00066 // Get the local instance/class method declared in this interface.
00067 ObjCMethodDecl *
00068 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
00069                              bool AllowHidden) const {
00070   // If this context is a hidden protocol definition, don't find any
00071   // methods there.
00072   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
00073     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
00074       if (Def->isHidden() && !AllowHidden)
00075         return nullptr;
00076   }
00077 
00078   // Since instance & class methods can have the same name, the loop below
00079   // ensures we get the correct method.
00080   //
00081   // @interface Whatever
00082   // - (int) class_method;
00083   // + (float) class_method;
00084   // @end
00085   //
00086   lookup_const_result R = lookup(Sel);
00087   for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
00088        Meth != MethEnd; ++Meth) {
00089     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
00090     if (MD && MD->isInstanceMethod() == isInstance)
00091       return MD;
00092   }
00093   return nullptr;
00094 }
00095 
00096 /// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
00097 /// method was found in the class, its protocols, its super classes or categories.
00098 /// It also returns 'true' if one of its categories has declared a 'readwrite' property.
00099 /// This is because, user must provide a setter method for the category's 'readwrite'
00100 /// property.
00101 bool
00102 ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
00103   Selector Sel = Property->getSetterName();
00104   lookup_const_result R = lookup(Sel);
00105   for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
00106        Meth != MethEnd; ++Meth) {
00107     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
00108     if (MD && MD->isInstanceMethod() && !MD->isImplicit())
00109       return true;
00110   }
00111 
00112   if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
00113     // Also look into categories, including class extensions, looking
00114     // for a user declared instance method.
00115     for (const auto *Cat : ID->visible_categories()) {
00116       if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
00117         if (!MD->isImplicit())
00118           return true;
00119       if (Cat->IsClassExtension())
00120         continue;
00121       // Also search through the categories looking for a 'readwrite' declaration
00122       // of this property. If one found, presumably a setter will be provided
00123       // (properties declared in categories will not get auto-synthesized).
00124       for (const auto *P : Cat->properties())
00125         if (P->getIdentifier() == Property->getIdentifier()) {
00126           if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
00127             return true;
00128           break;
00129         }
00130     }
00131     
00132     // Also look into protocols, for a user declared instance method.
00133     for (const auto *Proto : ID->all_referenced_protocols())
00134       if (Proto->HasUserDeclaredSetterMethod(Property))
00135         return true;
00136 
00137     // And in its super class.
00138     ObjCInterfaceDecl *OSC = ID->getSuperClass();
00139     while (OSC) {
00140       if (OSC->HasUserDeclaredSetterMethod(Property))
00141         return true;
00142       OSC = OSC->getSuperClass();
00143     }
00144   }
00145   if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
00146     for (const auto *PI : PD->protocols())
00147       if (PI->HasUserDeclaredSetterMethod(Property))
00148         return true;
00149   return false;
00150 }
00151 
00152 ObjCPropertyDecl *
00153 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
00154                                    IdentifierInfo *propertyID) {
00155   // If this context is a hidden protocol definition, don't find any
00156   // property.
00157   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
00158     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
00159       if (Def->isHidden())
00160         return nullptr;
00161   }
00162 
00163   DeclContext::lookup_const_result R = DC->lookup(propertyID);
00164   for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
00165        ++I)
00166     if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
00167       return PD;
00168 
00169   return nullptr;
00170 }
00171 
00172 IdentifierInfo *
00173 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
00174   SmallString<128> ivarName;
00175   {
00176     llvm::raw_svector_ostream os(ivarName);
00177     os << '_' << getIdentifier()->getName();
00178   }
00179   return &Ctx.Idents.get(ivarName.str());
00180 }
00181 
00182 /// FindPropertyDeclaration - Finds declaration of the property given its name
00183 /// in 'PropertyId' and returns it. It returns 0, if not found.
00184 ObjCPropertyDecl *
00185 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
00186   // Don't find properties within hidden protocol definitions.
00187   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
00188     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
00189       if (Def->isHidden())
00190         return nullptr;
00191   }
00192 
00193   if (ObjCPropertyDecl *PD =
00194         ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
00195     return PD;
00196 
00197   switch (getKind()) {
00198     default:
00199       break;
00200     case Decl::ObjCProtocol: {
00201       const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
00202       for (const auto *I : PID->protocols())
00203         if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
00204           return P;
00205       break;
00206     }
00207     case Decl::ObjCInterface: {
00208       const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
00209       // Look through categories (but not extensions).
00210       for (const auto *Cat : OID->visible_categories()) {
00211         if (!Cat->IsClassExtension())
00212           if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
00213             return P;
00214       }
00215 
00216       // Look through protocols.
00217       for (const auto *I : OID->all_referenced_protocols())
00218         if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
00219           return P;
00220 
00221       // Finally, check the super class.
00222       if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
00223         return superClass->FindPropertyDeclaration(PropertyId);
00224       break;
00225     }
00226     case Decl::ObjCCategory: {
00227       const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
00228       // Look through protocols.
00229       if (!OCD->IsClassExtension())
00230         for (const auto *I : OCD->protocols())
00231           if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
00232             return P;
00233       break;
00234     }
00235   }
00236   return nullptr;
00237 }
00238 
00239 void ObjCInterfaceDecl::anchor() { }
00240 
00241 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
00242 /// with name 'PropertyId' in the primary class; including those in protocols
00243 /// (direct or indirect) used by the primary class.
00244 ///
00245 ObjCPropertyDecl *
00246 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
00247                                             IdentifierInfo *PropertyId) const {
00248   // FIXME: Should make sure no callers ever do this.
00249   if (!hasDefinition())
00250     return nullptr;
00251 
00252   if (data().ExternallyCompleted)
00253     LoadExternalDefinition();
00254 
00255   if (ObjCPropertyDecl *PD =
00256       ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
00257     return PD;
00258 
00259   // Look through protocols.
00260   for (const auto *I : all_referenced_protocols())
00261     if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId))
00262       return P;
00263 
00264   return nullptr;
00265 }
00266 
00267 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
00268                                                      PropertyDeclOrder &PO) const {
00269   for (auto *Prop : properties()) {
00270     PM[Prop->getIdentifier()] = Prop;
00271     PO.push_back(Prop);
00272   }
00273   for (const auto *PI : all_referenced_protocols())
00274     PI->collectPropertiesToImplement(PM, PO);
00275   // Note, the properties declared only in class extensions are still copied
00276   // into the main @interface's property list, and therefore we don't
00277   // explicitly, have to search class extension properties.
00278 }
00279 
00280 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
00281   const ObjCInterfaceDecl *Class = this;
00282   while (Class) {
00283     if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
00284       return true;
00285     Class = Class->getSuperClass();
00286   }
00287   return false;
00288 }
00289 
00290 const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
00291   const ObjCInterfaceDecl *Class = this;
00292   while (Class) {
00293     if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
00294       return Class;
00295     Class = Class->getSuperClass();
00296   }
00297   return nullptr;
00298 }
00299 
00300 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
00301                               ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
00302                               ASTContext &C)
00303 {
00304   if (data().ExternallyCompleted)
00305     LoadExternalDefinition();
00306 
00307   if (data().AllReferencedProtocols.empty() && 
00308       data().ReferencedProtocols.empty()) {
00309     data().AllReferencedProtocols.set(ExtList, ExtNum, C);
00310     return;
00311   }
00312   
00313   // Check for duplicate protocol in class's protocol list.
00314   // This is O(n*m). But it is extremely rare and number of protocols in
00315   // class or its extension are very few.
00316   SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
00317   for (unsigned i = 0; i < ExtNum; i++) {
00318     bool protocolExists = false;
00319     ObjCProtocolDecl *ProtoInExtension = ExtList[i];
00320     for (auto *Proto : all_referenced_protocols()) {
00321       if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
00322         protocolExists = true;
00323         break;
00324       }      
00325     }
00326     // Do we want to warn on a protocol in extension class which
00327     // already exist in the class? Probably not.
00328     if (!protocolExists)
00329       ProtocolRefs.push_back(ProtoInExtension);
00330   }
00331 
00332   if (ProtocolRefs.empty())
00333     return;
00334 
00335   // Merge ProtocolRefs into class's protocol list;
00336   for (auto *P : all_referenced_protocols()) {
00337     ProtocolRefs.push_back(P);
00338   }
00339 
00340   data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
00341 }
00342 
00343 const ObjCInterfaceDecl *
00344 ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
00345   const ObjCInterfaceDecl *IFace = this;
00346   while (IFace) {
00347     if (IFace->hasDesignatedInitializers())
00348       return IFace;
00349     if (!IFace->inheritsDesignatedInitializers())
00350       break;
00351     IFace = IFace->getSuperClass();
00352   }
00353   return nullptr;
00354 }
00355 
00356 static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
00357   for (const auto *MD : D->instance_methods()) {
00358     if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
00359       return true;
00360   }
00361   for (const auto *Ext : D->visible_extensions()) {
00362     for (const auto *MD : Ext->instance_methods()) {
00363       if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
00364         return true;
00365     }
00366   }
00367   if (const auto *ImplD = D->getImplementation()) {
00368     for (const auto *MD : ImplD->instance_methods()) {
00369       if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
00370         return true;
00371     }
00372   }
00373   return false;
00374 }
00375 
00376 bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
00377   switch (data().InheritedDesignatedInitializers) {
00378   case DefinitionData::IDI_Inherited:
00379     return true;
00380   case DefinitionData::IDI_NotInherited:
00381     return false;
00382   case DefinitionData::IDI_Unknown: {
00383     // If the class introduced initializers we conservatively assume that we
00384     // don't know if any of them is a designated initializer to avoid possible
00385     // misleading warnings.
00386     if (isIntroducingInitializers(this)) {
00387       data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
00388     } else {
00389       if (auto SuperD = getSuperClass()) {
00390         data().InheritedDesignatedInitializers =
00391           SuperD->declaresOrInheritsDesignatedInitializers() ?
00392             DefinitionData::IDI_Inherited :
00393             DefinitionData::IDI_NotInherited;
00394       } else {
00395         data().InheritedDesignatedInitializers =
00396           DefinitionData::IDI_NotInherited;
00397       }
00398     }
00399     assert(data().InheritedDesignatedInitializers
00400              != DefinitionData::IDI_Unknown);
00401     return data().InheritedDesignatedInitializers ==
00402         DefinitionData::IDI_Inherited;
00403   }
00404   }
00405 
00406   llvm_unreachable("unexpected InheritedDesignatedInitializers value");
00407 }
00408 
00409 void ObjCInterfaceDecl::getDesignatedInitializers(
00410     llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
00411   // Check for a complete definition and recover if not so.
00412   if (!isThisDeclarationADefinition())
00413     return;
00414   if (data().ExternallyCompleted)
00415     LoadExternalDefinition();
00416 
00417   const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
00418   if (!IFace)
00419     return;
00420 
00421   for (const auto *MD : IFace->instance_methods())
00422     if (MD->isThisDeclarationADesignatedInitializer())
00423       Methods.push_back(MD);
00424   for (const auto *Ext : IFace->visible_extensions()) {
00425     for (const auto *MD : Ext->instance_methods())
00426       if (MD->isThisDeclarationADesignatedInitializer())
00427         Methods.push_back(MD);
00428   }
00429 }
00430 
00431 bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
00432                                       const ObjCMethodDecl **InitMethod) const {
00433   // Check for a complete definition and recover if not so.
00434   if (!isThisDeclarationADefinition())
00435     return false;
00436   if (data().ExternallyCompleted)
00437     LoadExternalDefinition();
00438 
00439   const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
00440   if (!IFace)
00441     return false;
00442 
00443   if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
00444     if (MD->isThisDeclarationADesignatedInitializer()) {
00445       if (InitMethod)
00446         *InitMethod = MD;
00447       return true;
00448     }
00449   }
00450   for (const auto *Ext : IFace->visible_extensions()) {
00451     if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
00452       if (MD->isThisDeclarationADesignatedInitializer()) {
00453         if (InitMethod)
00454           *InitMethod = MD;
00455         return true;
00456       }
00457     }
00458   }
00459   return false;
00460 }
00461 
00462 void ObjCInterfaceDecl::allocateDefinitionData() {
00463   assert(!hasDefinition() && "ObjC class already has a definition");
00464   Data.setPointer(new (getASTContext()) DefinitionData());
00465   Data.getPointer()->Definition = this;
00466 
00467   // Make the type point at the definition, now that we have one.
00468   if (TypeForDecl)
00469     cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
00470 }
00471 
00472 void ObjCInterfaceDecl::startDefinition() {
00473   allocateDefinitionData();
00474 
00475   // Update all of the declarations with a pointer to the definition.
00476   for (auto RD : redecls()) {
00477     if (RD != this)
00478       RD->Data = Data;
00479   }
00480 }
00481 
00482 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
00483                                               ObjCInterfaceDecl *&clsDeclared) {
00484   // FIXME: Should make sure no callers ever do this.
00485   if (!hasDefinition())
00486     return nullptr;
00487 
00488   if (data().ExternallyCompleted)
00489     LoadExternalDefinition();
00490 
00491   ObjCInterfaceDecl* ClassDecl = this;
00492   while (ClassDecl != nullptr) {
00493     if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
00494       clsDeclared = ClassDecl;
00495       return I;
00496     }
00497 
00498     for (const auto *Ext : ClassDecl->visible_extensions()) {
00499       if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
00500         clsDeclared = ClassDecl;
00501         return I;
00502       }
00503     }
00504       
00505     ClassDecl = ClassDecl->getSuperClass();
00506   }
00507   return nullptr;
00508 }
00509 
00510 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
00511 /// class whose name is passed as argument. If it is not one of the super classes
00512 /// the it returns NULL.
00513 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
00514                                         const IdentifierInfo*ICName) {
00515   // FIXME: Should make sure no callers ever do this.
00516   if (!hasDefinition())
00517     return nullptr;
00518 
00519   if (data().ExternallyCompleted)
00520     LoadExternalDefinition();
00521 
00522   ObjCInterfaceDecl* ClassDecl = this;
00523   while (ClassDecl != nullptr) {
00524     if (ClassDecl->getIdentifier() == ICName)
00525       return ClassDecl;
00526     ClassDecl = ClassDecl->getSuperClass();
00527   }
00528   return nullptr;
00529 }
00530 
00531 ObjCProtocolDecl *
00532 ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
00533   for (auto *P : all_referenced_protocols())
00534     if (P->lookupProtocolNamed(Name))
00535       return P;
00536   ObjCInterfaceDecl *SuperClass = getSuperClass();
00537   return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
00538 }
00539 
00540 /// lookupMethod - This method returns an instance/class method by looking in
00541 /// the class, its categories, and its super classes (using a linear search).
00542 /// When argument category "C" is specified, any implicit method found
00543 /// in this category is ignored.
00544 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, 
00545                                                 bool isInstance,
00546                                                 bool shallowCategoryLookup,
00547                                                 bool followSuper,
00548                                                 const ObjCCategoryDecl *C) const
00549 {
00550   // FIXME: Should make sure no callers ever do this.
00551   if (!hasDefinition())
00552     return nullptr;
00553 
00554   const ObjCInterfaceDecl* ClassDecl = this;
00555   ObjCMethodDecl *MethodDecl = nullptr;
00556 
00557   if (data().ExternallyCompleted)
00558     LoadExternalDefinition();
00559 
00560   while (ClassDecl) {
00561     // 1. Look through primary class.
00562     if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
00563       return MethodDecl;
00564     
00565     // 2. Didn't find one yet - now look through categories.
00566     for (const auto *Cat : ClassDecl->visible_categories())
00567       if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
00568         if (C != Cat || !MethodDecl->isImplicit())
00569           return MethodDecl;
00570 
00571     // 3. Didn't find one yet - look through primary class's protocols.
00572     for (const auto *I : ClassDecl->protocols())
00573       if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
00574         return MethodDecl;
00575     
00576     // 4. Didn't find one yet - now look through categories' protocols
00577     if (!shallowCategoryLookup)
00578       for (const auto *Cat : ClassDecl->visible_categories()) {
00579         // Didn't find one yet - look through protocols.
00580         const ObjCList<ObjCProtocolDecl> &Protocols =
00581           Cat->getReferencedProtocols();
00582         for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
00583              E = Protocols.end(); I != E; ++I)
00584           if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
00585             if (C != Cat || !MethodDecl->isImplicit())
00586               return MethodDecl;
00587       }
00588     
00589     
00590     if (!followSuper)
00591       return nullptr;
00592 
00593     // 5. Get to the super class (if any).
00594     ClassDecl = ClassDecl->getSuperClass();
00595   }
00596   return nullptr;
00597 }
00598 
00599 // Will search "local" class/category implementations for a method decl.
00600 // If failed, then we search in class's root for an instance method.
00601 // Returns 0 if no method is found.
00602 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
00603                                    const Selector &Sel,
00604                                    bool Instance) const {
00605   // FIXME: Should make sure no callers ever do this.
00606   if (!hasDefinition())
00607     return nullptr;
00608 
00609   if (data().ExternallyCompleted)
00610     LoadExternalDefinition();
00611 
00612   ObjCMethodDecl *Method = nullptr;
00613   if (ObjCImplementationDecl *ImpDecl = getImplementation())
00614     Method = Instance ? ImpDecl->getInstanceMethod(Sel) 
00615                       : ImpDecl->getClassMethod(Sel);
00616 
00617   // Look through local category implementations associated with the class.
00618   if (!Method)
00619     Method = Instance ? getCategoryInstanceMethod(Sel)
00620                       : getCategoryClassMethod(Sel);
00621 
00622   // Before we give up, check if the selector is an instance method.
00623   // But only in the root. This matches gcc's behavior and what the
00624   // runtime expects.
00625   if (!Instance && !Method && !getSuperClass()) {
00626     Method = lookupInstanceMethod(Sel);
00627     // Look through local category implementations associated
00628     // with the root class.
00629     if (!Method)
00630       Method = lookupPrivateMethod(Sel, true);
00631   }
00632 
00633   if (!Method && getSuperClass())
00634     return getSuperClass()->lookupPrivateMethod(Sel, Instance);
00635   return Method;
00636 }
00637 
00638 //===----------------------------------------------------------------------===//
00639 // ObjCMethodDecl
00640 //===----------------------------------------------------------------------===//
00641 
00642 ObjCMethodDecl *ObjCMethodDecl::Create(
00643     ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
00644     Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
00645     DeclContext *contextDecl, bool isInstance, bool isVariadic,
00646     bool isPropertyAccessor, bool isImplicitlyDeclared, bool isDefined,
00647     ImplementationControl impControl, bool HasRelatedResultType) {
00648   return new (C, contextDecl) ObjCMethodDecl(
00649       beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
00650       isVariadic, isPropertyAccessor, isImplicitlyDeclared, isDefined,
00651       impControl, HasRelatedResultType);
00652 }
00653 
00654 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
00655   return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
00656                                     Selector(), QualType(), nullptr, nullptr);
00657 }
00658 
00659 bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
00660   return getMethodFamily() == OMF_init &&
00661       hasAttr<ObjCDesignatedInitializerAttr>();
00662 }
00663 
00664 bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
00665     const ObjCMethodDecl **InitMethod) const {
00666   if (getMethodFamily() != OMF_init)
00667     return false;
00668   const DeclContext *DC = getDeclContext();
00669   if (isa<ObjCProtocolDecl>(DC))
00670     return false;
00671   if (const ObjCInterfaceDecl *ID = getClassInterface())
00672     return ID->isDesignatedInitializer(getSelector(), InitMethod);
00673   return false;
00674 }
00675 
00676 Stmt *ObjCMethodDecl::getBody() const {
00677   return Body.get(getASTContext().getExternalSource());
00678 }
00679 
00680 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
00681   assert(PrevMethod);
00682   getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
00683   IsRedeclaration = true;
00684   PrevMethod->HasRedeclaration = true;
00685 }
00686 
00687 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
00688                                          ArrayRef<ParmVarDecl*> Params,
00689                                          ArrayRef<SourceLocation> SelLocs) {
00690   ParamsAndSelLocs = nullptr;
00691   NumParams = Params.size();
00692   if (Params.empty() && SelLocs.empty())
00693     return;
00694 
00695   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
00696                   sizeof(SourceLocation) * SelLocs.size();
00697   ParamsAndSelLocs = C.Allocate(Size);
00698   std::copy(Params.begin(), Params.end(), getParams());
00699   std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
00700 }
00701 
00702 void ObjCMethodDecl::getSelectorLocs(
00703                                SmallVectorImpl<SourceLocation> &SelLocs) const {
00704   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
00705     SelLocs.push_back(getSelectorLoc(i));
00706 }
00707 
00708 void ObjCMethodDecl::setMethodParams(ASTContext &C,
00709                                      ArrayRef<ParmVarDecl*> Params,
00710                                      ArrayRef<SourceLocation> SelLocs) {
00711   assert((!SelLocs.empty() || isImplicit()) &&
00712          "No selector locs for non-implicit method");
00713   if (isImplicit())
00714     return setParamsAndSelLocs(C, Params, llvm::None);
00715 
00716   SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
00717                                         DeclEndLoc);
00718   if (SelLocsKind != SelLoc_NonStandard)
00719     return setParamsAndSelLocs(C, Params, llvm::None);
00720 
00721   setParamsAndSelLocs(C, Params, SelLocs);
00722 }
00723 
00724 /// \brief A definition will return its interface declaration.
00725 /// An interface declaration will return its definition.
00726 /// Otherwise it will return itself.
00727 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
00728   ASTContext &Ctx = getASTContext();
00729   ObjCMethodDecl *Redecl = nullptr;
00730   if (HasRedeclaration)
00731     Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
00732   if (Redecl)
00733     return Redecl;
00734 
00735   Decl *CtxD = cast<Decl>(getDeclContext());
00736 
00737   if (!CtxD->isInvalidDecl()) {
00738     if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
00739       if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
00740         if (!ImplD->isInvalidDecl())
00741           Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
00742 
00743     } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
00744       if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
00745         if (!ImplD->isInvalidDecl())
00746           Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
00747 
00748     } else if (ObjCImplementationDecl *ImplD =
00749                  dyn_cast<ObjCImplementationDecl>(CtxD)) {
00750       if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
00751         if (!IFD->isInvalidDecl())
00752           Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
00753 
00754     } else if (ObjCCategoryImplDecl *CImplD =
00755                  dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
00756       if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
00757         if (!CatD->isInvalidDecl())
00758           Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
00759     }
00760   }
00761 
00762   if (!Redecl && isRedeclaration()) {
00763     // This is the last redeclaration, go back to the first method.
00764     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
00765                                                     isInstanceMethod());
00766   }
00767 
00768   return Redecl ? Redecl : this;
00769 }
00770 
00771 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
00772   Decl *CtxD = cast<Decl>(getDeclContext());
00773 
00774   if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
00775     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
00776       if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
00777                                               isInstanceMethod()))
00778         return MD;
00779 
00780   } else if (ObjCCategoryImplDecl *CImplD =
00781                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
00782     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
00783       if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
00784                                                isInstanceMethod()))
00785         return MD;
00786   }
00787 
00788   if (isRedeclaration())
00789     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
00790                                                     isInstanceMethod());
00791 
00792   return this;
00793 }
00794 
00795 SourceLocation ObjCMethodDecl::getLocEnd() const {
00796   if (Stmt *Body = getBody())
00797     return Body->getLocEnd();
00798   return DeclEndLoc;
00799 }
00800 
00801 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
00802   ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
00803   if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
00804     return family;
00805 
00806   // Check for an explicit attribute.
00807   if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
00808     // The unfortunate necessity of mapping between enums here is due
00809     // to the attributes framework.
00810     switch (attr->getFamily()) {
00811     case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
00812     case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
00813     case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
00814     case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
00815     case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
00816     case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
00817     }
00818     Family = static_cast<unsigned>(family);
00819     return family;
00820   }
00821 
00822   family = getSelector().getMethodFamily();
00823   switch (family) {
00824   case OMF_None: break;
00825 
00826   // init only has a conventional meaning for an instance method, and
00827   // it has to return an object.
00828   case OMF_init:
00829     if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
00830       family = OMF_None;
00831     break;
00832 
00833   // alloc/copy/new have a conventional meaning for both class and
00834   // instance methods, but they require an object return.
00835   case OMF_alloc:
00836   case OMF_copy:
00837   case OMF_mutableCopy:
00838   case OMF_new:
00839     if (!getReturnType()->isObjCObjectPointerType())
00840       family = OMF_None;
00841     break;
00842 
00843   // These selectors have a conventional meaning only for instance methods.
00844   case OMF_dealloc:
00845   case OMF_finalize:
00846   case OMF_retain:
00847   case OMF_release:
00848   case OMF_autorelease:
00849   case OMF_retainCount:
00850   case OMF_self:
00851     if (!isInstanceMethod())
00852       family = OMF_None;
00853     break;
00854       
00855   case OMF_initialize:
00856     if (isInstanceMethod() || !getReturnType()->isVoidType())
00857       family = OMF_None;
00858     break;
00859       
00860   case OMF_performSelector:
00861     if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
00862       family = OMF_None;
00863     else {
00864       unsigned noParams = param_size();
00865       if (noParams < 1 || noParams > 3)
00866         family = OMF_None;
00867       else {
00868         ObjCMethodDecl::param_type_iterator it = param_type_begin();
00869         QualType ArgT = (*it);
00870         if (!ArgT->isObjCSelType()) {
00871           family = OMF_None;
00872           break;
00873         }
00874         while (--noParams) {
00875           it++;
00876           ArgT = (*it);
00877           if (!ArgT->isObjCIdType()) {
00878             family = OMF_None;
00879             break;
00880           }
00881         }
00882       }
00883     }
00884     break;
00885       
00886   }
00887 
00888   // Cache the result.
00889   Family = static_cast<unsigned>(family);
00890   return family;
00891 }
00892 
00893 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
00894                                           const ObjCInterfaceDecl *OID) {
00895   QualType selfTy;
00896   if (isInstanceMethod()) {
00897     // There may be no interface context due to error in declaration
00898     // of the interface (which has been reported). Recover gracefully.
00899     if (OID) {
00900       selfTy = Context.getObjCInterfaceType(OID);
00901       selfTy = Context.getObjCObjectPointerType(selfTy);
00902     } else {
00903       selfTy = Context.getObjCIdType();
00904     }
00905   } else // we have a factory method.
00906     selfTy = Context.getObjCClassType();
00907 
00908   bool selfIsPseudoStrong = false;
00909   bool selfIsConsumed = false;
00910   
00911   if (Context.getLangOpts().ObjCAutoRefCount) {
00912     if (isInstanceMethod()) {
00913       selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
00914 
00915       // 'self' is always __strong.  It's actually pseudo-strong except
00916       // in init methods (or methods labeled ns_consumes_self), though.
00917       Qualifiers qs;
00918       qs.setObjCLifetime(Qualifiers::OCL_Strong);
00919       selfTy = Context.getQualifiedType(selfTy, qs);
00920 
00921       // In addition, 'self' is const unless this is an init method.
00922       if (getMethodFamily() != OMF_init && !selfIsConsumed) {
00923         selfTy = selfTy.withConst();
00924         selfIsPseudoStrong = true;
00925       }
00926     }
00927     else {
00928       assert(isClassMethod());
00929       // 'self' is always const in class methods.
00930       selfTy = selfTy.withConst();
00931       selfIsPseudoStrong = true;
00932     }
00933   }
00934 
00935   ImplicitParamDecl *self
00936     = ImplicitParamDecl::Create(Context, this, SourceLocation(),
00937                                 &Context.Idents.get("self"), selfTy);
00938   setSelfDecl(self);
00939 
00940   if (selfIsConsumed)
00941     self->addAttr(NSConsumedAttr::CreateImplicit(Context));
00942 
00943   if (selfIsPseudoStrong)
00944     self->setARCPseudoStrong(true);
00945 
00946   setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
00947                                        &Context.Idents.get("_cmd"),
00948                                        Context.getObjCSelType()));
00949 }
00950 
00951 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
00952   if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
00953     return ID;
00954   if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
00955     return CD->getClassInterface();
00956   if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
00957     return IMD->getClassInterface();
00958   if (isa<ObjCProtocolDecl>(getDeclContext()))
00959     return nullptr;
00960   llvm_unreachable("unknown method context");
00961 }
00962 
00963 SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
00964   const auto *TSI = getReturnTypeSourceInfo();
00965   if (TSI)
00966     return TSI->getTypeLoc().getSourceRange();
00967   return SourceRange();
00968 }
00969 
00970 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
00971                                             const ObjCMethodDecl *Method,
00972                                SmallVectorImpl<const ObjCMethodDecl *> &Methods,
00973                                             bool MovedToSuper) {
00974   if (!Container)
00975     return;
00976 
00977   // In categories look for overriden methods from protocols. A method from
00978   // category is not "overriden" since it is considered as the "same" method
00979   // (same USR) as the one from the interface.
00980   if (const ObjCCategoryDecl *
00981         Category = dyn_cast<ObjCCategoryDecl>(Container)) {
00982     // Check whether we have a matching method at this category but only if we
00983     // are at the super class level.
00984     if (MovedToSuper)
00985       if (ObjCMethodDecl *
00986             Overridden = Container->getMethod(Method->getSelector(),
00987                                               Method->isInstanceMethod(),
00988                                               /*AllowHidden=*/true))
00989         if (Method != Overridden) {
00990           // We found an override at this category; there is no need to look
00991           // into its protocols.
00992           Methods.push_back(Overridden);
00993           return;
00994         }
00995 
00996     for (const auto *P : Category->protocols())
00997       CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
00998     return;
00999   }
01000 
01001   // Check whether we have a matching method at this level.
01002   if (const ObjCMethodDecl *
01003         Overridden = Container->getMethod(Method->getSelector(),
01004                                           Method->isInstanceMethod(),
01005                                           /*AllowHidden=*/true))
01006     if (Method != Overridden) {
01007       // We found an override at this level; there is no need to look
01008       // into other protocols or categories.
01009       Methods.push_back(Overridden);
01010       return;
01011     }
01012 
01013   if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
01014     for (const auto *P : Protocol->protocols())
01015       CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
01016   }
01017 
01018   if (const ObjCInterfaceDecl *
01019         Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
01020     for (const auto *P : Interface->protocols())
01021       CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
01022 
01023     for (const auto *Cat : Interface->known_categories())
01024       CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
01025 
01026     if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
01027       return CollectOverriddenMethodsRecurse(Super, Method, Methods,
01028                                              /*MovedToSuper=*/true);
01029   }
01030 }
01031 
01032 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
01033                                             const ObjCMethodDecl *Method,
01034                              SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
01035   CollectOverriddenMethodsRecurse(Container, Method, Methods,
01036                                   /*MovedToSuper=*/false);
01037 }
01038 
01039 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
01040                           SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
01041   assert(Method->isOverriding());
01042 
01043   if (const ObjCProtocolDecl *
01044         ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
01045     CollectOverriddenMethods(ProtD, Method, overridden);
01046 
01047   } else if (const ObjCImplDecl *
01048                IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
01049     const ObjCInterfaceDecl *ID = IMD->getClassInterface();
01050     if (!ID)
01051       return;
01052     // Start searching for overridden methods using the method from the
01053     // interface as starting point.
01054     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
01055                                                     Method->isInstanceMethod(),
01056                                                     /*AllowHidden=*/true))
01057       Method = IFaceMeth;
01058     CollectOverriddenMethods(ID, Method, overridden);
01059 
01060   } else if (const ObjCCategoryDecl *
01061                CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
01062     const ObjCInterfaceDecl *ID = CatD->getClassInterface();
01063     if (!ID)
01064       return;
01065     // Start searching for overridden methods using the method from the
01066     // interface as starting point.
01067     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
01068                                                      Method->isInstanceMethod(),
01069                                                      /*AllowHidden=*/true))
01070       Method = IFaceMeth;
01071     CollectOverriddenMethods(ID, Method, overridden);
01072 
01073   } else {
01074     CollectOverriddenMethods(
01075                   dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
01076                   Method, overridden);
01077   }
01078 }
01079 
01080 void ObjCMethodDecl::getOverriddenMethods(
01081                     SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
01082   const ObjCMethodDecl *Method = this;
01083 
01084   if (Method->isRedeclaration()) {
01085     Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
01086                    getMethod(Method->getSelector(), Method->isInstanceMethod());
01087   }
01088 
01089   if (Method->isOverriding()) {
01090     collectOverriddenMethodsSlow(Method, Overridden);
01091     assert(!Overridden.empty() &&
01092            "ObjCMethodDecl's overriding bit is not as expected");
01093   }
01094 }
01095 
01096 const ObjCPropertyDecl *
01097 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
01098   Selector Sel = getSelector();
01099   unsigned NumArgs = Sel.getNumArgs();
01100   if (NumArgs > 1)
01101     return nullptr;
01102 
01103   if (!isInstanceMethod() || getMethodFamily() != OMF_None)
01104     return nullptr;
01105 
01106   if (isPropertyAccessor()) {
01107     const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
01108     // If container is class extension, find its primary class.
01109     if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
01110       if (CatDecl->IsClassExtension())
01111         Container = CatDecl->getClassInterface();
01112     
01113     bool IsGetter = (NumArgs == 0);
01114 
01115     for (const auto *I : Container->properties()) {
01116       Selector NextSel = IsGetter ? I->getGetterName()
01117                                   : I->getSetterName();
01118       if (NextSel == Sel)
01119         return I;
01120     }
01121 
01122     llvm_unreachable("Marked as a property accessor but no property found!");
01123   }
01124 
01125   if (!CheckOverrides)
01126     return nullptr;
01127 
01128   typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
01129   OverridesTy Overrides;
01130   getOverriddenMethods(Overrides);
01131   for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
01132        I != E; ++I) {
01133     if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
01134       return Prop;
01135   }
01136 
01137   return nullptr;
01138 }
01139 
01140 //===----------------------------------------------------------------------===//
01141 // ObjCInterfaceDecl
01142 //===----------------------------------------------------------------------===//
01143 
01144 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
01145                                              DeclContext *DC,
01146                                              SourceLocation atLoc,
01147                                              IdentifierInfo *Id,
01148                                              ObjCInterfaceDecl *PrevDecl,
01149                                              SourceLocation ClassLoc,
01150                                              bool isInternal){
01151   ObjCInterfaceDecl *Result = new (C, DC)
01152       ObjCInterfaceDecl(C, DC, atLoc, Id, ClassLoc, PrevDecl, isInternal);
01153   Result->Data.setInt(!C.getLangOpts().Modules);
01154   C.getObjCInterfaceType(Result, PrevDecl);
01155   return Result;
01156 }
01157 
01158 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
01159                                                          unsigned ID) {
01160   ObjCInterfaceDecl *Result = new (C, ID) ObjCInterfaceDecl(C, nullptr,
01161                                                             SourceLocation(),
01162                                                             nullptr,
01163                                                             SourceLocation(),
01164                                                             nullptr, false);
01165   Result->Data.setInt(!C.getLangOpts().Modules);
01166   return Result;
01167 }
01168 
01169 ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
01170                                      SourceLocation AtLoc, IdentifierInfo *Id,
01171                                      SourceLocation CLoc,
01172                                      ObjCInterfaceDecl *PrevDecl,
01173                                      bool IsInternal)
01174     : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
01175       redeclarable_base(C), TypeForDecl(nullptr), Data() {
01176   setPreviousDecl(PrevDecl);
01177   
01178   // Copy the 'data' pointer over.
01179   if (PrevDecl)
01180     Data = PrevDecl->Data;
01181   
01182   setImplicit(IsInternal);
01183 }
01184 
01185 void ObjCInterfaceDecl::LoadExternalDefinition() const {
01186   assert(data().ExternallyCompleted && "Class is not externally completed");
01187   data().ExternallyCompleted = false;
01188   getASTContext().getExternalSource()->CompleteType(
01189                                         const_cast<ObjCInterfaceDecl *>(this));
01190 }
01191 
01192 void ObjCInterfaceDecl::setExternallyCompleted() {
01193   assert(getASTContext().getExternalSource() && 
01194          "Class can't be externally completed without an external source");
01195   assert(hasDefinition() && 
01196          "Forward declarations can't be externally completed");
01197   data().ExternallyCompleted = true;
01198 }
01199 
01200 void ObjCInterfaceDecl::setHasDesignatedInitializers() {
01201   // Check for a complete definition and recover if not so.
01202   if (!isThisDeclarationADefinition())
01203     return;
01204   data().HasDesignatedInitializers = true;
01205 }
01206 
01207 bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
01208   // Check for a complete definition and recover if not so.
01209   if (!isThisDeclarationADefinition())
01210     return false;
01211   if (data().ExternallyCompleted)
01212     LoadExternalDefinition();
01213 
01214   return data().HasDesignatedInitializers;
01215 }
01216 
01217 StringRef
01218 ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
01219   if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
01220     return ObjCRTName->getMetadataName();
01221 
01222   return getName();
01223 }
01224 
01225 StringRef
01226 ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
01227   if (ObjCInterfaceDecl *ID =
01228       const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
01229     return ID->getObjCRuntimeNameAsString();
01230     
01231   return getName();
01232 }
01233 
01234 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
01235   if (const ObjCInterfaceDecl *Def = getDefinition()) {
01236     if (data().ExternallyCompleted)
01237       LoadExternalDefinition();
01238     
01239     return getASTContext().getObjCImplementation(
01240              const_cast<ObjCInterfaceDecl*>(Def));
01241   }
01242   
01243   // FIXME: Should make sure no callers ever do this.
01244   return nullptr;
01245 }
01246 
01247 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
01248   getASTContext().setObjCImplementation(getDefinition(), ImplD);
01249 }
01250 
01251 namespace {
01252   struct SynthesizeIvarChunk {
01253     uint64_t Size;
01254     ObjCIvarDecl *Ivar;
01255     SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
01256       : Size(size), Ivar(ivar) {}
01257   };
01258 
01259   bool operator<(const SynthesizeIvarChunk & LHS,
01260                  const SynthesizeIvarChunk &RHS) {
01261       return LHS.Size < RHS.Size;
01262   }
01263 }
01264 
01265 /// all_declared_ivar_begin - return first ivar declared in this class,
01266 /// its extensions and its implementation. Lazily build the list on first
01267 /// access.
01268 ///
01269 /// Caveat: The list returned by this method reflects the current
01270 /// state of the parser. The cache will be updated for every ivar
01271 /// added by an extension or the implementation when they are
01272 /// encountered.
01273 /// See also ObjCIvarDecl::Create().
01274 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
01275   // FIXME: Should make sure no callers ever do this.
01276   if (!hasDefinition())
01277     return nullptr;
01278 
01279   ObjCIvarDecl *curIvar = nullptr;
01280   if (!data().IvarList) {
01281     if (!ivar_empty()) {
01282       ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
01283       data().IvarList = *I; ++I;
01284       for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
01285         curIvar->setNextIvar(*I);
01286     }
01287 
01288     for (const auto *Ext : known_extensions()) {
01289       if (!Ext->ivar_empty()) {
01290         ObjCCategoryDecl::ivar_iterator
01291           I = Ext->ivar_begin(),
01292           E = Ext->ivar_end();
01293         if (!data().IvarList) {
01294           data().IvarList = *I; ++I;
01295           curIvar = data().IvarList;
01296         }
01297         for ( ;I != E; curIvar = *I, ++I)
01298           curIvar->setNextIvar(*I);
01299       }
01300     }
01301     data().IvarListMissingImplementation = true;
01302   }
01303 
01304   // cached and complete!
01305   if (!data().IvarListMissingImplementation)
01306       return data().IvarList;
01307   
01308   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
01309     data().IvarListMissingImplementation = false;
01310     if (!ImplDecl->ivar_empty()) {
01311       SmallVector<SynthesizeIvarChunk, 16> layout;
01312       for (auto *IV : ImplDecl->ivars()) {
01313         if (IV->getSynthesize() && !IV->isInvalidDecl()) {
01314           layout.push_back(SynthesizeIvarChunk(
01315                              IV->getASTContext().getTypeSize(IV->getType()), IV));
01316           continue;
01317         }
01318         if (!data().IvarList)
01319           data().IvarList = IV;
01320         else
01321           curIvar->setNextIvar(IV);
01322         curIvar = IV;
01323       }
01324       
01325       if (!layout.empty()) {
01326         // Order synthesized ivars by their size.
01327         std::stable_sort(layout.begin(), layout.end());
01328         unsigned Ix = 0, EIx = layout.size();
01329         if (!data().IvarList) {
01330           data().IvarList = layout[0].Ivar; Ix++;
01331           curIvar = data().IvarList;
01332         }
01333         for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
01334           curIvar->setNextIvar(layout[Ix].Ivar);
01335       }
01336     }
01337   }
01338   return data().IvarList;
01339 }
01340 
01341 /// FindCategoryDeclaration - Finds category declaration in the list of
01342 /// categories for this class and returns it. Name of the category is passed
01343 /// in 'CategoryId'. If category not found, return 0;
01344 ///
01345 ObjCCategoryDecl *
01346 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
01347   // FIXME: Should make sure no callers ever do this.
01348   if (!hasDefinition())
01349     return nullptr;
01350 
01351   if (data().ExternallyCompleted)
01352     LoadExternalDefinition();
01353 
01354   for (auto *Cat : visible_categories())
01355     if (Cat->getIdentifier() == CategoryId)
01356       return Cat;
01357 
01358   return nullptr;
01359 }
01360 
01361 ObjCMethodDecl *
01362 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
01363   for (const auto *Cat : visible_categories()) {
01364     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
01365       if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
01366         return MD;
01367   }
01368 
01369   return nullptr;
01370 }
01371 
01372 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
01373   for (const auto *Cat : visible_categories()) {
01374     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
01375       if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
01376         return MD;
01377   }
01378 
01379   return nullptr;
01380 }
01381 
01382 /// ClassImplementsProtocol - Checks that 'lProto' protocol
01383 /// has been implemented in IDecl class, its super class or categories (if
01384 /// lookupCategory is true).
01385 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
01386                                     bool lookupCategory,
01387                                     bool RHSIsQualifiedID) {
01388   if (!hasDefinition())
01389     return false;
01390   
01391   ObjCInterfaceDecl *IDecl = this;
01392   // 1st, look up the class.
01393   for (auto *PI : IDecl->protocols()){
01394     if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
01395       return true;
01396     // This is dubious and is added to be compatible with gcc.  In gcc, it is
01397     // also allowed assigning a protocol-qualified 'id' type to a LHS object
01398     // when protocol in qualified LHS is in list of protocols in the rhs 'id'
01399     // object. This IMO, should be a bug.
01400     // FIXME: Treat this as an extension, and flag this as an error when GCC
01401     // extensions are not enabled.
01402     if (RHSIsQualifiedID &&
01403         getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
01404       return true;
01405   }
01406 
01407   // 2nd, look up the category.
01408   if (lookupCategory)
01409     for (const auto *Cat : visible_categories()) {
01410       for (auto *PI : Cat->protocols())
01411         if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
01412           return true;
01413     }
01414 
01415   // 3rd, look up the super class(s)
01416   if (IDecl->getSuperClass())
01417     return
01418   IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
01419                                                   RHSIsQualifiedID);
01420 
01421   return false;
01422 }
01423 
01424 //===----------------------------------------------------------------------===//
01425 // ObjCIvarDecl
01426 //===----------------------------------------------------------------------===//
01427 
01428 void ObjCIvarDecl::anchor() { }
01429 
01430 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
01431                                    SourceLocation StartLoc,
01432                                    SourceLocation IdLoc, IdentifierInfo *Id,
01433                                    QualType T, TypeSourceInfo *TInfo,
01434                                    AccessControl ac, Expr *BW,
01435                                    bool synthesized) {
01436   if (DC) {
01437     // Ivar's can only appear in interfaces, implementations (via synthesized
01438     // properties), and class extensions (via direct declaration, or synthesized
01439     // properties).
01440     //
01441     // FIXME: This should really be asserting this:
01442     //   (isa<ObjCCategoryDecl>(DC) &&
01443     //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
01444     // but unfortunately we sometimes place ivars into non-class extension
01445     // categories on error. This breaks an AST invariant, and should not be
01446     // fixed.
01447     assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
01448             isa<ObjCCategoryDecl>(DC)) &&
01449            "Invalid ivar decl context!");
01450     // Once a new ivar is created in any of class/class-extension/implementation
01451     // decl contexts, the previously built IvarList must be rebuilt.
01452     ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
01453     if (!ID) {
01454       if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
01455         ID = IM->getClassInterface();
01456       else
01457         ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
01458     }
01459     ID->setIvarList(nullptr);
01460   }
01461 
01462   return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
01463                                   synthesized);
01464 }
01465 
01466 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
01467   return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
01468                                   nullptr, QualType(), nullptr,
01469                                   ObjCIvarDecl::None, nullptr, false);
01470 }
01471 
01472 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
01473   const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
01474 
01475   switch (DC->getKind()) {
01476   default:
01477   case ObjCCategoryImpl:
01478   case ObjCProtocol:
01479     llvm_unreachable("invalid ivar container!");
01480 
01481     // Ivars can only appear in class extension categories.
01482   case ObjCCategory: {
01483     const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
01484     assert(CD->IsClassExtension() && "invalid container for ivar!");
01485     return CD->getClassInterface();
01486   }
01487 
01488   case ObjCImplementation:
01489     return cast<ObjCImplementationDecl>(DC)->getClassInterface();
01490 
01491   case ObjCInterface:
01492     return cast<ObjCInterfaceDecl>(DC);
01493   }
01494 }
01495 
01496 //===----------------------------------------------------------------------===//
01497 // ObjCAtDefsFieldDecl
01498 //===----------------------------------------------------------------------===//
01499 
01500 void ObjCAtDefsFieldDecl::anchor() { }
01501 
01502 ObjCAtDefsFieldDecl
01503 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
01504                              SourceLocation StartLoc,  SourceLocation IdLoc,
01505                              IdentifierInfo *Id, QualType T, Expr *BW) {
01506   return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
01507 }
01508 
01509 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
01510                                                              unsigned ID) {
01511   return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
01512                                          SourceLocation(), nullptr, QualType(),
01513                                          nullptr);
01514 }
01515 
01516 //===----------------------------------------------------------------------===//
01517 // ObjCProtocolDecl
01518 //===----------------------------------------------------------------------===//
01519 
01520 void ObjCProtocolDecl::anchor() { }
01521 
01522 ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
01523                                    IdentifierInfo *Id, SourceLocation nameLoc,
01524                                    SourceLocation atStartLoc,
01525                                    ObjCProtocolDecl *PrevDecl)
01526     : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
01527       redeclarable_base(C), Data() {
01528   setPreviousDecl(PrevDecl);
01529   if (PrevDecl)
01530     Data = PrevDecl->Data;
01531 }
01532 
01533 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
01534                                            IdentifierInfo *Id,
01535                                            SourceLocation nameLoc,
01536                                            SourceLocation atStartLoc,
01537                                            ObjCProtocolDecl *PrevDecl) {
01538   ObjCProtocolDecl *Result =
01539       new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
01540   Result->Data.setInt(!C.getLangOpts().Modules);
01541   return Result;
01542 }
01543 
01544 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
01545                                                        unsigned ID) {
01546   ObjCProtocolDecl *Result =
01547       new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
01548                                    SourceLocation(), nullptr);
01549   Result->Data.setInt(!C.getLangOpts().Modules);
01550   return Result;
01551 }
01552 
01553 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
01554   ObjCProtocolDecl *PDecl = this;
01555 
01556   if (Name == getIdentifier())
01557     return PDecl;
01558 
01559   for (auto *I : protocols())
01560     if ((PDecl = I->lookupProtocolNamed(Name)))
01561       return PDecl;
01562 
01563   return nullptr;
01564 }
01565 
01566 // lookupMethod - Lookup a instance/class method in the protocol and protocols
01567 // it inherited.
01568 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
01569                                                bool isInstance) const {
01570   ObjCMethodDecl *MethodDecl = nullptr;
01571 
01572   // If there is no definition or the definition is hidden, we don't find
01573   // anything.
01574   const ObjCProtocolDecl *Def = getDefinition();
01575   if (!Def || Def->isHidden())
01576     return nullptr;
01577 
01578   if ((MethodDecl = getMethod(Sel, isInstance)))
01579     return MethodDecl;
01580 
01581   for (const auto *I : protocols())
01582     if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
01583       return MethodDecl;
01584   return nullptr;
01585 }
01586 
01587 void ObjCProtocolDecl::allocateDefinitionData() {
01588   assert(!Data.getPointer() && "Protocol already has a definition!");
01589   Data.setPointer(new (getASTContext()) DefinitionData);
01590   Data.getPointer()->Definition = this;
01591 }
01592 
01593 void ObjCProtocolDecl::startDefinition() {
01594   allocateDefinitionData();
01595   
01596   // Update all of the declarations with a pointer to the definition.
01597   for (auto RD : redecls())
01598     RD->Data = this->Data;
01599 }
01600 
01601 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
01602                                                     PropertyDeclOrder &PO) const {
01603   
01604   if (const ObjCProtocolDecl *PDecl = getDefinition()) {
01605     for (auto *Prop : PDecl->properties()) {
01606       // Insert into PM if not there already.
01607       PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
01608       PO.push_back(Prop);
01609     }
01610     // Scan through protocol's protocols.
01611     for (const auto *PI : PDecl->protocols())
01612       PI->collectPropertiesToImplement(PM, PO);
01613   }
01614 }
01615 
01616     
01617 void ObjCProtocolDecl::collectInheritedProtocolProperties(
01618                                                 const ObjCPropertyDecl *Property,
01619                                                 ProtocolPropertyMap &PM) const {
01620   if (const ObjCProtocolDecl *PDecl = getDefinition()) {
01621     bool MatchFound = false;
01622     for (auto *Prop : PDecl->properties()) {
01623       if (Prop == Property)
01624         continue;
01625       if (Prop->getIdentifier() == Property->getIdentifier()) {
01626         PM[PDecl] = Prop;
01627         MatchFound = true;
01628         break;
01629       }
01630     }
01631     // Scan through protocol's protocols which did not have a matching property.
01632     if (!MatchFound)
01633       for (const auto *PI : PDecl->protocols())
01634         PI->collectInheritedProtocolProperties(Property, PM);
01635   }
01636 }
01637 
01638 StringRef
01639 ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
01640   if (ObjCRuntimeNameAttr *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
01641     return ObjCRTName->getMetadataName();
01642 
01643   return getName();
01644 }
01645 
01646 //===----------------------------------------------------------------------===//
01647 // ObjCCategoryDecl
01648 //===----------------------------------------------------------------------===//
01649 
01650 void ObjCCategoryDecl::anchor() { }
01651 
01652 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
01653                                            SourceLocation AtLoc,
01654                                            SourceLocation ClassNameLoc,
01655                                            SourceLocation CategoryNameLoc,
01656                                            IdentifierInfo *Id,
01657                                            ObjCInterfaceDecl *IDecl,
01658                                            SourceLocation IvarLBraceLoc,
01659                                            SourceLocation IvarRBraceLoc) {
01660   ObjCCategoryDecl *CatDecl =
01661       new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
01662                                    IDecl, IvarLBraceLoc, IvarRBraceLoc);
01663   if (IDecl) {
01664     // Link this category into its class's category list.
01665     CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
01666     if (IDecl->hasDefinition()) {
01667       IDecl->setCategoryListRaw(CatDecl);
01668       if (ASTMutationListener *L = C.getASTMutationListener())
01669         L->AddedObjCCategoryToInterface(CatDecl, IDecl);
01670     }
01671   }
01672 
01673   return CatDecl;
01674 }
01675 
01676 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
01677                                                        unsigned ID) {
01678   return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
01679                                       SourceLocation(), SourceLocation(),
01680                                       nullptr, nullptr);
01681 }
01682 
01683 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
01684   return getASTContext().getObjCImplementation(
01685                                            const_cast<ObjCCategoryDecl*>(this));
01686 }
01687 
01688 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
01689   getASTContext().setObjCImplementation(this, ImplD);
01690 }
01691 
01692 
01693 //===----------------------------------------------------------------------===//
01694 // ObjCCategoryImplDecl
01695 //===----------------------------------------------------------------------===//
01696 
01697 void ObjCCategoryImplDecl::anchor() { }
01698 
01699 ObjCCategoryImplDecl *
01700 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
01701                              IdentifierInfo *Id,
01702                              ObjCInterfaceDecl *ClassInterface,
01703                              SourceLocation nameLoc,
01704                              SourceLocation atStartLoc,
01705                              SourceLocation CategoryNameLoc) {
01706   if (ClassInterface && ClassInterface->hasDefinition())
01707     ClassInterface = ClassInterface->getDefinition();
01708   return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
01709                                           atStartLoc, CategoryNameLoc);
01710 }
01711 
01712 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C, 
01713                                                                unsigned ID) {
01714   return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
01715                                           SourceLocation(), SourceLocation(),
01716                                           SourceLocation());
01717 }
01718 
01719 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
01720   // The class interface might be NULL if we are working with invalid code.
01721   if (const ObjCInterfaceDecl *ID = getClassInterface())
01722     return ID->FindCategoryDeclaration(getIdentifier());
01723   return nullptr;
01724 }
01725 
01726 
01727 void ObjCImplDecl::anchor() { }
01728 
01729 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
01730   // FIXME: The context should be correct before we get here.
01731   property->setLexicalDeclContext(this);
01732   addDecl(property);
01733 }
01734 
01735 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
01736   ASTContext &Ctx = getASTContext();
01737 
01738   if (ObjCImplementationDecl *ImplD
01739         = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
01740     if (IFace)
01741       Ctx.setObjCImplementation(IFace, ImplD);
01742 
01743   } else if (ObjCCategoryImplDecl *ImplD =
01744              dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
01745     if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
01746       Ctx.setObjCImplementation(CD, ImplD);
01747   }
01748 
01749   ClassInterface = IFace;
01750 }
01751 
01752 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
01753 /// properties implemented in this \@implementation block and returns
01754 /// the implemented property that uses it.
01755 ///
01756 ObjCPropertyImplDecl *ObjCImplDecl::
01757 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
01758   for (auto *PID : property_impls())
01759     if (PID->getPropertyIvarDecl() &&
01760         PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
01761       return PID;
01762   return nullptr;
01763 }
01764 
01765 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
01766 /// added to the list of those properties \@synthesized/\@dynamic in this
01767 /// category \@implementation block.
01768 ///
01769 ObjCPropertyImplDecl *ObjCImplDecl::
01770 FindPropertyImplDecl(IdentifierInfo *Id) const {
01771   for (auto *PID : property_impls())
01772     if (PID->getPropertyDecl()->getIdentifier() == Id)
01773       return PID;
01774   return nullptr;
01775 }
01776 
01777 raw_ostream &clang::operator<<(raw_ostream &OS,
01778                                const ObjCCategoryImplDecl &CID) {
01779   OS << CID.getName();
01780   return OS;
01781 }
01782 
01783 //===----------------------------------------------------------------------===//
01784 // ObjCImplementationDecl
01785 //===----------------------------------------------------------------------===//
01786 
01787 void ObjCImplementationDecl::anchor() { }
01788 
01789 ObjCImplementationDecl *
01790 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
01791                                ObjCInterfaceDecl *ClassInterface,
01792                                ObjCInterfaceDecl *SuperDecl,
01793                                SourceLocation nameLoc,
01794                                SourceLocation atStartLoc,
01795                                SourceLocation superLoc,
01796                                SourceLocation IvarLBraceLoc,
01797                                SourceLocation IvarRBraceLoc) {
01798   if (ClassInterface && ClassInterface->hasDefinition())
01799     ClassInterface = ClassInterface->getDefinition();
01800   return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
01801                                             nameLoc, atStartLoc, superLoc,
01802                                             IvarLBraceLoc, IvarRBraceLoc);
01803 }
01804 
01805 ObjCImplementationDecl *
01806 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
01807   return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
01808                                             SourceLocation(), SourceLocation());
01809 }
01810 
01811 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
01812                                              CXXCtorInitializer ** initializers,
01813                                                  unsigned numInitializers) {
01814   if (numInitializers > 0) {
01815     NumIvarInitializers = numInitializers;
01816     CXXCtorInitializer **ivarInitializers =
01817     new (C) CXXCtorInitializer*[NumIvarInitializers];
01818     memcpy(ivarInitializers, initializers,
01819            numInitializers * sizeof(CXXCtorInitializer*));
01820     IvarInitializers = ivarInitializers;
01821   }
01822 }
01823 
01824 raw_ostream &clang::operator<<(raw_ostream &OS,
01825                                const ObjCImplementationDecl &ID) {
01826   OS << ID.getName();
01827   return OS;
01828 }
01829 
01830 //===----------------------------------------------------------------------===//
01831 // ObjCCompatibleAliasDecl
01832 //===----------------------------------------------------------------------===//
01833 
01834 void ObjCCompatibleAliasDecl::anchor() { }
01835 
01836 ObjCCompatibleAliasDecl *
01837 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
01838                                 SourceLocation L,
01839                                 IdentifierInfo *Id,
01840                                 ObjCInterfaceDecl* AliasedClass) {
01841   return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
01842 }
01843 
01844 ObjCCompatibleAliasDecl *
01845 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
01846   return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
01847                                              nullptr, nullptr);
01848 }
01849 
01850 //===----------------------------------------------------------------------===//
01851 // ObjCPropertyDecl
01852 //===----------------------------------------------------------------------===//
01853 
01854 void ObjCPropertyDecl::anchor() { }
01855 
01856 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
01857                                            SourceLocation L,
01858                                            IdentifierInfo *Id,
01859                                            SourceLocation AtLoc,
01860                                            SourceLocation LParenLoc,
01861                                            TypeSourceInfo *T,
01862                                            PropertyControl propControl) {
01863   return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
01864 }
01865 
01866 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
01867                                                        unsigned ID) {
01868   return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
01869                                       SourceLocation(), SourceLocation(),
01870                                       nullptr);
01871 }
01872 
01873 //===----------------------------------------------------------------------===//
01874 // ObjCPropertyImplDecl
01875 //===----------------------------------------------------------------------===//
01876 
01877 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
01878                                                    DeclContext *DC,
01879                                                    SourceLocation atLoc,
01880                                                    SourceLocation L,
01881                                                    ObjCPropertyDecl *property,
01882                                                    Kind PK,
01883                                                    ObjCIvarDecl *ivar,
01884                                                    SourceLocation ivarLoc) {
01885   return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
01886                                           ivarLoc);
01887 }
01888 
01889 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
01890                                                                unsigned ID) {
01891   return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
01892                                           SourceLocation(), nullptr, Dynamic,
01893                                           nullptr, SourceLocation());
01894 }
01895 
01896 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
01897   SourceLocation EndLoc = getLocation();
01898   if (IvarLoc.isValid())
01899     EndLoc = IvarLoc;
01900 
01901   return SourceRange(AtLoc, EndLoc);
01902 }