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 noCategoryLookup) 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     const ObjCList<ObjCProtocolDecl> &Protocols =
338       ClassDecl->getReferencedProtocols();
339     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
340          E = Protocols.end(); I != E; ++I)
341       if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
342         return MethodDecl;
343     if (!noCategoryLookup) {
344       // Didn't find one yet - now look through categories.
345       ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
346       while (CatDecl) {
347         if ((MethodDecl = CatDecl->getMethod(Sel, isInstance)))
348           return MethodDecl;
349 
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         CatDecl = CatDecl->getNextClassCategory();
358       }
359     }
360     ClassDecl = ClassDecl->getSuperClass();
361   }
362   return NULL;
363 }
364 
365 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
366                                    const Selector &Sel,
367                                    bool Instance) {
368   // FIXME: Should make sure no callers ever do this.
369   if (!hasDefinition())
370     return 0;
371 
372   if (data().ExternallyCompleted)
373     LoadExternalDefinition();
374 
375   ObjCMethodDecl *Method = 0;
376   if (ObjCImplementationDecl *ImpDecl = getImplementation())
377     Method = Instance ? ImpDecl->getInstanceMethod(Sel)
378                       : ImpDecl->getClassMethod(Sel);
379 
380   if (!Method && getSuperClass())
381     return getSuperClass()->lookupPrivateMethod(Sel, Instance);
382   return Method;
383 }
384 
385 //===----------------------------------------------------------------------===//
386 // ObjCMethodDecl
387 //===----------------------------------------------------------------------===//
388 
389 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
390                                        SourceLocation beginLoc,
391                                        SourceLocation endLoc,
392                                        Selector SelInfo, QualType T,
393                                        TypeSourceInfo *ResultTInfo,
394                                        DeclContext *contextDecl,
395                                        bool isInstance,
396                                        bool isVariadic,
397                                        bool isSynthesized,
398                                        bool isImplicitlyDeclared,
399                                        bool isDefined,
400                                        ImplementationControl impControl,
401                                        bool HasRelatedResultType) {
402   return new (C) ObjCMethodDecl(beginLoc, endLoc,
403                                 SelInfo, T, ResultTInfo, contextDecl,
404                                 isInstance,
405                                 isVariadic, isSynthesized, isImplicitlyDeclared,
406                                 isDefined,
407                                 impControl,
408                                 HasRelatedResultType);
409 }
410 
411 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
412   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
413   return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
414                                   Selector(), QualType(), 0, 0);
415 }
416 
417 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
418   assert(PrevMethod);
419   getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
420   IsRedeclaration = true;
421   PrevMethod->HasRedeclaration = true;
422 }
423 
424 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
425                                          ArrayRef<ParmVarDecl*> Params,
426                                          ArrayRef<SourceLocation> SelLocs) {
427   ParamsAndSelLocs = 0;
428   NumParams = Params.size();
429   if (Params.empty() && SelLocs.empty())
430     return;
431 
432   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
433                   sizeof(SourceLocation) * SelLocs.size();
434   ParamsAndSelLocs = C.Allocate(Size);
435   std::copy(Params.begin(), Params.end(), getParams());
436   std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
437 }
438 
439 void ObjCMethodDecl::getSelectorLocs(
440                                SmallVectorImpl<SourceLocation> &SelLocs) const {
441   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
442     SelLocs.push_back(getSelectorLoc(i));
443 }
444 
445 void ObjCMethodDecl::setMethodParams(ASTContext &C,
446                                      ArrayRef<ParmVarDecl*> Params,
447                                      ArrayRef<SourceLocation> SelLocs) {
448   assert((!SelLocs.empty() || isImplicit()) &&
449          "No selector locs for non-implicit method");
450   if (isImplicit())
451     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
452 
453   SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params, EndLoc);
454   if (SelLocsKind != SelLoc_NonStandard)
455     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
456 
457   setParamsAndSelLocs(C, Params, SelLocs);
458 }
459 
460 /// \brief A definition will return its interface declaration.
461 /// An interface declaration will return its definition.
462 /// Otherwise it will return itself.
463 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
464   ASTContext &Ctx = getASTContext();
465   ObjCMethodDecl *Redecl = 0;
466   if (HasRedeclaration)
467     Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
468   if (Redecl)
469     return Redecl;
470 
471   Decl *CtxD = cast<Decl>(getDeclContext());
472 
473   if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
474     if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
475       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
476 
477   } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
478     if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
479       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
480 
481   } else if (ObjCImplementationDecl *ImplD =
482                dyn_cast<ObjCImplementationDecl>(CtxD)) {
483     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
484       Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
485 
486   } else if (ObjCCategoryImplDecl *CImplD =
487                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
488     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
489       Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
490   }
491 
492   if (!Redecl && isRedeclaration()) {
493     // This is the last redeclaration, go back to the first method.
494     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
495                                                     isInstanceMethod());
496   }
497 
498   return Redecl ? Redecl : this;
499 }
500 
501 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
502   Decl *CtxD = cast<Decl>(getDeclContext());
503 
504   if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
505     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
506       if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
507                                               isInstanceMethod()))
508         return MD;
509 
510   } else if (ObjCCategoryImplDecl *CImplD =
511                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
512     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
513       if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
514                                                isInstanceMethod()))
515         return MD;
516   }
517 
518   if (isRedeclaration())
519     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
520                                                     isInstanceMethod());
521 
522   return this;
523 }
524 
525 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
526   ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
527   if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
528     return family;
529 
530   // Check for an explicit attribute.
531   if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
532     // The unfortunate necessity of mapping between enums here is due
533     // to the attributes framework.
534     switch (attr->getFamily()) {
535     case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
536     case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
537     case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
538     case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
539     case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
540     case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
541     }
542     Family = static_cast<unsigned>(family);
543     return family;
544   }
545 
546   family = getSelector().getMethodFamily();
547   switch (family) {
548   case OMF_None: break;
549 
550   // init only has a conventional meaning for an instance method, and
551   // it has to return an object.
552   case OMF_init:
553     if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
554       family = OMF_None;
555     break;
556 
557   // alloc/copy/new have a conventional meaning for both class and
558   // instance methods, but they require an object return.
559   case OMF_alloc:
560   case OMF_copy:
561   case OMF_mutableCopy:
562   case OMF_new:
563     if (!getResultType()->isObjCObjectPointerType())
564       family = OMF_None;
565     break;
566 
567   // These selectors have a conventional meaning only for instance methods.
568   case OMF_dealloc:
569   case OMF_finalize:
570   case OMF_retain:
571   case OMF_release:
572   case OMF_autorelease:
573   case OMF_retainCount:
574   case OMF_self:
575     if (!isInstanceMethod())
576       family = OMF_None;
577     break;
578 
579   case OMF_performSelector:
580     if (!isInstanceMethod() ||
581         !getResultType()->isObjCIdType())
582       family = OMF_None;
583     else {
584       unsigned noParams = param_size();
585       if (noParams < 1 || noParams > 3)
586         family = OMF_None;
587       else {
588         ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
589         QualType ArgT = (*it);
590         if (!ArgT->isObjCSelType()) {
591           family = OMF_None;
592           break;
593         }
594         while (--noParams) {
595           it++;
596           ArgT = (*it);
597           if (!ArgT->isObjCIdType()) {
598             family = OMF_None;
599             break;
600           }
601         }
602       }
603     }
604     break;
605 
606   }
607 
608   // Cache the result.
609   Family = static_cast<unsigned>(family);
610   return family;
611 }
612 
613 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
614                                           const ObjCInterfaceDecl *OID) {
615   QualType selfTy;
616   if (isInstanceMethod()) {
617     // There may be no interface context due to error in declaration
618     // of the interface (which has been reported). Recover gracefully.
619     if (OID) {
620       selfTy = Context.getObjCInterfaceType(OID);
621       selfTy = Context.getObjCObjectPointerType(selfTy);
622     } else {
623       selfTy = Context.getObjCIdType();
624     }
625   } else // we have a factory method.
626     selfTy = Context.getObjCClassType();
627 
628   bool selfIsPseudoStrong = false;
629   bool selfIsConsumed = false;
630 
631   if (Context.getLangOptions().ObjCAutoRefCount) {
632     if (isInstanceMethod()) {
633       selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
634 
635       // 'self' is always __strong.  It's actually pseudo-strong except
636       // in init methods (or methods labeled ns_consumes_self), though.
637       Qualifiers qs;
638       qs.setObjCLifetime(Qualifiers::OCL_Strong);
639       selfTy = Context.getQualifiedType(selfTy, qs);
640 
641       // In addition, 'self' is const unless this is an init method.
642       if (getMethodFamily() != OMF_init && !selfIsConsumed) {
643         selfTy = selfTy.withConst();
644         selfIsPseudoStrong = true;
645       }
646     }
647     else {
648       assert(isClassMethod());
649       // 'self' is always const in class methods.
650       selfTy = selfTy.withConst();
651       selfIsPseudoStrong = true;
652     }
653   }
654 
655   ImplicitParamDecl *self
656     = ImplicitParamDecl::Create(Context, this, SourceLocation(),
657                                 &Context.Idents.get("self"), selfTy);
658   setSelfDecl(self);
659 
660   if (selfIsConsumed)
661     self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
662 
663   if (selfIsPseudoStrong)
664     self->setARCPseudoStrong(true);
665 
666   setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
667                                        &Context.Idents.get("_cmd"),
668                                        Context.getObjCSelType()));
669 }
670 
671 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
672   if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
673     return ID;
674   if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
675     return CD->getClassInterface();
676   if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
677     return IMD->getClassInterface();
678 
679   assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
680   llvm_unreachable("unknown method context");
681 }
682 
683 //===----------------------------------------------------------------------===//
684 // ObjCInterfaceDecl
685 //===----------------------------------------------------------------------===//
686 
687 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
688                                              DeclContext *DC,
689                                              SourceLocation atLoc,
690                                              IdentifierInfo *Id,
691                                              ObjCInterfaceDecl *PrevDecl,
692                                              SourceLocation ClassLoc,
693                                              bool isInternal){
694   ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
695                                                         PrevDecl, isInternal);
696   C.getObjCInterfaceType(Result, PrevDecl);
697   return Result;
698 }
699 
700 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
701                                                          unsigned ID) {
702   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
703   return new (Mem) ObjCInterfaceDecl(0, SourceLocation(), 0, SourceLocation(),
704                                      0, false);
705 }
706 
707 ObjCInterfaceDecl::
708 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
709                   SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
710                   bool isInternal)
711   : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
712     TypeForDecl(0), Data()
713 {
714   setPreviousDeclaration(PrevDecl);
715 
716   // Copy the 'data' pointer over.
717   if (PrevDecl)
718     Data = PrevDecl->Data;
719 
720   setImplicit(isInternal);
721 }
722 
723 void ObjCInterfaceDecl::LoadExternalDefinition() const {
724   assert(data().ExternallyCompleted && "Class is not externally completed");
725   data().ExternallyCompleted = false;
726   getASTContext().getExternalSource()->CompleteType(
727                                         const_cast<ObjCInterfaceDecl *>(this));
728 }
729 
730 void ObjCInterfaceDecl::setExternallyCompleted() {
731   assert(getASTContext().getExternalSource() &&
732          "Class can't be externally completed without an external source");
733   assert(hasDefinition() &&
734          "Forward declarations can't be externally completed");
735   data().ExternallyCompleted = true;
736 }
737 
738 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
739   if (const ObjCInterfaceDecl *Def = getDefinition()) {
740     if (data().ExternallyCompleted)
741       LoadExternalDefinition();
742 
743     return getASTContext().getObjCImplementation(
744              const_cast<ObjCInterfaceDecl*>(Def));
745   }
746 
747   // FIXME: Should make sure no callers ever do this.
748   return 0;
749 }
750 
751 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
752   getASTContext().setObjCImplementation(getDefinition(), ImplD);
753 }
754 
755 /// all_declared_ivar_begin - return first ivar declared in this class,
756 /// its extensions and its implementation. Lazily build the list on first
757 /// access.
758 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
759   // FIXME: Should make sure no callers ever do this.
760   if (!hasDefinition())
761     return 0;
762 
763   if (data().IvarList)
764     return data().IvarList;
765 
766   ObjCIvarDecl *curIvar = 0;
767   if (!ivar_empty()) {
768     ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
769     data().IvarList = (*I); ++I;
770     for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
771       curIvar->setNextIvar(*I);
772   }
773 
774   for (const ObjCCategoryDecl *CDecl = getFirstClassExtension(); CDecl;
775        CDecl = CDecl->getNextClassExtension()) {
776     if (!CDecl->ivar_empty()) {
777       ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
778                                           E = CDecl->ivar_end();
779       if (!data().IvarList) {
780         data().IvarList = (*I); ++I;
781         curIvar = data().IvarList;
782       }
783       for ( ;I != E; curIvar = *I, ++I)
784         curIvar->setNextIvar(*I);
785     }
786   }
787 
788   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
789     if (!ImplDecl->ivar_empty()) {
790       ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
791                                             E = ImplDecl->ivar_end();
792       if (!data().IvarList) {
793         data().IvarList = (*I); ++I;
794         curIvar = data().IvarList;
795       }
796       for ( ;I != E; curIvar = *I, ++I)
797         curIvar->setNextIvar(*I);
798     }
799   }
800   return data().IvarList;
801 }
802 
803 /// FindCategoryDeclaration - Finds category declaration in the list of
804 /// categories for this class and returns it. Name of the category is passed
805 /// in 'CategoryId'. If category not found, return 0;
806 ///
807 ObjCCategoryDecl *
808 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
809   if (data().ExternallyCompleted)
810     LoadExternalDefinition();
811 
812   for (ObjCCategoryDecl *Category = getCategoryList();
813        Category; Category = Category->getNextClassCategory())
814     if (Category->getIdentifier() == CategoryId)
815       return Category;
816   return 0;
817 }
818 
819 ObjCMethodDecl *
820 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
821   for (ObjCCategoryDecl *Category = getCategoryList();
822        Category; Category = Category->getNextClassCategory())
823     if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
824       if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
825         return MD;
826   return 0;
827 }
828 
829 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
830   for (ObjCCategoryDecl *Category = getCategoryList();
831        Category; Category = Category->getNextClassCategory())
832     if (ObjCCategoryImplDecl *Impl = Category->getImplementation())
833       if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
834         return MD;
835   return 0;
836 }
837 
838 /// ClassImplementsProtocol - Checks that 'lProto' protocol
839 /// has been implemented in IDecl class, its super class or categories (if
840 /// lookupCategory is true).
841 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
842                                     bool lookupCategory,
843                                     bool RHSIsQualifiedID) {
844   if (!hasDefinition())
845     return false;
846 
847   ObjCInterfaceDecl *IDecl = this;
848   // 1st, look up the class.
849   const ObjCList<ObjCProtocolDecl> &Protocols =
850   IDecl->getReferencedProtocols();
851 
852   for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
853        E = Protocols.end(); PI != E; ++PI) {
854     if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
855       return true;
856     // This is dubious and is added to be compatible with gcc.  In gcc, it is
857     // also allowed assigning a protocol-qualified 'id' type to a LHS object
858     // when protocol in qualified LHS is in list of protocols in the rhs 'id'
859     // object. This IMO, should be a bug.
860     // FIXME: Treat this as an extension, and flag this as an error when GCC
861     // extensions are not enabled.
862     if (RHSIsQualifiedID &&
863         getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
864       return true;
865   }
866 
867   // 2nd, look up the category.
868   if (lookupCategory)
869     for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
870          CDecl = CDecl->getNextClassCategory()) {
871       for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
872            E = CDecl->protocol_end(); PI != E; ++PI)
873         if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
874           return true;
875     }
876 
877   // 3rd, look up the super class(s)
878   if (IDecl->getSuperClass())
879     return
880   IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
881                                                   RHSIsQualifiedID);
882 
883   return false;
884 }
885 
886 //===----------------------------------------------------------------------===//
887 // ObjCIvarDecl
888 //===----------------------------------------------------------------------===//
889 
890 void ObjCIvarDecl::anchor() { }
891 
892 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
893                                    SourceLocation StartLoc,
894                                    SourceLocation IdLoc, IdentifierInfo *Id,
895                                    QualType T, TypeSourceInfo *TInfo,
896                                    AccessControl ac, Expr *BW,
897                                    bool synthesized) {
898   if (DC) {
899     // Ivar's can only appear in interfaces, implementations (via synthesized
900     // properties), and class extensions (via direct declaration, or synthesized
901     // properties).
902     //
903     // FIXME: This should really be asserting this:
904     //   (isa<ObjCCategoryDecl>(DC) &&
905     //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
906     // but unfortunately we sometimes place ivars into non-class extension
907     // categories on error. This breaks an AST invariant, and should not be
908     // fixed.
909     assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
910             isa<ObjCCategoryDecl>(DC)) &&
911            "Invalid ivar decl context!");
912     // Once a new ivar is created in any of class/class-extension/implementation
913     // decl contexts, the previously built IvarList must be rebuilt.
914     ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
915     if (!ID) {
916       if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC)) {
917         ID = IM->getClassInterface();
918         if (BW)
919           IM->setHasSynthBitfield(true);
920       } else {
921         ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
922         ID = CD->getClassInterface();
923         if (BW)
924           CD->setHasSynthBitfield(true);
925       }
926     }
927     ID->setIvarList(0);
928   }
929 
930   return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
931                               ac, BW, synthesized);
932 }
933 
934 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
935   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
936   return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
937                                 QualType(), 0, ObjCIvarDecl::None, 0, false);
938 }
939 
940 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
941   const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
942 
943   switch (DC->getKind()) {
944   default:
945   case ObjCCategoryImpl:
946   case ObjCProtocol:
947     llvm_unreachable("invalid ivar container!");
948 
949     // Ivars can only appear in class extension categories.
950   case ObjCCategory: {
951     const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
952     assert(CD->IsClassExtension() && "invalid container for ivar!");
953     return CD->getClassInterface();
954   }
955 
956   case ObjCImplementation:
957     return cast<ObjCImplementationDecl>(DC)->getClassInterface();
958 
959   case ObjCInterface:
960     return cast<ObjCInterfaceDecl>(DC);
961   }
962 }
963 
964 //===----------------------------------------------------------------------===//
965 // ObjCAtDefsFieldDecl
966 //===----------------------------------------------------------------------===//
967 
968 void ObjCAtDefsFieldDecl::anchor() { }
969 
970 ObjCAtDefsFieldDecl
971 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
972                              SourceLocation StartLoc,  SourceLocation IdLoc,
973                              IdentifierInfo *Id, QualType T, Expr *BW) {
974   return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
975 }
976 
977 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
978                                                              unsigned ID) {
979   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
980   return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
981                                        0, QualType(), 0);
982 }
983 
984 //===----------------------------------------------------------------------===//
985 // ObjCProtocolDecl
986 //===----------------------------------------------------------------------===//
987 
988 void ObjCProtocolDecl::anchor() { }
989 
990 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
991                                    SourceLocation nameLoc,
992                                    SourceLocation atStartLoc,
993                                    ObjCProtocolDecl *PrevDecl)
994   : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
995 {
996   setPreviousDeclaration(PrevDecl);
997   if (PrevDecl)
998     Data = PrevDecl->Data;
999 }
1000 
1001 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1002                                            IdentifierInfo *Id,
1003                                            SourceLocation nameLoc,
1004                                            SourceLocation atStartLoc,
1005                                            ObjCProtocolDecl *PrevDecl) {
1006   ObjCProtocolDecl *Result
1007     = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1008 
1009   return Result;
1010 }
1011 
1012 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1013                                                        unsigned ID) {
1014   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1015   return new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(), SourceLocation(),
1016                                     0);
1017 }
1018 
1019 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1020   ObjCProtocolDecl *PDecl = this;
1021 
1022   if (Name == getIdentifier())
1023     return PDecl;
1024 
1025   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1026     if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1027       return PDecl;
1028 
1029   return NULL;
1030 }
1031 
1032 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1033 // it inherited.
1034 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1035                                                bool isInstance) const {
1036   ObjCMethodDecl *MethodDecl = NULL;
1037 
1038   if ((MethodDecl = getMethod(Sel, isInstance)))
1039     return MethodDecl;
1040 
1041   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1042     if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1043       return MethodDecl;
1044   return NULL;
1045 }
1046 
1047 void ObjCProtocolDecl::allocateDefinitionData() {
1048   assert(!Data && "Protocol already has a definition!");
1049   Data = new (getASTContext()) DefinitionData;
1050   Data->Definition = this;
1051 }
1052 
1053 void ObjCProtocolDecl::startDefinition() {
1054   allocateDefinitionData();
1055 
1056   // Update all of the declarations with a pointer to the definition.
1057   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1058        RD != RDEnd; ++RD)
1059     RD->Data = this->Data;
1060 }
1061 
1062 //===----------------------------------------------------------------------===//
1063 // ObjCCategoryDecl
1064 //===----------------------------------------------------------------------===//
1065 
1066 void ObjCCategoryDecl::anchor() { }
1067 
1068 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1069                                            SourceLocation AtLoc,
1070                                            SourceLocation ClassNameLoc,
1071                                            SourceLocation CategoryNameLoc,
1072                                            IdentifierInfo *Id,
1073                                            ObjCInterfaceDecl *IDecl,
1074                                            SourceLocation IvarLBraceLoc,
1075                                            SourceLocation IvarRBraceLoc) {
1076   ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1077                                                        CategoryNameLoc, Id,
1078                                                        IDecl,
1079                                                        IvarLBraceLoc, IvarRBraceLoc);
1080   if (IDecl) {
1081     // Link this category into its class's category list.
1082     CatDecl->NextClassCategory = IDecl->getCategoryList();
1083     if (IDecl->hasDefinition()) {
1084       IDecl->setCategoryList(CatDecl);
1085       if (ASTMutationListener *L = C.getASTMutationListener())
1086         L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1087     }
1088   }
1089 
1090   return CatDecl;
1091 }
1092 
1093 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1094                                                        unsigned ID) {
1095   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1096   return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1097                                     SourceLocation(), 0, 0);
1098 }
1099 
1100 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1101   return getASTContext().getObjCImplementation(
1102                                            const_cast<ObjCCategoryDecl*>(this));
1103 }
1104 
1105 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1106   getASTContext().setObjCImplementation(this, ImplD);
1107 }
1108 
1109 
1110 //===----------------------------------------------------------------------===//
1111 // ObjCCategoryImplDecl
1112 //===----------------------------------------------------------------------===//
1113 
1114 void ObjCCategoryImplDecl::anchor() { }
1115 
1116 ObjCCategoryImplDecl *
1117 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1118                              IdentifierInfo *Id,
1119                              ObjCInterfaceDecl *ClassInterface,
1120                              SourceLocation nameLoc,
1121                              SourceLocation atStartLoc,
1122                              SourceLocation CategoryNameLoc) {
1123   if (ClassInterface && ClassInterface->hasDefinition())
1124     ClassInterface = ClassInterface->getDefinition();
1125   return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1126                                       nameLoc, atStartLoc, CategoryNameLoc);
1127 }
1128 
1129 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1130                                                                unsigned ID) {
1131   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1132   return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1133                                         SourceLocation(), SourceLocation());
1134 }
1135 
1136 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1137   // The class interface might be NULL if we are working with invalid code.
1138   if (const ObjCInterfaceDecl *ID = getClassInterface())
1139     return ID->FindCategoryDeclaration(getIdentifier());
1140   return 0;
1141 }
1142 
1143 
1144 void ObjCImplDecl::anchor() { }
1145 
1146 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1147   // FIXME: The context should be correct before we get here.
1148   property->setLexicalDeclContext(this);
1149   addDecl(property);
1150 }
1151 
1152 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1153   ASTContext &Ctx = getASTContext();
1154 
1155   if (ObjCImplementationDecl *ImplD
1156         = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1157     if (IFace)
1158       Ctx.setObjCImplementation(IFace, ImplD);
1159 
1160   } else if (ObjCCategoryImplDecl *ImplD =
1161              dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1162     if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1163       Ctx.setObjCImplementation(CD, ImplD);
1164   }
1165 
1166   ClassInterface = IFace;
1167 }
1168 
1169 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1170 /// properties implemented in this category @implementation block and returns
1171 /// the implemented property that uses it.
1172 ///
1173 ObjCPropertyImplDecl *ObjCImplDecl::
1174 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1175   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1176     ObjCPropertyImplDecl *PID = *i;
1177     if (PID->getPropertyIvarDecl() &&
1178         PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1179       return PID;
1180   }
1181   return 0;
1182 }
1183 
1184 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1185 /// added to the list of those properties @synthesized/@dynamic in this
1186 /// category @implementation block.
1187 ///
1188 ObjCPropertyImplDecl *ObjCImplDecl::
1189 FindPropertyImplDecl(IdentifierInfo *Id) const {
1190   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1191     ObjCPropertyImplDecl *PID = *i;
1192     if (PID->getPropertyDecl()->getIdentifier() == Id)
1193       return PID;
1194   }
1195   return 0;
1196 }
1197 
1198 raw_ostream &clang::operator<<(raw_ostream &OS,
1199                                const ObjCCategoryImplDecl &CID) {
1200   OS << CID.getName();
1201   return OS;
1202 }
1203 
1204 //===----------------------------------------------------------------------===//
1205 // ObjCImplementationDecl
1206 //===----------------------------------------------------------------------===//
1207 
1208 void ObjCImplementationDecl::anchor() { }
1209 
1210 ObjCImplementationDecl *
1211 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1212                                ObjCInterfaceDecl *ClassInterface,
1213                                ObjCInterfaceDecl *SuperDecl,
1214                                SourceLocation nameLoc,
1215                                SourceLocation atStartLoc,
1216                                SourceLocation IvarLBraceLoc,
1217                                SourceLocation IvarRBraceLoc) {
1218   if (ClassInterface && ClassInterface->hasDefinition())
1219     ClassInterface = ClassInterface->getDefinition();
1220   return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1221                                         nameLoc, atStartLoc,
1222                                         IvarLBraceLoc, IvarRBraceLoc);
1223 }
1224 
1225 ObjCImplementationDecl *
1226 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1227   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1228   return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1229                                           SourceLocation());
1230 }
1231 
1232 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1233                                              CXXCtorInitializer ** initializers,
1234                                                  unsigned numInitializers) {
1235   if (numInitializers > 0) {
1236     NumIvarInitializers = numInitializers;
1237     CXXCtorInitializer **ivarInitializers =
1238     new (C) CXXCtorInitializer*[NumIvarInitializers];
1239     memcpy(ivarInitializers, initializers,
1240            numInitializers * sizeof(CXXCtorInitializer*));
1241     IvarInitializers = ivarInitializers;
1242   }
1243 }
1244 
1245 raw_ostream &clang::operator<<(raw_ostream &OS,
1246                                const ObjCImplementationDecl &ID) {
1247   OS << ID.getName();
1248   return OS;
1249 }
1250 
1251 //===----------------------------------------------------------------------===//
1252 // ObjCCompatibleAliasDecl
1253 //===----------------------------------------------------------------------===//
1254 
1255 void ObjCCompatibleAliasDecl::anchor() { }
1256 
1257 ObjCCompatibleAliasDecl *
1258 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1259                                 SourceLocation L,
1260                                 IdentifierInfo *Id,
1261                                 ObjCInterfaceDecl* AliasedClass) {
1262   return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1263 }
1264 
1265 ObjCCompatibleAliasDecl *
1266 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1267   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1268   return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1269 }
1270 
1271 //===----------------------------------------------------------------------===//
1272 // ObjCPropertyDecl
1273 //===----------------------------------------------------------------------===//
1274 
1275 void ObjCPropertyDecl::anchor() { }
1276 
1277 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1278                                            SourceLocation L,
1279                                            IdentifierInfo *Id,
1280                                            SourceLocation AtLoc,
1281                                            TypeSourceInfo *T,
1282                                            PropertyControl propControl) {
1283   return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, T);
1284 }
1285 
1286 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1287                                                        unsigned ID) {
1288   void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1289   return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1290                                     0);
1291 }
1292 
1293 //===----------------------------------------------------------------------===//
1294 // ObjCPropertyImplDecl
1295 //===----------------------------------------------------------------------===//
1296 
1297 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1298                                                    DeclContext *DC,
1299                                                    SourceLocation atLoc,
1300                                                    SourceLocation L,
1301                                                    ObjCPropertyDecl *property,
1302                                                    Kind PK,
1303                                                    ObjCIvarDecl *ivar,
1304                                                    SourceLocation ivarLoc) {
1305   return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1306                                       ivarLoc);
1307 }
1308 
1309 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1310                                                                unsigned ID) {
1311   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1312   return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1313                                         0, Dynamic, 0, SourceLocation());
1314 }
1315 
1316 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1317   SourceLocation EndLoc = getLocation();
1318   if (IvarLoc.isValid())
1319     EndLoc = IvarLoc;
1320 
1321   return SourceRange(AtLoc, EndLoc);
1322 }
1323