Home | History | Annotate | Download | only in AST
      1 //===--- DeclObjC.cpp - ObjC Declaration AST Node Implementation ----------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the Objective-C related Decl classes.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/AST/DeclObjC.h"
     15 #include "clang/AST/ASTContext.h"
     16 #include "clang/AST/Stmt.h"
     17 #include "clang/AST/ASTMutationListener.h"
     18 #include "llvm/ADT/STLExtras.h"
     19 using namespace clang;
     20 
     21 //===----------------------------------------------------------------------===//
     22 // ObjCListBase
     23 //===----------------------------------------------------------------------===//
     24 
     25 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
     26   List = 0;
     27   if (Elts == 0) return;  // Setting to an empty list is a noop.
     28 
     29 
     30   List = new (Ctx) void*[Elts];
     31   NumElts = Elts;
     32   memcpy(List, InList, sizeof(void*)*Elts);
     33 }
     34 
     35 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
     36                            const SourceLocation *Locs, ASTContext &Ctx) {
     37   if (Elts == 0)
     38     return;
     39 
     40   Locations = new (Ctx) SourceLocation[Elts];
     41   memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
     42   set(InList, Elts, Ctx);
     43 }
     44 
     45 //===----------------------------------------------------------------------===//
     46 // ObjCInterfaceDecl
     47 //===----------------------------------------------------------------------===//
     48 
     49 void ObjCContainerDecl::anchor() { }
     50 
     51 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
     52 ///
     53 ObjCIvarDecl *
     54 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
     55   lookup_const_iterator Ivar, IvarEnd;
     56   for (llvm::tie(Ivar, IvarEnd) = lookup(Id); Ivar != IvarEnd; ++Ivar) {
     57     if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
     58       return ivar;
     59   }
     60   return 0;
     61 }
     62 
     63 // Get the local instance/class method declared in this interface.
     64 ObjCMethodDecl *
     65 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance) const {
     66   // Since instance & class methods can have the same name, the loop below
     67   // ensures we get the correct method.
     68   //
     69   // @interface Whatever
     70   // - (int) class_method;
     71   // + (float) class_method;
     72   // @end
     73   //
     74   lookup_const_iterator Meth, MethEnd;
     75   for (llvm::tie(Meth, MethEnd) = lookup(Sel); Meth != MethEnd; ++Meth) {
     76     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
     77     if (MD && MD->isInstanceMethod() == isInstance)
     78       return MD;
     79   }
     80   return 0;
     81 }
     82 
     83 ObjCPropertyDecl *
     84 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
     85                                    IdentifierInfo *propertyID) {
     86 
     87   DeclContext::lookup_const_iterator I, E;
     88   llvm::tie(I, E) = DC->lookup(propertyID);
     89   for ( ; I != E; ++I)
     90     if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
     91       return PD;
     92 
     93   return 0;
     94 }
     95 
     96 /// FindPropertyDeclaration - Finds declaration of the property given its name
     97 /// in 'PropertyId' and returns it. It returns 0, if not found.
     98 ObjCPropertyDecl *
     99 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
    100 
    101   if (ObjCPropertyDecl *PD =
    102         ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
    103     return PD;
    104 
    105   switch (getKind()) {
    106     default:
    107       break;
    108     case Decl::ObjCProtocol: {
    109       const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
    110       for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
    111            E = PID->protocol_end(); I != E; ++I)
    112         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
    113           return P;
    114       break;
    115     }
    116     case Decl::ObjCInterface: {
    117       const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
    118       // Look through categories.
    119       for (ObjCCategoryDecl *Cat = OID->getCategoryList();
    120            Cat; Cat = Cat->getNextClassCategory())
    121         if (!Cat->IsClassExtension())
    122           if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
    123             return P;
    124 
    125       // Look through protocols.
    126       for (ObjCInterfaceDecl::all_protocol_iterator
    127             I = OID->all_referenced_protocol_begin(),
    128             E = OID->all_referenced_protocol_end(); I != E; ++I)
    129         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
    130           return P;
    131 
    132       // Finally, check the super class.
    133       if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
    134         return superClass->FindPropertyDeclaration(PropertyId);
    135       break;
    136     }
    137     case Decl::ObjCCategory: {
    138       const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
    139       // Look through protocols.
    140       if (!OCD->IsClassExtension())
    141         for (ObjCCategoryDecl::protocol_iterator
    142               I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
    143         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
    144           return P;
    145 
    146       break;
    147     }
    148   }
    149   return 0;
    150 }
    151 
    152 void ObjCInterfaceDecl::anchor() { }
    153 
    154 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
    155 /// with name 'PropertyId' in the primary class; including those in protocols
    156 /// (direct or indirect) used by the primary class.
    157 ///
    158 ObjCPropertyDecl *
    159 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
    160                                             IdentifierInfo *PropertyId) const {
    161   // FIXME: Should make sure no callers ever do this.
    162   if (!hasDefinition())
    163     return 0;
    164 
    165   if (data().ExternallyCompleted)
    166     LoadExternalDefinition();
    167 
    168   if (ObjCPropertyDecl *PD =
    169       ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
    170     return PD;
    171 
    172   // Look through protocols.
    173   for (ObjCInterfaceDecl::all_protocol_iterator
    174         I = all_referenced_protocol_begin(),
    175         E = all_referenced_protocol_end(); I != E; ++I)
    176     if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
    177       return P;
    178 
    179   return 0;
    180 }
    181 
    182 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
    183                               ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
    184                               ASTContext &C)
    185 {
    186   if (data().ExternallyCompleted)
    187     LoadExternalDefinition();
    188 
    189   if (data().AllReferencedProtocols.empty() &&
    190       data().ReferencedProtocols.empty()) {
    191     data().AllReferencedProtocols.set(ExtList, ExtNum, C);
    192     return;
    193   }
    194 
    195   // Check for duplicate protocol in class's protocol list.
    196   // This is O(n*m). But it is extremely rare and number of protocols in
    197   // class or its extension are very few.
    198   SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
    199   for (unsigned i = 0; i < ExtNum; i++) {
    200     bool protocolExists = false;
    201     ObjCProtocolDecl *ProtoInExtension = ExtList[i];
    202     for (all_protocol_iterator
    203           p = all_referenced_protocol_begin(),
    204           e = all_referenced_protocol_end(); p != e; ++p) {
    205       ObjCProtocolDecl *Proto = (*p);
    206       if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
    207         protocolExists = true;
    208         break;
    209       }
    210     }
    211     // Do we want to warn on a protocol in extension class which
    212     // already exist in the class? Probably not.
    213     if (!protocolExists)
    214       ProtocolRefs.push_back(ProtoInExtension);
    215   }
    216 
    217   if (ProtocolRefs.empty())
    218     return;
    219 
    220   // Merge ProtocolRefs into class's protocol list;
    221   for (all_protocol_iterator p = all_referenced_protocol_begin(),
    222         e = all_referenced_protocol_end(); p != e; ++p) {
    223     ProtocolRefs.push_back(*p);
    224   }
    225 
    226   data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
    227 }
    228 
    229 void ObjCInterfaceDecl::allocateDefinitionData() {
    230   assert(!hasDefinition() && "ObjC class already has a definition");
    231   Data = new (getASTContext()) DefinitionData();
    232   Data->Definition = this;
    233 
    234   // Make the type point at the definition, now that we have one.
    235   if (TypeForDecl)
    236     cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
    237 }
    238 
    239 void ObjCInterfaceDecl::startDefinition() {
    240   allocateDefinitionData();
    241 
    242   // Update all of the declarations with a pointer to the definition.
    243   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
    244        RD != RDEnd; ++RD) {
    245     if (*RD != this)
    246       RD->Data = Data;
    247   }
    248 }
    249 
    250 /// getFirstClassExtension - Find first class extension of the given class.
    251 ObjCCategoryDecl* ObjCInterfaceDecl::getFirstClassExtension() const {
    252   for (ObjCCategoryDecl *CDecl = getCategoryList(); CDecl;
    253        CDecl = CDecl->getNextClassCategory())
    254     if (CDecl->IsClassExtension())
    255       return CDecl;
    256   return 0;
    257 }
    258 
    259 /// getNextClassCategory - Find next class extension in list of categories.
    260 const ObjCCategoryDecl* ObjCCategoryDecl::getNextClassExtension() const {
    261   for (const ObjCCategoryDecl *CDecl = getNextClassCategory(); CDecl;
    262         CDecl = CDecl->getNextClassCategory())
    263     if (CDecl->IsClassExtension())
    264       return CDecl;
    265   return 0;
    266 }
    267 
    268 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
    269                                               ObjCInterfaceDecl *&clsDeclared) {
    270   // FIXME: Should make sure no callers ever do this.
    271   if (!hasDefinition())
    272     return 0;
    273 
    274   if (data().ExternallyCompleted)
    275     LoadExternalDefinition();
    276 
    277   ObjCInterfaceDecl* ClassDecl = this;
    278   while (ClassDecl != NULL) {
    279     if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
    280       clsDeclared = ClassDecl;
    281       return I;
    282     }
    283     for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
    284          CDecl; CDecl = CDecl->getNextClassExtension()) {
    285       if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
    286         clsDeclared = ClassDecl;
    287         return I;
    288       }
    289     }
    290 
    291     ClassDecl = ClassDecl->getSuperClass();
    292   }
    293   return NULL;
    294 }
    295 
    296 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
    297 /// class whose name is passed as argument. If it is not one of the super classes
    298 /// the it returns NULL.
    299 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
    300                                         const IdentifierInfo*ICName) {
    301   // FIXME: Should make sure no callers ever do this.
    302   if (!hasDefinition())
    303     return 0;
    304 
    305   if (data().ExternallyCompleted)
    306     LoadExternalDefinition();
    307 
    308   ObjCInterfaceDecl* ClassDecl = this;
    309   while (ClassDecl != NULL) {
    310     if (ClassDecl->getIdentifier() == ICName)
    311       return ClassDecl;
    312     ClassDecl = ClassDecl->getSuperClass();
    313   }
    314   return NULL;
    315 }
    316 
    317 /// lookupMethod - This method returns an instance/class method by looking in
    318 /// the class, its categories, and its super classes (using a linear search).
    319 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
    320                                      bool isInstance,
    321                                      bool shallowCategoryLookup) const {
    322   // FIXME: Should make sure no callers ever do this.
    323   if (!hasDefinition())
    324     return 0;
    325 
    326   const ObjCInterfaceDecl* ClassDecl = this;
    327   ObjCMethodDecl *MethodDecl = 0;
    328 
    329   if (data().ExternallyCompleted)
    330     LoadExternalDefinition();
    331 
    332   while (ClassDecl != NULL) {
    333     if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
    334       return MethodDecl;
    335 
    336     // Didn't find one yet - look through protocols.
    337     for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
    338                                               E = ClassDecl->protocol_end();
    339            I != E; ++I)
    340       if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
    341         return MethodDecl;
    342 
    343     // Didn't find one yet - now look through categories.
    344     ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
    345     while (CatDecl) {
    346       if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
    347         return MethodDecl;
    348 
    349       if (!shallowCategoryLookup) {
    350         // Didn't find one yet - look through protocols.
    351         const ObjCList<ObjCProtocolDecl> &Protocols =
    352           CatDecl->getReferencedProtocols();
    353         for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
    354              E = Protocols.end(); I != E; ++I)
    355           if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
    356             return MethodDecl;
    357       }
    358       CatDecl = CatDecl->getNextClassCategory();
    359     }
    360 
    361     ClassDecl = ClassDecl->getSuperClass();
    362   }
    363   return NULL;
    364 }
    365 
    366 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
    367                                    const Selector &Sel,
    368                                    bool Instance) {
    369   // FIXME: Should make sure no callers ever do this.
    370   if (!hasDefinition())
    371     return 0;
    372 
    373   if (data().ExternallyCompleted)
    374     LoadExternalDefinition();
    375 
    376   ObjCMethodDecl *Method = 0;
    377   if (ObjCImplementationDecl *ImpDecl = getImplementation())
    378     Method = Instance ? ImpDecl->getInstanceMethod(Sel)
    379                       : ImpDecl->getClassMethod(Sel);
    380 
    381   if (!Method && getSuperClass())
    382     return getSuperClass()->lookupPrivateMethod(Sel, Instance);
    383   return Method;
    384 }
    385 
    386 //===----------------------------------------------------------------------===//
    387 // ObjCMethodDecl
    388 //===----------------------------------------------------------------------===//
    389 
    390 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
    391                                        SourceLocation beginLoc,
    392                                        SourceLocation endLoc,
    393                                        Selector SelInfo, QualType T,
    394                                        TypeSourceInfo *ResultTInfo,
    395                                        DeclContext *contextDecl,
    396                                        bool isInstance,
    397                                        bool isVariadic,
    398                                        bool isSynthesized,
    399                                        bool isImplicitlyDeclared,
    400                                        bool isDefined,
    401                                        ImplementationControl impControl,
    402                                        bool HasRelatedResultType) {
    403   return new (C) ObjCMethodDecl(beginLoc, endLoc,
    404                                 SelInfo, T, ResultTInfo, contextDecl,
    405                                 isInstance,
    406                                 isVariadic, isSynthesized, isImplicitlyDeclared,
    407                                 isDefined,
    408                                 impControl,
    409                                 HasRelatedResultType);
    410 }
    411 
    412 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
    413   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
    414   return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
    415                                   Selector(), QualType(), 0, 0);
    416 }
    417 
    418 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
    419   assert(PrevMethod);
    420   getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
    421   IsRedeclaration = true;
    422   PrevMethod->HasRedeclaration = true;
    423 }
    424 
    425 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
    426                                          ArrayRef<ParmVarDecl*> Params,
    427                                          ArrayRef<SourceLocation> SelLocs) {
    428   ParamsAndSelLocs = 0;
    429   NumParams = Params.size();
    430   if (Params.empty() && SelLocs.empty())
    431     return;
    432 
    433   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
    434                   sizeof(SourceLocation) * SelLocs.size();
    435   ParamsAndSelLocs = C.Allocate(Size);
    436   std::copy(Params.begin(), Params.end(), getParams());
    437   std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
    438 }
    439 
    440 void ObjCMethodDecl::getSelectorLocs(
    441                                SmallVectorImpl<SourceLocation> &SelLocs) const {
    442   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
    443     SelLocs.push_back(getSelectorLoc(i));
    444 }
    445 
    446 void ObjCMethodDecl::setMethodParams(ASTContext &C,
    447                                      ArrayRef<ParmVarDecl*> Params,
    448                                      ArrayRef<SourceLocation> SelLocs) {
    449   assert((!SelLocs.empty() || isImplicit()) &&
    450          "No selector locs for non-implicit method");
    451   if (isImplicit())
    452     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
    453 
    454   SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
    455   if (SelLocsKind != SelLoc_NonStandard)
    456     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
    457 
    458   setParamsAndSelLocs(C, Params, SelLocs);
    459 }
    460 
    461 /// \brief A definition will return its interface declaration.
    462 /// An interface declaration will return its definition.
    463 /// Otherwise it will return itself.
    464 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
    465   ASTContext &Ctx = getASTContext();
    466   ObjCMethodDecl *Redecl = 0;
    467   if (HasRedeclaration)
    468     Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
    469   if (Redecl)
    470     return Redecl;
    471 
    472   Decl *CtxD = cast<Decl>(getDeclContext());
    473 
    474   if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
    475     if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
    476       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
    477 
    478   } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
    479     if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
    480       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
    481 
    482   } else if (ObjCImplementationDecl *ImplD =
    483                dyn_cast<ObjCImplementationDecl>(CtxD)) {
    484     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
    485       Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
    486 
    487   } else if (ObjCCategoryImplDecl *CImplD =
    488                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
    489     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
    490       Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
    491   }
    492 
    493   if (!Redecl && isRedeclaration()) {
    494     // This is the last redeclaration, go back to the first method.
    495     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
    496                                                     isInstanceMethod());
    497   }
    498 
    499   return Redecl ? Redecl : this;
    500 }
    501 
    502 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
    503   Decl *CtxD = cast<Decl>(getDeclContext());
    504 
    505   if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
    506     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
    507       if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
    508                                               isInstanceMethod()))
    509         return MD;
    510 
    511   } else if (ObjCCategoryImplDecl *CImplD =
    512                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
    513     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
    514       if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
    515                                                isInstanceMethod()))
    516         return MD;
    517   }
    518 
    519   if (isRedeclaration())
    520     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
    521                                                     isInstanceMethod());
    522 
    523   return this;
    524 }
    525 
    526 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
    527   ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
    528   if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
    529     return family;
    530 
    531   // Check for an explicit attribute.
    532   if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
    533     // The unfortunate necessity of mapping between enums here is due
    534     // to the attributes framework.
    535     switch (attr->getFamily()) {
    536     case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
    537     case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
    538     case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
    539     case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
    540     case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
    541     case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
    542     }
    543     Family = static_cast<unsigned>(family);
    544     return family;
    545   }
    546 
    547   family = getSelector().getMethodFamily();
    548   switch (family) {
    549   case OMF_None: break;
    550 
    551   // init only has a conventional meaning for an instance method, and
    552   // it has to return an object.
    553   case OMF_init:
    554     if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
    555       family = OMF_None;
    556     break;
    557 
    558   // alloc/copy/new have a conventional meaning for both class and
    559   // instance methods, but they require an object return.
    560   case OMF_alloc:
    561   case OMF_copy:
    562   case OMF_mutableCopy:
    563   case OMF_new:
    564     if (!getResultType()->isObjCObjectPointerType())
    565       family = OMF_None;
    566     break;
    567 
    568   // These selectors have a conventional meaning only for instance methods.
    569   case OMF_dealloc:
    570   case OMF_finalize:
    571   case OMF_retain:
    572   case OMF_release:
    573   case OMF_autorelease:
    574   case OMF_retainCount:
    575   case OMF_self:
    576     if (!isInstanceMethod())
    577       family = OMF_None;
    578     break;
    579 
    580   case OMF_performSelector:
    581     if (!isInstanceMethod() ||
    582         !getResultType()->isObjCIdType())
    583       family = OMF_None;
    584     else {
    585       unsigned noParams = param_size();
    586       if (noParams < 1 || noParams > 3)
    587         family = OMF_None;
    588       else {
    589         ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
    590         QualType ArgT = (*it);
    591         if (!ArgT->isObjCSelType()) {
    592           family = OMF_None;
    593           break;
    594         }
    595         while (--noParams) {
    596           it++;
    597           ArgT = (*it);
    598           if (!ArgT->isObjCIdType()) {
    599             family = OMF_None;
    600             break;
    601           }
    602         }
    603       }
    604     }
    605     break;
    606 
    607   }
    608 
    609   // Cache the result.
    610   Family = static_cast<unsigned>(family);
    611   return family;
    612 }
    613 
    614 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
    615                                           const ObjCInterfaceDecl *OID) {
    616   QualType selfTy;
    617   if (isInstanceMethod()) {
    618     // There may be no interface context due to error in declaration
    619     // of the interface (which has been reported). Recover gracefully.
    620     if (OID) {
    621       selfTy = Context.getObjCInterfaceType(OID);
    622       selfTy = Context.getObjCObjectPointerType(selfTy);
    623     } else {
    624       selfTy = Context.getObjCIdType();
    625     }
    626   } else // we have a factory method.
    627     selfTy = Context.getObjCClassType();
    628 
    629   bool selfIsPseudoStrong = false;
    630   bool selfIsConsumed = false;
    631 
    632   if (Context.getLangOpts().ObjCAutoRefCount) {
    633     if (isInstanceMethod()) {
    634       selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
    635 
    636       // 'self' is always __strong.  It's actually pseudo-strong except
    637       // in init methods (or methods labeled ns_consumes_self), though.
    638       Qualifiers qs;
    639       qs.setObjCLifetime(Qualifiers::OCL_Strong);
    640       selfTy = Context.getQualifiedType(selfTy, qs);
    641 
    642       // In addition, 'self' is const unless this is an init method.
    643       if (getMethodFamily() != OMF_init && !selfIsConsumed) {
    644         selfTy = selfTy.withConst();
    645         selfIsPseudoStrong = true;
    646       }
    647     }
    648     else {
    649       assert(isClassMethod());
    650       // 'self' is always const in class methods.
    651       selfTy = selfTy.withConst();
    652       selfIsPseudoStrong = true;
    653     }
    654   }
    655 
    656   ImplicitParamDecl *self
    657     = ImplicitParamDecl::Create(Context, this, SourceLocation(),
    658                                 &Context.Idents.get("self"), selfTy);
    659   setSelfDecl(self);
    660 
    661   if (selfIsConsumed)
    662     self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
    663 
    664   if (selfIsPseudoStrong)
    665     self->setARCPseudoStrong(true);
    666 
    667   setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
    668                                        &Context.Idents.get("_cmd"),
    669                                        Context.getObjCSelType()));
    670 }
    671 
    672 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
    673   if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
    674     return ID;
    675   if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
    676     return CD->getClassInterface();
    677   if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
    678     return IMD->getClassInterface();
    679 
    680   assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
    681   llvm_unreachable("unknown method context");
    682 }
    683 
    684 //===----------------------------------------------------------------------===//
    685 // ObjCInterfaceDecl
    686 //===----------------------------------------------------------------------===//
    687 
    688 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
    689                                              DeclContext *DC,
    690                                              SourceLocation atLoc,
    691                                              IdentifierInfo *Id,
    692                                              ObjCInterfaceDecl *PrevDecl,
    693                                              SourceLocation ClassLoc,
    694                                              bool isInternal){
    695   ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
    696                                                         PrevDecl, isInternal);
    697   C.getObjCInterfaceType(Result, PrevDecl);
    698   return Result;
    699 }
    700 
    701 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
    702                                                          unsigned ID) {
    703   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
    704   return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
    705                                      0, false);
    706 }
    707 
    708 ObjCInterfaceDecl::
    709 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
    710                   SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
    711                   bool isInternal)
    712   : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
    713     TypeForDecl(0), Data()
    714 {
    715   setPreviousDeclaration(PrevDecl);
    716 
    717   // Copy the 'data' pointer over.
    718   if (PrevDecl)
    719     Data = PrevDecl->Data;
    720 
    721   setImplicit(isInternal);
    722 }
    723 
    724 void ObjCInterfaceDecl::LoadExternalDefinition() const {
    725   assert(data().ExternallyCompleted && "Class is not externally completed");
    726   data().ExternallyCompleted = false;
    727   getASTContext().getExternalSource()->CompleteType(
    728                                         const_cast<ObjCInterfaceDecl *>(this));
    729 }
    730 
    731 void ObjCInterfaceDecl::setExternallyCompleted() {
    732   assert(getASTContext().getExternalSource() &&
    733          "Class can't be externally completed without an external source");
    734   assert(hasDefinition() &&
    735          "Forward declarations can't be externally completed");
    736   data().ExternallyCompleted = true;
    737 }
    738 
    739 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
    740   if (const ObjCInterfaceDecl *Def = getDefinition()) {
    741     if (data().ExternallyCompleted)
    742       LoadExternalDefinition();
    743 
    744     return getASTContext().getObjCImplementation(
    745              const_cast<ObjCInterfaceDecl*>(Def));
    746   }
    747 
    748   // FIXME: Should make sure no callers ever do this.
    749   return 0;
    750 }
    751 
    752 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
    753   getASTContext().setObjCImplementation(getDefinition(), ImplD);
    754 }
    755 
    756 /// all_declared_ivar_begin - return first ivar declared in this class,
    757 /// its extensions and its implementation. Lazily build the list on first
    758 /// access.
    759 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
    760   // FIXME: Should make sure no callers ever do this.
    761   if (!hasDefinition())
    762     return 0;
    763 
    764   if (data().IvarList)
    765     return data().IvarList;
    766 
    767   ObjCIvarDecl *curIvar = 0;
    768   if (!ivar_empty()) {
    769     ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
    770     data().IvarList = (*I); ++I;
    771     for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
    772       curIvar->setNextIvar(*I);
    773   }
    774 
    775   for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
    776        CDecl = CDecl->getNextClassExtension()) {
    777     if (!CDecl->ivar_empty()) {
    778       ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
    779                                           E = CDecl->ivar_end();
    780       if (!data().IvarList) {
    781         data().IvarList = (*I); ++I;
    782         curIvar = data().IvarList;
    783       }
    784       for ( ;I != E; curIvar = *I, ++I)
    785         curIvar->setNextIvar(*I);
    786     }
    787   }
    788 
    789   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
    790     if (!ImplDecl->ivar_empty()) {
    791       ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
    792                                             E = ImplDecl->ivar_end();
    793       if (!data().IvarList) {
    794         data().IvarList = (*I); ++I;
    795         curIvar = data().IvarList;
    796       }
    797       for ( ;I != E; curIvar = *I, ++I)
    798         curIvar->setNextIvar(*I);
    799     }
    800   }
    801   return data().IvarList;
    802 }
    803 
    804 /// FindCategoryDeclaration - Finds category declaration in the list of
    805 /// categories for this class and returns it. Name of the category is passed
    806 /// in 'CategoryId'. If category not found, return 0;
    807 ///
    808 ObjCCategoryDecl *
    809 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
    810   // FIXME: Should make sure no callers ever do this.
    811   if (!hasDefinition())
    812     return 0;
    813 
    814   if (data().ExternallyCompleted)
    815     LoadExternalDefinition();
    816 
    817   for (ObjCCategoryDecl *Category = getCategoryList();
    818        Category; Category = Category->getNextClassCategory())
    819     if (Category->getIdentifier() == CategoryId)
    820       return Category;
    821   return 0;
    822 }
    823 
    824 ObjCMethodDecl *
    825 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
    826   for (ObjCCategoryDecl *Category = getCategoryList();
    827        Category; Category = Category->getNextClassCategory())
    828     if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
    829       if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
    830         return MD;
    831   return 0;
    832 }
    833 
    834 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
    835   for (ObjCCategoryDecl *Category = getCategoryList();
    836        Category; Category = Category->getNextClassCategory())
    837     if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
    838       if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
    839         return MD;
    840   return 0;
    841 }
    842 
    843 /// ClassImplementsProtocol - Checks that 'lProto' protocol
    844 /// has been implemented in IDecl class, its super class or categories (if
    845 /// lookupCategory is true).
    846 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
    847                                     bool lookupCategory,
    848                                     bool RHSIsQualifiedID) {
    849   if (!hasDefinition())
    850     return false;
    851 
    852   ObjCInterfaceDecl *IDecl = this;
    853   // 1st, look up the class.
    854   for (ObjCInterfaceDecl::protocol_iterator
    855         PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
    856     if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
    857       return true;
    858     // This is dubious and is added to be compatible with gcc.  In gcc, it is
    859     // also allowed assigning a protocol-qualified 'id' type to a LHS object
    860     // when protocol in qualified LHS is in list of protocols in the rhs 'id'
    861     // object. This IMO, should be a bug.
    862     // FIXME: Treat this as an extension, and flag this as an error when GCC
    863     // extensions are not enabled.
    864     if (RHSIsQualifiedID &&
    865         getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
    866       return true;
    867   }
    868 
    869   // 2nd, look up the category.
    870   if (lookupCategory)
    871     for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
    872          CDecl = CDecl->getNextClassCategory()) {
    873       for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
    874            E = CDecl->protocol_end(); PI != E; ++PI)
    875         if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
    876           return true;
    877     }
    878 
    879   // 3rd, look up the super class(s)
    880   if (IDecl->getSuperClass())
    881     return
    882   IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
    883                                                   RHSIsQualifiedID);
    884 
    885   return false;
    886 }
    887 
    888 //===----------------------------------------------------------------------===//
    889 // ObjCIvarDecl
    890 //===----------------------------------------------------------------------===//
    891 
    892 void ObjCIvarDecl::anchor() { }
    893 
    894 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
    895                                    SourceLocation StartLoc,
    896                                    SourceLocation IdLoc, IdentifierInfo *Id,
    897                                    QualType T, TypeSourceInfo *TInfo,
    898                                    AccessControl ac, Expr *BW,
    899                                    bool synthesized) {
    900   if (DC) {
    901     // Ivar's can only appear in interfaces, implementations (via synthesized
    902     // properties), and class extensions (via direct declaration, or synthesized
    903     // properties).
    904     //
    905     // FIXME: This should really be asserting this:
    906     //   (isa<ObjCCategoryDecl>(DC) &&
    907     //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
    908     // but unfortunately we sometimes place ivars into non-class extension
    909     // categories on error. This breaks an AST invariant, and should not be
    910     // fixed.
    911     assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
    912             isa<ObjCCategoryDecl>(DC)) &&
    913            "Invalid ivar decl context!");
    914     // Once a new ivar is created in any of class/class-extension/implementation
    915     // decl contexts, the previously built IvarList must be rebuilt.
    916     ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
    917     if (!ID) {
    918       if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
    919         ID = IM->getClassInterface();
    920         if (BW)
    921           IM->setHasSynthBitfield(true);
    922       } else {
    923         ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
    924         ID = CD->getClassInterface();
    925         if (BW)
    926           CD->setHasSynthBitfield(true);
    927       }
    928     }
    929     ID->setIvarList(0);
    930   }
    931 
    932   return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
    933                               ac, BW, synthesized);
    934 }
    935 
    936 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
    937   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
    938   return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
    939                                 QualType(), 0, ObjCIvarDecl::None, 0, false);
    940 }
    941 
    942 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
    943   const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
    944 
    945   switch (DC->getKind()) {
    946   default:
    947   case ObjCCategoryImpl:
    948   case ObjCProtocol:
    949     llvm_unreachable("invalid ivar container!");
    950 
    951     // Ivars can only appear in class extension categories.
    952   case ObjCCategory: {
    953     const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
    954     assert(CD->IsClassExtension() && "invalid container for ivar!");
    955     return CD->getClassInterface();
    956   }
    957 
    958   case ObjCImplementation:
    959     return cast<ObjCImplementationDecl>(DC)->getClassInterface();
    960 
    961   case ObjCInterface:
    962     return cast<ObjCInterfaceDecl>(DC);
    963   }
    964 }
    965 
    966 //===----------------------------------------------------------------------===//
    967 // ObjCAtDefsFieldDecl
    968 //===----------------------------------------------------------------------===//
    969 
    970 void ObjCAtDefsFieldDecl::anchor() { }
    971 
    972 ObjCAtDefsFieldDecl
    973 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
    974                              SourceLocation StartLoc,  SourceLocation IdLoc,
    975                              IdentifierInfo *Id, QualType T, Expr *BW) {
    976   return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
    977 }
    978 
    979 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
    980                                                              unsigned ID) {
    981   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
    982   return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
    983                                        0, QualType(), 0);
    984 }
    985 
    986 //===----------------------------------------------------------------------===//
    987 // ObjCProtocolDecl
    988 //===----------------------------------------------------------------------===//
    989 
    990 void ObjCProtocolDecl::anchor() { }
    991 
    992 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
    993                                    SourceLocation nameLoc,
    994                                    SourceLocation atStartLoc,
    995                                    ObjCProtocolDecl *PrevDecl)
    996   : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
    997 {
    998   setPreviousDeclaration(PrevDecl);
    999   if (PrevDecl)
   1000     Data = PrevDecl->Data;
   1001 }
   1002 
   1003 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
   1004                                            IdentifierInfo *Id,
   1005                                            SourceLocation nameLoc,
   1006                                            SourceLocation atStartLoc,
   1007                                            ObjCProtocolDecl *PrevDecl) {
   1008   ObjCProtocolDecl *Result
   1009     = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
   1010 
   1011   return Result;
   1012 }
   1013 
   1014 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
   1015                                                        unsigned ID) {
   1016   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
   1017   return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
   1018                                     0);
   1019 }
   1020 
   1021 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
   1022   ObjCProtocolDecl *PDecl = this;
   1023 
   1024   if (Name == getIdentifier())
   1025     return PDecl;
   1026 
   1027   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
   1028     if ((PDecl = (*I)->lookupProtocolNamed(Name)))
   1029       return PDecl;
   1030 
   1031   return NULL;
   1032 }
   1033 
   1034 // lookupMethod - Lookup a instance/class method in the protocol and protocols
   1035 // it inherited.
   1036 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
   1037                                                bool isInstance) const {
   1038   ObjCMethodDecl *MethodDecl = NULL;
   1039 
   1040   if ((MethodDecl = getMethod(Sel, isInstance)))
   1041     return MethodDecl;
   1042 
   1043   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
   1044     if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
   1045       return MethodDecl;
   1046   return NULL;
   1047 }
   1048 
   1049 void ObjCProtocolDecl::allocateDefinitionData() {
   1050   assert(!Data && "Protocol already has a definition!");
   1051   Data = new (getASTContext()) DefinitionData;
   1052   Data->Definition = this;
   1053 }
   1054 
   1055 void ObjCProtocolDecl::startDefinition() {
   1056   allocateDefinitionData();
   1057 
   1058   // Update all of the declarations with a pointer to the definition.
   1059   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
   1060        RD != RDEnd; ++RD)
   1061     RD->Data = this->Data;
   1062 }
   1063 
   1064 //===----------------------------------------------------------------------===//
   1065 // ObjCCategoryDecl
   1066 //===----------------------------------------------------------------------===//
   1067 
   1068 void ObjCCategoryDecl::anchor() { }
   1069 
   1070 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
   1071                                            SourceLocation AtLoc,
   1072                                            SourceLocation ClassNameLoc,
   1073                                            SourceLocation CategoryNameLoc,
   1074                                            IdentifierInfo *Id,
   1075                                            ObjCInterfaceDecl *IDecl,
   1076                                            SourceLocation IvarLBraceLoc,
   1077                                            SourceLocation IvarRBraceLoc) {
   1078   ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
   1079                                                        CategoryNameLoc, Id,
   1080                                                        IDecl,
   1081                                                        IvarLBraceLoc, IvarRBraceLoc);
   1082   if (IDecl) {
   1083     // Link this category into its class's category list.
   1084     CatDecl->NextClassCategory = IDecl->getCategoryList();
   1085     if (IDecl->hasDefinition()) {
   1086       IDecl->setCategoryList(CatDecl);
   1087       if (ASTMutationListener *L = C.getASTMutationListener())
   1088         L->AddedObjCCategoryToInterface(CatDecl, IDecl);
   1089     }
   1090   }
   1091 
   1092   return CatDecl;
   1093 }
   1094 
   1095 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
   1096                                                        unsigned ID) {
   1097   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
   1098   return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
   1099                                     SourceLocation(), 0, 0);
   1100 }
   1101 
   1102 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
   1103   return getASTContext().getObjCImplementation(
   1104                                            const_cast<ObjCCategoryDecl*>(this));
   1105 }
   1106 
   1107 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
   1108   getASTContext().setObjCImplementation(this, ImplD);
   1109 }
   1110 
   1111 
   1112 //===----------------------------------------------------------------------===//
   1113 // ObjCCategoryImplDecl
   1114 //===----------------------------------------------------------------------===//
   1115 
   1116 void ObjCCategoryImplDecl::anchor() { }
   1117 
   1118 ObjCCategoryImplDecl *
   1119 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
   1120                              IdentifierInfo *Id,
   1121                              ObjCInterfaceDecl *ClassInterface,
   1122                              SourceLocation nameLoc,
   1123                              SourceLocation atStartLoc,
   1124                              SourceLocation CategoryNameLoc) {
   1125   if (ClassInterface && ClassInterface->hasDefinition())
   1126     ClassInterface = ClassInterface->getDefinition();
   1127   return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
   1128                                       nameLoc, atStartLoc, CategoryNameLoc);
   1129 }
   1130 
   1131 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
   1132                                                                unsigned ID) {
   1133   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
   1134   return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
   1135                                         SourceLocation(), SourceLocation());
   1136 }
   1137 
   1138 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
   1139   // The class interface might be NULL if we are working with invalid code.
   1140   if (const ObjCInterfaceDecl *ID = getClassInterface())
   1141     return ID->FindCategoryDeclaration(getIdentifier());
   1142   return 0;
   1143 }
   1144 
   1145 
   1146 void ObjCImplDecl::anchor() { }
   1147 
   1148 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
   1149   // FIXME: The context should be correct before we get here.
   1150   property->setLexicalDeclContext(this);
   1151   addDecl(property);
   1152 }
   1153 
   1154 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
   1155   ASTContext &Ctx = getASTContext();
   1156 
   1157   if (ObjCImplementationDecl *ImplD
   1158         = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
   1159     if (IFace)
   1160       Ctx.setObjCImplementation(IFace, ImplD);
   1161 
   1162   } else if (ObjCCategoryImplDecl *ImplD =
   1163              dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
   1164     if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
   1165       Ctx.setObjCImplementation(CD, ImplD);
   1166   }
   1167 
   1168   ClassInterface = IFace;
   1169 }
   1170 
   1171 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
   1172 /// properties implemented in this category @implementation block and returns
   1173 /// the implemented property that uses it.
   1174 ///
   1175 ObjCPropertyImplDecl *ObjCImplDecl::
   1176 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
   1177   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
   1178     ObjCPropertyImplDecl *PID = *i;
   1179     if (PID->getPropertyIvarDecl() &&
   1180         PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
   1181       return PID;
   1182   }
   1183   return 0;
   1184 }
   1185 
   1186 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
   1187 /// added to the list of those properties @synthesized/@dynamic in this
   1188 /// category @implementation block.
   1189 ///
   1190 ObjCPropertyImplDecl *ObjCImplDecl::
   1191 FindPropertyImplDecl(IdentifierInfo *Id) const {
   1192   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
   1193     ObjCPropertyImplDecl *PID = *i;
   1194     if (PID->getPropertyDecl()->getIdentifier() == Id)
   1195       return PID;
   1196   }
   1197   return 0;
   1198 }
   1199 
   1200 raw_ostream &clang::operator<<(raw_ostream &OS,
   1201                                const ObjCCategoryImplDecl &CID) {
   1202   OS << CID.getName();
   1203   return OS;
   1204 }
   1205 
   1206 //===----------------------------------------------------------------------===//
   1207 // ObjCImplementationDecl
   1208 //===----------------------------------------------------------------------===//
   1209 
   1210 void ObjCImplementationDecl::anchor() { }
   1211 
   1212 ObjCImplementationDecl *
   1213 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
   1214                                ObjCInterfaceDecl *ClassInterface,
   1215                                ObjCInterfaceDecl *SuperDecl,
   1216                                SourceLocation nameLoc,
   1217                                SourceLocation atStartLoc,
   1218                                SourceLocation IvarLBraceLoc,
   1219                                SourceLocation IvarRBraceLoc) {
   1220   if (ClassInterface && ClassInterface->hasDefinition())
   1221     ClassInterface = ClassInterface->getDefinition();
   1222   return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
   1223                                         nameLoc, atStartLoc,
   1224                                         IvarLBraceLoc, IvarRBraceLoc);
   1225 }
   1226 
   1227 ObjCImplementationDecl *
   1228 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
   1229   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
   1230   return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
   1231                                           SourceLocation());
   1232 }
   1233 
   1234 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
   1235                                              CXXCtorInitializer ** initializers,
   1236                                                  unsigned numInitializers) {
   1237   if (numInitializers > 0) {
   1238     NumIvarInitializers = numInitializers;
   1239     CXXCtorInitializer **ivarInitializers =
   1240     new (C) CXXCtorInitializer*[NumIvarInitializers];
   1241     memcpy(ivarInitializers, initializers,
   1242            numInitializers * sizeof(CXXCtorInitializer*));
   1243     IvarInitializers = ivarInitializers;
   1244   }
   1245 }
   1246 
   1247 raw_ostream &clang::operator<<(raw_ostream &OS,
   1248                                const ObjCImplementationDecl &ID) {
   1249   OS << ID.getName();
   1250   return OS;
   1251 }
   1252 
   1253 //===----------------------------------------------------------------------===//
   1254 // ObjCCompatibleAliasDecl
   1255 //===----------------------------------------------------------------------===//
   1256 
   1257 void ObjCCompatibleAliasDecl::anchor() { }
   1258 
   1259 ObjCCompatibleAliasDecl *
   1260 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
   1261                                 SourceLocation L,
   1262                                 IdentifierInfo *Id,
   1263                                 ObjCInterfaceDecl* AliasedClass) {
   1264   return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
   1265 }
   1266 
   1267 ObjCCompatibleAliasDecl *
   1268 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
   1269   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
   1270   return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
   1271 }
   1272 
   1273 //===----------------------------------------------------------------------===//
   1274 // ObjCPropertyDecl
   1275 //===----------------------------------------------------------------------===//
   1276 
   1277 void ObjCPropertyDecl::anchor() { }
   1278 
   1279 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
   1280                                            SourceLocation L,
   1281                                            IdentifierInfo *Id,
   1282                                            SourceLocation AtLoc,
   1283                                            SourceLocation LParenLoc,
   1284                                            TypeSourceInfo *T,
   1285                                            PropertyControl propControl) {
   1286   return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
   1287 }
   1288 
   1289 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
   1290                                                        unsigned ID) {
   1291   void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
   1292   return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
   1293                                     SourceLocation(),
   1294                                     0);
   1295 }
   1296 
   1297 //===----------------------------------------------------------------------===//
   1298 // ObjCPropertyImplDecl
   1299 //===----------------------------------------------------------------------===//
   1300 
   1301 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
   1302                                                    DeclContext *DC,
   1303                                                    SourceLocation atLoc,
   1304                                                    SourceLocation L,
   1305                                                    ObjCPropertyDecl *property,
   1306                                                    Kind PK,
   1307                                                    ObjCIvarDecl *ivar,
   1308                                                    SourceLocation ivarLoc) {
   1309   return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
   1310                                       ivarLoc);
   1311 }
   1312 
   1313 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
   1314                                                                unsigned ID) {
   1315   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
   1316   return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
   1317                                         0, Dynamic, 0, SourceLocation());
   1318 }
   1319 
   1320 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
   1321   SourceLocation EndLoc = getLocation();
   1322   if (IvarLoc.isValid())
   1323     EndLoc = IvarLoc;
   1324 
   1325   return SourceRange(AtLoc, EndLoc);
   1326 }
   1327