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