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