]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/lib/AST/DeclObjC.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / lib / AST / DeclObjC.cpp
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/ASTMutationListener.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/Stmt.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 using namespace clang;
22
23 //===----------------------------------------------------------------------===//
24 // ObjCListBase
25 //===----------------------------------------------------------------------===//
26
27 void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
28   List = 0;
29   if (Elts == 0) return;  // Setting to an empty list is a noop.
30
31
32   List = new (Ctx) void*[Elts];
33   NumElts = Elts;
34   memcpy(List, InList, sizeof(void*)*Elts);
35 }
36
37 void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts, 
38                            const SourceLocation *Locs, ASTContext &Ctx) {
39   if (Elts == 0)
40     return;
41
42   Locations = new (Ctx) SourceLocation[Elts];
43   memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
44   set(InList, Elts, Ctx);
45 }
46
47 //===----------------------------------------------------------------------===//
48 // ObjCInterfaceDecl
49 //===----------------------------------------------------------------------===//
50
51 void ObjCContainerDecl::anchor() { }
52
53 /// getIvarDecl - This method looks up an ivar in this ContextDecl.
54 ///
55 ObjCIvarDecl *
56 ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
57   lookup_const_result R = lookup(Id);
58   for (lookup_const_iterator Ivar = R.begin(), IvarEnd = R.end();
59        Ivar != IvarEnd; ++Ivar) {
60     if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
61       return ivar;
62   }
63   return 0;
64 }
65
66 // Get the local instance/class method declared in this interface.
67 ObjCMethodDecl *
68 ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
69                              bool AllowHidden) const {
70   // If this context is a hidden protocol definition, don't find any
71   // methods there.
72   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
73     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
74       if (Def->isHidden() && !AllowHidden)
75         return 0;
76   }
77
78   // Since instance & class methods can have the same name, the loop below
79   // ensures we get the correct method.
80   //
81   // @interface Whatever
82   // - (int) class_method;
83   // + (float) class_method;
84   // @end
85   //
86   lookup_const_result R = lookup(Sel);
87   for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
88        Meth != MethEnd; ++Meth) {
89     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
90     if (MD && MD->isInstanceMethod() == isInstance)
91       return MD;
92   }
93   return 0;
94 }
95
96 /// HasUserDeclaredSetterMethod - This routine returns 'true' if a user declared setter
97 /// method was found in the class, its protocols, its super classes or categories.
98 /// It also returns 'true' if one of its categories has declared a 'readwrite' property.
99 /// This is because, user must provide a setter method for the category's 'readwrite'
100 /// property.
101 bool
102 ObjCContainerDecl::HasUserDeclaredSetterMethod(const ObjCPropertyDecl *Property) const {
103   Selector Sel = Property->getSetterName();
104   lookup_const_result R = lookup(Sel);
105   for (lookup_const_iterator Meth = R.begin(), MethEnd = R.end();
106        Meth != MethEnd; ++Meth) {
107     ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
108     if (MD && MD->isInstanceMethod() && !MD->isImplicit())
109       return true;
110   }
111
112   if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
113     // Also look into categories, including class extensions, looking
114     // for a user declared instance method.
115     for (ObjCInterfaceDecl::visible_categories_iterator
116          Cat = ID->visible_categories_begin(),
117          CatEnd = ID->visible_categories_end();
118          Cat != CatEnd;
119          ++Cat) {
120       if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
121         if (!MD->isImplicit())
122           return true;
123       if (Cat->IsClassExtension())
124         continue;
125       // Also search through the categories looking for a 'readwrite' declaration
126       // of this property. If one found, presumably a setter will be provided
127       // (properties declared in categories will not get auto-synthesized).
128       for (ObjCContainerDecl::prop_iterator P = Cat->prop_begin(),
129            E = Cat->prop_end(); P != E; ++P)
130         if (P->getIdentifier() == Property->getIdentifier()) {
131           if (P->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite)
132             return true;
133           break;
134         }
135     }
136     
137     // Also look into protocols, for a user declared instance method.
138     for (ObjCInterfaceDecl::all_protocol_iterator P =
139          ID->all_referenced_protocol_begin(),
140          PE = ID->all_referenced_protocol_end(); P != PE; ++P) {
141       ObjCProtocolDecl *Proto = (*P);
142       if (Proto->HasUserDeclaredSetterMethod(Property))
143         return true;
144     }
145     // And in its super class.
146     ObjCInterfaceDecl *OSC = ID->getSuperClass();
147     while (OSC) {
148       if (OSC->HasUserDeclaredSetterMethod(Property))
149         return true;
150       OSC = OSC->getSuperClass();
151     }
152   }
153   if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(this))
154     for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
155          E = PD->protocol_end(); PI != E; ++PI) {
156       if ((*PI)->HasUserDeclaredSetterMethod(Property))
157         return true;
158     }
159   return false;
160 }
161
162 ObjCPropertyDecl *
163 ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
164                                    IdentifierInfo *propertyID) {
165   // If this context is a hidden protocol definition, don't find any
166   // property.
167   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
168     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
169       if (Def->isHidden())
170         return 0;
171   }
172
173   DeclContext::lookup_const_result R = DC->lookup(propertyID);
174   for (DeclContext::lookup_const_iterator I = R.begin(), E = R.end(); I != E;
175        ++I)
176     if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(*I))
177       return PD;
178
179   return 0;
180 }
181
182 IdentifierInfo *
183 ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
184   SmallString<128> ivarName;
185   {
186     llvm::raw_svector_ostream os(ivarName);
187     os << '_' << getIdentifier()->getName();
188   }
189   return &Ctx.Idents.get(ivarName.str());
190 }
191
192 /// FindPropertyDeclaration - Finds declaration of the property given its name
193 /// in 'PropertyId' and returns it. It returns 0, if not found.
194 ObjCPropertyDecl *
195 ObjCContainerDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
196   // Don't find properties within hidden protocol definitions.
197   if (const ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
198     if (const ObjCProtocolDecl *Def = Proto->getDefinition())
199       if (Def->isHidden())
200         return 0;
201   }
202
203   if (ObjCPropertyDecl *PD =
204         ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
205     return PD;
206
207   switch (getKind()) {
208     default:
209       break;
210     case Decl::ObjCProtocol: {
211       const ObjCProtocolDecl *PID = cast<ObjCProtocolDecl>(this);
212       for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
213            E = PID->protocol_end(); I != E; ++I)
214         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
215           return P;
216       break;
217     }
218     case Decl::ObjCInterface: {
219       const ObjCInterfaceDecl *OID = cast<ObjCInterfaceDecl>(this);
220       // Look through categories (but not extensions).
221       for (ObjCInterfaceDecl::visible_categories_iterator
222              Cat = OID->visible_categories_begin(),
223              CatEnd = OID->visible_categories_end();
224            Cat != CatEnd; ++Cat) {
225         if (!Cat->IsClassExtension())
226           if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(PropertyId))
227             return P;
228       }
229
230       // Look through protocols.
231       for (ObjCInterfaceDecl::all_protocol_iterator
232             I = OID->all_referenced_protocol_begin(),
233             E = OID->all_referenced_protocol_end(); I != E; ++I)
234         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
235           return P;
236
237       // Finally, check the super class.
238       if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
239         return superClass->FindPropertyDeclaration(PropertyId);
240       break;
241     }
242     case Decl::ObjCCategory: {
243       const ObjCCategoryDecl *OCD = cast<ObjCCategoryDecl>(this);
244       // Look through protocols.
245       if (!OCD->IsClassExtension())
246         for (ObjCCategoryDecl::protocol_iterator
247               I = OCD->protocol_begin(), E = OCD->protocol_end(); I != E; ++I)
248         if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
249           return P;
250
251       break;
252     }
253   }
254   return 0;
255 }
256
257 void ObjCInterfaceDecl::anchor() { }
258
259 /// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
260 /// with name 'PropertyId' in the primary class; including those in protocols
261 /// (direct or indirect) used by the primary class.
262 ///
263 ObjCPropertyDecl *
264 ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
265                                             IdentifierInfo *PropertyId) const {
266   // FIXME: Should make sure no callers ever do this.
267   if (!hasDefinition())
268     return 0;
269   
270   if (data().ExternallyCompleted)
271     LoadExternalDefinition();
272
273   if (ObjCPropertyDecl *PD =
274       ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId))
275     return PD;
276
277   // Look through protocols.
278   for (ObjCInterfaceDecl::all_protocol_iterator
279         I = all_referenced_protocol_begin(),
280         E = all_referenced_protocol_end(); I != E; ++I)
281     if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
282       return P;
283
284   return 0;
285 }
286
287 void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
288                                                      PropertyDeclOrder &PO) const {
289   for (ObjCContainerDecl::prop_iterator P = prop_begin(),
290       E = prop_end(); P != E; ++P) {
291     ObjCPropertyDecl *Prop = *P;
292     PM[Prop->getIdentifier()] = Prop;
293     PO.push_back(Prop);
294   }
295   for (ObjCInterfaceDecl::all_protocol_iterator
296       PI = all_referenced_protocol_begin(),
297       E = all_referenced_protocol_end(); PI != E; ++PI)
298     (*PI)->collectPropertiesToImplement(PM, PO);
299   // Note, the properties declared only in class extensions are still copied
300   // into the main @interface's property list, and therefore we don't
301   // explicitly, have to search class extension properties.
302 }
303
304 bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
305   const ObjCInterfaceDecl *Class = this;
306   while (Class) {
307     if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
308       return true;
309     Class = Class->getSuperClass();
310   }
311   return false;
312 }
313
314 const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
315   const ObjCInterfaceDecl *Class = this;
316   while (Class) {
317     if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
318       return Class;
319     Class = Class->getSuperClass();
320   }
321   return 0;
322 }
323
324 void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
325                               ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
326                               ASTContext &C)
327 {
328   if (data().ExternallyCompleted)
329     LoadExternalDefinition();
330
331   if (data().AllReferencedProtocols.empty() && 
332       data().ReferencedProtocols.empty()) {
333     data().AllReferencedProtocols.set(ExtList, ExtNum, C);
334     return;
335   }
336   
337   // Check for duplicate protocol in class's protocol list.
338   // This is O(n*m). But it is extremely rare and number of protocols in
339   // class or its extension are very few.
340   SmallVector<ObjCProtocolDecl*, 8> ProtocolRefs;
341   for (unsigned i = 0; i < ExtNum; i++) {
342     bool protocolExists = false;
343     ObjCProtocolDecl *ProtoInExtension = ExtList[i];
344     for (all_protocol_iterator
345           p = all_referenced_protocol_begin(),
346           e = all_referenced_protocol_end(); p != e; ++p) {
347       ObjCProtocolDecl *Proto = (*p);
348       if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
349         protocolExists = true;
350         break;
351       }      
352     }
353     // Do we want to warn on a protocol in extension class which
354     // already exist in the class? Probably not.
355     if (!protocolExists)
356       ProtocolRefs.push_back(ProtoInExtension);
357   }
358
359   if (ProtocolRefs.empty())
360     return;
361
362   // Merge ProtocolRefs into class's protocol list;
363   for (all_protocol_iterator p = all_referenced_protocol_begin(), 
364         e = all_referenced_protocol_end(); p != e; ++p) {
365     ProtocolRefs.push_back(*p);
366   }
367
368   data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
369 }
370
371 void ObjCInterfaceDecl::allocateDefinitionData() {
372   assert(!hasDefinition() && "ObjC class already has a definition");
373   Data.setPointer(new (getASTContext()) DefinitionData());
374   Data.getPointer()->Definition = this;
375
376   // Make the type point at the definition, now that we have one.
377   if (TypeForDecl)
378     cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
379 }
380
381 void ObjCInterfaceDecl::startDefinition() {
382   allocateDefinitionData();
383
384   // Update all of the declarations with a pointer to the definition.
385   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
386        RD != RDEnd; ++RD) {
387     if (*RD != this)
388       RD->Data = Data;
389   }
390 }
391
392 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
393                                               ObjCInterfaceDecl *&clsDeclared) {
394   // FIXME: Should make sure no callers ever do this.
395   if (!hasDefinition())
396     return 0;  
397
398   if (data().ExternallyCompleted)
399     LoadExternalDefinition();
400
401   ObjCInterfaceDecl* ClassDecl = this;
402   while (ClassDecl != NULL) {
403     if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
404       clsDeclared = ClassDecl;
405       return I;
406     }
407
408     for (ObjCInterfaceDecl::visible_extensions_iterator
409            Ext = ClassDecl->visible_extensions_begin(),
410            ExtEnd = ClassDecl->visible_extensions_end();
411          Ext != ExtEnd; ++Ext) {
412       if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
413         clsDeclared = ClassDecl;
414         return I;
415       }
416     }
417       
418     ClassDecl = ClassDecl->getSuperClass();
419   }
420   return NULL;
421 }
422
423 /// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
424 /// class whose name is passed as argument. If it is not one of the super classes
425 /// the it returns NULL.
426 ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
427                                         const IdentifierInfo*ICName) {
428   // FIXME: Should make sure no callers ever do this.
429   if (!hasDefinition())
430     return 0;
431
432   if (data().ExternallyCompleted)
433     LoadExternalDefinition();
434
435   ObjCInterfaceDecl* ClassDecl = this;
436   while (ClassDecl != NULL) {
437     if (ClassDecl->getIdentifier() == ICName)
438       return ClassDecl;
439     ClassDecl = ClassDecl->getSuperClass();
440   }
441   return NULL;
442 }
443
444 /// lookupMethod - This method returns an instance/class method by looking in
445 /// the class, its categories, and its super classes (using a linear search).
446 /// When argument category "C" is specified, any implicit method found
447 /// in this category is ignored.
448 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel, 
449                                      bool isInstance,
450                                      bool shallowCategoryLookup,
451                                      const ObjCCategoryDecl *C) const {
452   // FIXME: Should make sure no callers ever do this.
453   if (!hasDefinition())
454     return 0;
455
456   const ObjCInterfaceDecl* ClassDecl = this;
457   ObjCMethodDecl *MethodDecl = 0;
458
459   if (data().ExternallyCompleted)
460     LoadExternalDefinition();
461
462   while (ClassDecl != NULL) {
463     if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
464       return MethodDecl;
465
466     // Didn't find one yet - look through protocols.
467     for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
468                                               E = ClassDecl->protocol_end();
469            I != E; ++I)
470       if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
471         return MethodDecl;
472     
473     // Didn't find one yet - now look through categories.
474     for (ObjCInterfaceDecl::visible_categories_iterator
475          Cat = ClassDecl->visible_categories_begin(),
476          CatEnd = ClassDecl->visible_categories_end();
477          Cat != CatEnd; ++Cat) {
478       if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
479         if (C != (*Cat) || !MethodDecl->isImplicit())
480           return MethodDecl;
481
482       if (!shallowCategoryLookup) {
483         // Didn't find one yet - look through protocols.
484         const ObjCList<ObjCProtocolDecl> &Protocols =
485         Cat->getReferencedProtocols();
486         for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
487              E = Protocols.end(); I != E; ++I)
488           if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
489             if (C != (*Cat) || !MethodDecl->isImplicit())
490               return MethodDecl;
491       }
492     }
493   
494     ClassDecl = ClassDecl->getSuperClass();
495   }
496   return NULL;
497 }
498
499 // Will search "local" class/category implementations for a method decl.
500 // If failed, then we search in class's root for an instance method.
501 // Returns 0 if no method is found.
502 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
503                                    const Selector &Sel,
504                                    bool Instance) const {
505   // FIXME: Should make sure no callers ever do this.
506   if (!hasDefinition())
507     return 0;
508
509   if (data().ExternallyCompleted)
510     LoadExternalDefinition();
511
512   ObjCMethodDecl *Method = 0;
513   if (ObjCImplementationDecl *ImpDecl = getImplementation())
514     Method = Instance ? ImpDecl->getInstanceMethod(Sel) 
515                       : ImpDecl->getClassMethod(Sel);
516
517   // Look through local category implementations associated with the class.
518   if (!Method)
519     Method = Instance ? getCategoryInstanceMethod(Sel)
520                       : getCategoryClassMethod(Sel);
521
522   // Before we give up, check if the selector is an instance method.
523   // But only in the root. This matches gcc's behavior and what the
524   // runtime expects.
525   if (!Instance && !Method && !getSuperClass()) {
526     Method = lookupInstanceMethod(Sel);
527     // Look through local category implementations associated
528     // with the root class.
529     if (!Method)
530       Method = lookupPrivateMethod(Sel, true);
531   }
532
533   if (!Method && getSuperClass())
534     return getSuperClass()->lookupPrivateMethod(Sel, Instance);
535   return Method;
536 }
537
538 //===----------------------------------------------------------------------===//
539 // ObjCMethodDecl
540 //===----------------------------------------------------------------------===//
541
542 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
543                                        SourceLocation beginLoc,
544                                        SourceLocation endLoc,
545                                        Selector SelInfo, QualType T,
546                                        TypeSourceInfo *ResultTInfo,
547                                        DeclContext *contextDecl,
548                                        bool isInstance,
549                                        bool isVariadic,
550                                        bool isPropertyAccessor,
551                                        bool isImplicitlyDeclared,
552                                        bool isDefined,
553                                        ImplementationControl impControl,
554                                        bool HasRelatedResultType) {
555   return new (C) ObjCMethodDecl(beginLoc, endLoc,
556                                 SelInfo, T, ResultTInfo, contextDecl,
557                                 isInstance, isVariadic, isPropertyAccessor,
558                                 isImplicitlyDeclared, isDefined,
559                                 impControl,
560                                 HasRelatedResultType);
561 }
562
563 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
564   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
565   return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(), 
566                                   Selector(), QualType(), 0, 0);
567 }
568
569 Stmt *ObjCMethodDecl::getBody() const {
570   return Body.get(getASTContext().getExternalSource());
571 }
572
573 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
574   assert(PrevMethod);
575   getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
576   IsRedeclaration = true;
577   PrevMethod->HasRedeclaration = true;
578 }
579
580 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
581                                          ArrayRef<ParmVarDecl*> Params,
582                                          ArrayRef<SourceLocation> SelLocs) {
583   ParamsAndSelLocs = 0;
584   NumParams = Params.size();
585   if (Params.empty() && SelLocs.empty())
586     return;
587
588   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
589                   sizeof(SourceLocation) * SelLocs.size();
590   ParamsAndSelLocs = C.Allocate(Size);
591   std::copy(Params.begin(), Params.end(), getParams());
592   std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
593 }
594
595 void ObjCMethodDecl::getSelectorLocs(
596                                SmallVectorImpl<SourceLocation> &SelLocs) const {
597   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
598     SelLocs.push_back(getSelectorLoc(i));
599 }
600
601 void ObjCMethodDecl::setMethodParams(ASTContext &C,
602                                      ArrayRef<ParmVarDecl*> Params,
603                                      ArrayRef<SourceLocation> SelLocs) {
604   assert((!SelLocs.empty() || isImplicit()) &&
605          "No selector locs for non-implicit method");
606   if (isImplicit())
607     return setParamsAndSelLocs(C, Params, llvm::None);
608
609   SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
610                                         DeclEndLoc);
611   if (SelLocsKind != SelLoc_NonStandard)
612     return setParamsAndSelLocs(C, Params, llvm::None);
613
614   setParamsAndSelLocs(C, Params, SelLocs);
615 }
616
617 /// \brief A definition will return its interface declaration.
618 /// An interface declaration will return its definition.
619 /// Otherwise it will return itself.
620 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
621   ASTContext &Ctx = getASTContext();
622   ObjCMethodDecl *Redecl = 0;
623   if (HasRedeclaration)
624     Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
625   if (Redecl)
626     return Redecl;
627
628   Decl *CtxD = cast<Decl>(getDeclContext());
629
630   if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
631     if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
632       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
633
634   } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
635     if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
636       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
637
638   } else if (ObjCImplementationDecl *ImplD =
639                dyn_cast<ObjCImplementationDecl>(CtxD)) {
640     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
641       Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
642
643   } else if (ObjCCategoryImplDecl *CImplD =
644                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
645     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
646       Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
647   }
648
649   if (!Redecl && isRedeclaration()) {
650     // This is the last redeclaration, go back to the first method.
651     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
652                                                     isInstanceMethod());
653   }
654
655   return Redecl ? Redecl : this;
656 }
657
658 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
659   Decl *CtxD = cast<Decl>(getDeclContext());
660
661   if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
662     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
663       if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
664                                               isInstanceMethod()))
665         return MD;
666
667   } else if (ObjCCategoryImplDecl *CImplD =
668                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
669     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
670       if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
671                                                isInstanceMethod()))
672         return MD;
673   }
674
675   if (isRedeclaration())
676     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
677                                                     isInstanceMethod());
678
679   return this;
680 }
681
682 SourceLocation ObjCMethodDecl::getLocEnd() const {
683   if (Stmt *Body = getBody())
684     return Body->getLocEnd();
685   return DeclEndLoc;
686 }
687
688 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
689   ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
690   if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
691     return family;
692
693   // Check for an explicit attribute.
694   if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
695     // The unfortunate necessity of mapping between enums here is due
696     // to the attributes framework.
697     switch (attr->getFamily()) {
698     case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
699     case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
700     case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
701     case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
702     case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
703     case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
704     }
705     Family = static_cast<unsigned>(family);
706     return family;
707   }
708
709   family = getSelector().getMethodFamily();
710   switch (family) {
711   case OMF_None: break;
712
713   // init only has a conventional meaning for an instance method, and
714   // it has to return an object.
715   case OMF_init:
716     if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
717       family = OMF_None;
718     break;
719
720   // alloc/copy/new have a conventional meaning for both class and
721   // instance methods, but they require an object return.
722   case OMF_alloc:
723   case OMF_copy:
724   case OMF_mutableCopy:
725   case OMF_new:
726     if (!getResultType()->isObjCObjectPointerType())
727       family = OMF_None;
728     break;
729
730   // These selectors have a conventional meaning only for instance methods.
731   case OMF_dealloc:
732   case OMF_finalize:
733   case OMF_retain:
734   case OMF_release:
735   case OMF_autorelease:
736   case OMF_retainCount:
737   case OMF_self:
738     if (!isInstanceMethod())
739       family = OMF_None;
740     break;
741       
742   case OMF_performSelector:
743     if (!isInstanceMethod() ||
744         !getResultType()->isObjCIdType())
745       family = OMF_None;
746     else {
747       unsigned noParams = param_size();
748       if (noParams < 1 || noParams > 3)
749         family = OMF_None;
750       else {
751         ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
752         QualType ArgT = (*it);
753         if (!ArgT->isObjCSelType()) {
754           family = OMF_None;
755           break;
756         }
757         while (--noParams) {
758           it++;
759           ArgT = (*it);
760           if (!ArgT->isObjCIdType()) {
761             family = OMF_None;
762             break;
763           }
764         }
765       }
766     }
767     break;
768       
769   }
770
771   // Cache the result.
772   Family = static_cast<unsigned>(family);
773   return family;
774 }
775
776 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
777                                           const ObjCInterfaceDecl *OID) {
778   QualType selfTy;
779   if (isInstanceMethod()) {
780     // There may be no interface context due to error in declaration
781     // of the interface (which has been reported). Recover gracefully.
782     if (OID) {
783       selfTy = Context.getObjCInterfaceType(OID);
784       selfTy = Context.getObjCObjectPointerType(selfTy);
785     } else {
786       selfTy = Context.getObjCIdType();
787     }
788   } else // we have a factory method.
789     selfTy = Context.getObjCClassType();
790
791   bool selfIsPseudoStrong = false;
792   bool selfIsConsumed = false;
793   
794   if (Context.getLangOpts().ObjCAutoRefCount) {
795     if (isInstanceMethod()) {
796       selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
797
798       // 'self' is always __strong.  It's actually pseudo-strong except
799       // in init methods (or methods labeled ns_consumes_self), though.
800       Qualifiers qs;
801       qs.setObjCLifetime(Qualifiers::OCL_Strong);
802       selfTy = Context.getQualifiedType(selfTy, qs);
803
804       // In addition, 'self' is const unless this is an init method.
805       if (getMethodFamily() != OMF_init && !selfIsConsumed) {
806         selfTy = selfTy.withConst();
807         selfIsPseudoStrong = true;
808       }
809     }
810     else {
811       assert(isClassMethod());
812       // 'self' is always const in class methods.
813       selfTy = selfTy.withConst();
814       selfIsPseudoStrong = true;
815     }
816   }
817
818   ImplicitParamDecl *self
819     = ImplicitParamDecl::Create(Context, this, SourceLocation(),
820                                 &Context.Idents.get("self"), selfTy);
821   setSelfDecl(self);
822
823   if (selfIsConsumed)
824     self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
825
826   if (selfIsPseudoStrong)
827     self->setARCPseudoStrong(true);
828
829   setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
830                                        &Context.Idents.get("_cmd"),
831                                        Context.getObjCSelType()));
832 }
833
834 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
835   if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
836     return ID;
837   if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
838     return CD->getClassInterface();
839   if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
840     return IMD->getClassInterface();
841
842   assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
843   llvm_unreachable("unknown method context");
844 }
845
846 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
847                                             const ObjCMethodDecl *Method,
848                                SmallVectorImpl<const ObjCMethodDecl *> &Methods,
849                                             bool MovedToSuper) {
850   if (!Container)
851     return;
852
853   // In categories look for overriden methods from protocols. A method from
854   // category is not "overriden" since it is considered as the "same" method
855   // (same USR) as the one from the interface.
856   if (const ObjCCategoryDecl *
857         Category = dyn_cast<ObjCCategoryDecl>(Container)) {
858     // Check whether we have a matching method at this category but only if we
859     // are at the super class level.
860     if (MovedToSuper)
861       if (ObjCMethodDecl *
862             Overridden = Container->getMethod(Method->getSelector(),
863                                               Method->isInstanceMethod(),
864                                               /*AllowHidden=*/true))
865         if (Method != Overridden) {
866           // We found an override at this category; there is no need to look
867           // into its protocols.
868           Methods.push_back(Overridden);
869           return;
870         }
871
872     for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
873                                           PEnd = Category->protocol_end();
874          P != PEnd; ++P)
875       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
876     return;
877   }
878
879   // Check whether we have a matching method at this level.
880   if (const ObjCMethodDecl *
881         Overridden = Container->getMethod(Method->getSelector(),
882                                           Method->isInstanceMethod(),
883                                           /*AllowHidden=*/true))
884     if (Method != Overridden) {
885       // We found an override at this level; there is no need to look
886       // into other protocols or categories.
887       Methods.push_back(Overridden);
888       return;
889     }
890
891   if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
892     for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
893                                           PEnd = Protocol->protocol_end();
894          P != PEnd; ++P)
895       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
896   }
897
898   if (const ObjCInterfaceDecl *
899         Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
900     for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
901                                            PEnd = Interface->protocol_end();
902          P != PEnd; ++P)
903       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
904
905     for (ObjCInterfaceDecl::known_categories_iterator
906            Cat = Interface->known_categories_begin(),
907            CatEnd = Interface->known_categories_end();
908          Cat != CatEnd; ++Cat) {
909       CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
910                                       MovedToSuper);
911     }
912
913     if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
914       return CollectOverriddenMethodsRecurse(Super, Method, Methods,
915                                              /*MovedToSuper=*/true);
916   }
917 }
918
919 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
920                                             const ObjCMethodDecl *Method,
921                              SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
922   CollectOverriddenMethodsRecurse(Container, Method, Methods,
923                                   /*MovedToSuper=*/false);
924 }
925
926 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
927                           SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
928   assert(Method->isOverriding());
929
930   if (const ObjCProtocolDecl *
931         ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
932     CollectOverriddenMethods(ProtD, Method, overridden);
933
934   } else if (const ObjCImplDecl *
935                IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
936     const ObjCInterfaceDecl *ID = IMD->getClassInterface();
937     if (!ID)
938       return;
939     // Start searching for overridden methods using the method from the
940     // interface as starting point.
941     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
942                                                     Method->isInstanceMethod(),
943                                                     /*AllowHidden=*/true))
944       Method = IFaceMeth;
945     CollectOverriddenMethods(ID, Method, overridden);
946
947   } else if (const ObjCCategoryDecl *
948                CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
949     const ObjCInterfaceDecl *ID = CatD->getClassInterface();
950     if (!ID)
951       return;
952     // Start searching for overridden methods using the method from the
953     // interface as starting point.
954     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
955                                                      Method->isInstanceMethod(),
956                                                      /*AllowHidden=*/true))
957       Method = IFaceMeth;
958     CollectOverriddenMethods(ID, Method, overridden);
959
960   } else {
961     CollectOverriddenMethods(
962                   dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
963                   Method, overridden);
964   }
965 }
966
967 void ObjCMethodDecl::getOverriddenMethods(
968                     SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
969   const ObjCMethodDecl *Method = this;
970
971   if (Method->isRedeclaration()) {
972     Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
973                    getMethod(Method->getSelector(), Method->isInstanceMethod());
974   }
975
976   if (Method->isOverriding()) {
977     collectOverriddenMethodsSlow(Method, Overridden);
978     assert(!Overridden.empty() &&
979            "ObjCMethodDecl's overriding bit is not as expected");
980   }
981 }
982
983 const ObjCPropertyDecl *
984 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
985   Selector Sel = getSelector();
986   unsigned NumArgs = Sel.getNumArgs();
987   if (NumArgs > 1)
988     return 0;
989
990   if (!isInstanceMethod() || getMethodFamily() != OMF_None)
991     return 0;
992   
993   if (isPropertyAccessor()) {
994     const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
995     // If container is class extension, find its primary class.
996     if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
997       if (CatDecl->IsClassExtension())
998         Container = CatDecl->getClassInterface();
999     
1000     bool IsGetter = (NumArgs == 0);
1001
1002     for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
1003                                           E = Container->prop_end();
1004          I != E; ++I) {
1005       Selector NextSel = IsGetter ? (*I)->getGetterName()
1006                                   : (*I)->getSetterName();
1007       if (NextSel == Sel)
1008         return *I;
1009     }
1010
1011     llvm_unreachable("Marked as a property accessor but no property found!");
1012   }
1013
1014   if (!CheckOverrides)
1015     return 0;
1016
1017   typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1018   OverridesTy Overrides;
1019   getOverriddenMethods(Overrides);
1020   for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1021        I != E; ++I) {
1022     if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1023       return Prop;
1024   }
1025
1026   return 0;
1027
1028 }
1029
1030 //===----------------------------------------------------------------------===//
1031 // ObjCInterfaceDecl
1032 //===----------------------------------------------------------------------===//
1033
1034 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1035                                              DeclContext *DC,
1036                                              SourceLocation atLoc,
1037                                              IdentifierInfo *Id,
1038                                              ObjCInterfaceDecl *PrevDecl,
1039                                              SourceLocation ClassLoc,
1040                                              bool isInternal){
1041   ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc, 
1042                                                         PrevDecl, isInternal);
1043   Result->Data.setInt(!C.getLangOpts().Modules);
1044   C.getObjCInterfaceType(Result, PrevDecl);
1045   return Result;
1046 }
1047
1048 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C, 
1049                                                          unsigned ID) {
1050   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1051   ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1052                                                           0, SourceLocation(),
1053                                                           0, false);
1054   Result->Data.setInt(!C.getLangOpts().Modules);
1055   return Result;
1056 }
1057
1058 ObjCInterfaceDecl::
1059 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
1060                   SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1061                   bool isInternal)
1062   : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
1063     TypeForDecl(0), Data()
1064 {
1065   setPreviousDeclaration(PrevDecl);
1066   
1067   // Copy the 'data' pointer over.
1068   if (PrevDecl)
1069     Data = PrevDecl->Data;
1070   
1071   setImplicit(isInternal);
1072 }
1073
1074 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1075   assert(data().ExternallyCompleted && "Class is not externally completed");
1076   data().ExternallyCompleted = false;
1077   getASTContext().getExternalSource()->CompleteType(
1078                                         const_cast<ObjCInterfaceDecl *>(this));
1079 }
1080
1081 void ObjCInterfaceDecl::setExternallyCompleted() {
1082   assert(getASTContext().getExternalSource() && 
1083          "Class can't be externally completed without an external source");
1084   assert(hasDefinition() && 
1085          "Forward declarations can't be externally completed");
1086   data().ExternallyCompleted = true;
1087 }
1088
1089 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1090   if (const ObjCInterfaceDecl *Def = getDefinition()) {
1091     if (data().ExternallyCompleted)
1092       LoadExternalDefinition();
1093     
1094     return getASTContext().getObjCImplementation(
1095              const_cast<ObjCInterfaceDecl*>(Def));
1096   }
1097   
1098   // FIXME: Should make sure no callers ever do this.
1099   return 0;
1100 }
1101
1102 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1103   getASTContext().setObjCImplementation(getDefinition(), ImplD);
1104 }
1105
1106 namespace {
1107   struct SynthesizeIvarChunk {
1108     uint64_t Size;
1109     ObjCIvarDecl *Ivar;
1110     SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1111       : Size(size), Ivar(ivar) {}
1112   };
1113
1114   bool operator<(const SynthesizeIvarChunk & LHS,
1115                  const SynthesizeIvarChunk &RHS) {
1116       return LHS.Size < RHS.Size;
1117   }
1118 }
1119
1120 /// all_declared_ivar_begin - return first ivar declared in this class,
1121 /// its extensions and its implementation. Lazily build the list on first
1122 /// access.
1123 ///
1124 /// Caveat: The list returned by this method reflects the current
1125 /// state of the parser. The cache will be updated for every ivar
1126 /// added by an extension or the implementation when they are
1127 /// encountered.
1128 /// See also ObjCIvarDecl::Create().
1129 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1130   // FIXME: Should make sure no callers ever do this.
1131   if (!hasDefinition())
1132     return 0;
1133   
1134   ObjCIvarDecl *curIvar = 0;
1135   if (!data().IvarList) {
1136     if (!ivar_empty()) {
1137       ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1138       data().IvarList = *I; ++I;
1139       for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1140         curIvar->setNextIvar(*I);
1141     }
1142
1143     for (ObjCInterfaceDecl::known_extensions_iterator
1144            Ext = known_extensions_begin(),
1145            ExtEnd = known_extensions_end();
1146          Ext != ExtEnd; ++Ext) {
1147       if (!Ext->ivar_empty()) {
1148         ObjCCategoryDecl::ivar_iterator
1149           I = Ext->ivar_begin(),
1150           E = Ext->ivar_end();
1151         if (!data().IvarList) {
1152           data().IvarList = *I; ++I;
1153           curIvar = data().IvarList;
1154         }
1155         for ( ;I != E; curIvar = *I, ++I)
1156           curIvar->setNextIvar(*I);
1157       }
1158     }
1159     data().IvarListMissingImplementation = true;
1160   }
1161
1162   // cached and complete!
1163   if (!data().IvarListMissingImplementation)
1164       return data().IvarList;
1165   
1166   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1167     data().IvarListMissingImplementation = false;
1168     if (!ImplDecl->ivar_empty()) {
1169       SmallVector<SynthesizeIvarChunk, 16> layout;
1170       for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1171            E = ImplDecl->ivar_end(); I != E; ++I) {
1172         ObjCIvarDecl *IV = *I;
1173         if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1174           layout.push_back(SynthesizeIvarChunk(
1175                              IV->getASTContext().getTypeSize(IV->getType()), IV));
1176           continue;
1177         }
1178         if (!data().IvarList)
1179           data().IvarList = *I;
1180         else
1181           curIvar->setNextIvar(*I);
1182         curIvar = *I;
1183       }
1184       
1185       if (!layout.empty()) {
1186         // Order synthesized ivars by their size.
1187         std::stable_sort(layout.begin(), layout.end());
1188         unsigned Ix = 0, EIx = layout.size();
1189         if (!data().IvarList) {
1190           data().IvarList = layout[0].Ivar; Ix++;
1191           curIvar = data().IvarList;
1192         }
1193         for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1194           curIvar->setNextIvar(layout[Ix].Ivar);
1195       }
1196     }
1197   }
1198   return data().IvarList;
1199 }
1200
1201 /// FindCategoryDeclaration - Finds category declaration in the list of
1202 /// categories for this class and returns it. Name of the category is passed
1203 /// in 'CategoryId'. If category not found, return 0;
1204 ///
1205 ObjCCategoryDecl *
1206 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1207   // FIXME: Should make sure no callers ever do this.
1208   if (!hasDefinition())
1209     return 0;
1210
1211   if (data().ExternallyCompleted)
1212     LoadExternalDefinition();
1213
1214   for (visible_categories_iterator Cat = visible_categories_begin(),
1215                                    CatEnd = visible_categories_end();
1216        Cat != CatEnd;
1217        ++Cat) {
1218     if (Cat->getIdentifier() == CategoryId)
1219       return *Cat;
1220   }
1221   
1222   return 0;
1223 }
1224
1225 ObjCMethodDecl *
1226 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1227   for (visible_categories_iterator Cat = visible_categories_begin(),
1228                                    CatEnd = visible_categories_end();
1229        Cat != CatEnd;
1230        ++Cat) {
1231     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1232       if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1233         return MD;
1234   }
1235
1236   return 0;
1237 }
1238
1239 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1240   for (visible_categories_iterator Cat = visible_categories_begin(),
1241                                    CatEnd = visible_categories_end();
1242        Cat != CatEnd;
1243        ++Cat) {
1244     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1245       if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1246         return MD;
1247   }
1248   
1249   return 0;
1250 }
1251
1252 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1253 /// has been implemented in IDecl class, its super class or categories (if
1254 /// lookupCategory is true).
1255 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1256                                     bool lookupCategory,
1257                                     bool RHSIsQualifiedID) {
1258   if (!hasDefinition())
1259     return false;
1260   
1261   ObjCInterfaceDecl *IDecl = this;
1262   // 1st, look up the class.
1263   for (ObjCInterfaceDecl::protocol_iterator
1264         PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
1265     if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1266       return true;
1267     // This is dubious and is added to be compatible with gcc.  In gcc, it is
1268     // also allowed assigning a protocol-qualified 'id' type to a LHS object
1269     // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1270     // object. This IMO, should be a bug.
1271     // FIXME: Treat this as an extension, and flag this as an error when GCC
1272     // extensions are not enabled.
1273     if (RHSIsQualifiedID &&
1274         getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1275       return true;
1276   }
1277
1278   // 2nd, look up the category.
1279   if (lookupCategory)
1280     for (visible_categories_iterator Cat = visible_categories_begin(),
1281                                      CatEnd = visible_categories_end();
1282          Cat != CatEnd;
1283          ++Cat) {
1284       for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1285                                                E = Cat->protocol_end();
1286            PI != E; ++PI)
1287         if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1288           return true;
1289     }
1290
1291   // 3rd, look up the super class(s)
1292   if (IDecl->getSuperClass())
1293     return
1294   IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1295                                                   RHSIsQualifiedID);
1296
1297   return false;
1298 }
1299
1300 //===----------------------------------------------------------------------===//
1301 // ObjCIvarDecl
1302 //===----------------------------------------------------------------------===//
1303
1304 void ObjCIvarDecl::anchor() { }
1305
1306 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1307                                    SourceLocation StartLoc,
1308                                    SourceLocation IdLoc, IdentifierInfo *Id,
1309                                    QualType T, TypeSourceInfo *TInfo,
1310                                    AccessControl ac, Expr *BW,
1311                                    bool synthesized) {
1312   if (DC) {
1313     // Ivar's can only appear in interfaces, implementations (via synthesized
1314     // properties), and class extensions (via direct declaration, or synthesized
1315     // properties).
1316     //
1317     // FIXME: This should really be asserting this:
1318     //   (isa<ObjCCategoryDecl>(DC) &&
1319     //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1320     // but unfortunately we sometimes place ivars into non-class extension
1321     // categories on error. This breaks an AST invariant, and should not be
1322     // fixed.
1323     assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1324             isa<ObjCCategoryDecl>(DC)) &&
1325            "Invalid ivar decl context!");
1326     // Once a new ivar is created in any of class/class-extension/implementation
1327     // decl contexts, the previously built IvarList must be rebuilt.
1328     ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1329     if (!ID) {
1330       if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1331         ID = IM->getClassInterface();
1332       else
1333         ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1334     }
1335     ID->setIvarList(0);
1336   }
1337
1338   return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1339                               ac, BW, synthesized);
1340 }
1341
1342 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1343   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1344   return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1345                                 QualType(), 0, ObjCIvarDecl::None, 0, false);
1346 }
1347
1348 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1349   const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1350
1351   switch (DC->getKind()) {
1352   default:
1353   case ObjCCategoryImpl:
1354   case ObjCProtocol:
1355     llvm_unreachable("invalid ivar container!");
1356
1357     // Ivars can only appear in class extension categories.
1358   case ObjCCategory: {
1359     const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1360     assert(CD->IsClassExtension() && "invalid container for ivar!");
1361     return CD->getClassInterface();
1362   }
1363
1364   case ObjCImplementation:
1365     return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1366
1367   case ObjCInterface:
1368     return cast<ObjCInterfaceDecl>(DC);
1369   }
1370 }
1371
1372 //===----------------------------------------------------------------------===//
1373 // ObjCAtDefsFieldDecl
1374 //===----------------------------------------------------------------------===//
1375
1376 void ObjCAtDefsFieldDecl::anchor() { }
1377
1378 ObjCAtDefsFieldDecl
1379 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1380                              SourceLocation StartLoc,  SourceLocation IdLoc,
1381                              IdentifierInfo *Id, QualType T, Expr *BW) {
1382   return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1383 }
1384
1385 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C, 
1386                                                              unsigned ID) {
1387   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1388   return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1389                                        0, QualType(), 0);
1390 }
1391
1392 //===----------------------------------------------------------------------===//
1393 // ObjCProtocolDecl
1394 //===----------------------------------------------------------------------===//
1395
1396 void ObjCProtocolDecl::anchor() { }
1397
1398 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1399                                    SourceLocation nameLoc, 
1400                                    SourceLocation atStartLoc,
1401                                    ObjCProtocolDecl *PrevDecl)
1402   : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1403 {
1404   setPreviousDeclaration(PrevDecl);
1405   if (PrevDecl)
1406     Data = PrevDecl->Data;
1407 }
1408
1409 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1410                                            IdentifierInfo *Id,
1411                                            SourceLocation nameLoc,
1412                                            SourceLocation atStartLoc,
1413                                            ObjCProtocolDecl *PrevDecl) {
1414   ObjCProtocolDecl *Result 
1415     = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1416   Result->Data.setInt(!C.getLangOpts().Modules);
1417   return Result;
1418 }
1419
1420 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C, 
1421                                                        unsigned ID) {
1422   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1423   ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1424                                                         SourceLocation(), 0);
1425   Result->Data.setInt(!C.getLangOpts().Modules);
1426   return Result;
1427 }
1428
1429 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1430   ObjCProtocolDecl *PDecl = this;
1431
1432   if (Name == getIdentifier())
1433     return PDecl;
1434
1435   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1436     if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1437       return PDecl;
1438
1439   return NULL;
1440 }
1441
1442 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1443 // it inherited.
1444 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1445                                                bool isInstance) const {
1446   ObjCMethodDecl *MethodDecl = NULL;
1447
1448   // If there is no definition or the definition is hidden, we don't find
1449   // anything.
1450   const ObjCProtocolDecl *Def = getDefinition();
1451   if (!Def || Def->isHidden())
1452     return NULL;
1453
1454   if ((MethodDecl = getMethod(Sel, isInstance)))
1455     return MethodDecl;
1456
1457   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1458     if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1459       return MethodDecl;
1460   return NULL;
1461 }
1462
1463 void ObjCProtocolDecl::allocateDefinitionData() {
1464   assert(!Data.getPointer() && "Protocol already has a definition!");
1465   Data.setPointer(new (getASTContext()) DefinitionData);
1466   Data.getPointer()->Definition = this;
1467 }
1468
1469 void ObjCProtocolDecl::startDefinition() {
1470   allocateDefinitionData();
1471   
1472   // Update all of the declarations with a pointer to the definition.
1473   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1474        RD != RDEnd; ++RD)
1475     RD->Data = this->Data;
1476 }
1477
1478 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1479                                                     PropertyDeclOrder &PO) const {
1480   
1481   if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1482     for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1483          E = PDecl->prop_end(); P != E; ++P) {
1484       ObjCPropertyDecl *Prop = *P;
1485       // Insert into PM if not there already.
1486       PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1487       PO.push_back(Prop);
1488     }
1489     // Scan through protocol's protocols.
1490     for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1491          E = PDecl->protocol_end(); PI != E; ++PI)
1492       (*PI)->collectPropertiesToImplement(PM, PO);
1493   }
1494 }
1495
1496
1497 //===----------------------------------------------------------------------===//
1498 // ObjCCategoryDecl
1499 //===----------------------------------------------------------------------===//
1500
1501 void ObjCCategoryDecl::anchor() { }
1502
1503 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1504                                            SourceLocation AtLoc, 
1505                                            SourceLocation ClassNameLoc,
1506                                            SourceLocation CategoryNameLoc,
1507                                            IdentifierInfo *Id,
1508                                            ObjCInterfaceDecl *IDecl,
1509                                            SourceLocation IvarLBraceLoc,
1510                                            SourceLocation IvarRBraceLoc) {
1511   ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1512                                                        CategoryNameLoc, Id,
1513                                                        IDecl,
1514                                                        IvarLBraceLoc, IvarRBraceLoc);
1515   if (IDecl) {
1516     // Link this category into its class's category list.
1517     CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1518     if (IDecl->hasDefinition()) {
1519       IDecl->setCategoryListRaw(CatDecl);
1520       if (ASTMutationListener *L = C.getASTMutationListener())
1521         L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1522     }
1523   }
1524
1525   return CatDecl;
1526 }
1527
1528 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C, 
1529                                                        unsigned ID) {
1530   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1531   return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1532                                     SourceLocation(), 0, 0);
1533 }
1534
1535 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1536   return getASTContext().getObjCImplementation(
1537                                            const_cast<ObjCCategoryDecl*>(this));
1538 }
1539
1540 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1541   getASTContext().setObjCImplementation(this, ImplD);
1542 }
1543
1544
1545 //===----------------------------------------------------------------------===//
1546 // ObjCCategoryImplDecl
1547 //===----------------------------------------------------------------------===//
1548
1549 void ObjCCategoryImplDecl::anchor() { }
1550
1551 ObjCCategoryImplDecl *
1552 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1553                              IdentifierInfo *Id,
1554                              ObjCInterfaceDecl *ClassInterface,
1555                              SourceLocation nameLoc,
1556                              SourceLocation atStartLoc,
1557                              SourceLocation CategoryNameLoc) {
1558   if (ClassInterface && ClassInterface->hasDefinition())
1559     ClassInterface = ClassInterface->getDefinition();
1560   return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1561                                       nameLoc, atStartLoc, CategoryNameLoc);
1562 }
1563
1564 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C, 
1565                                                                unsigned ID) {
1566   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1567   return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(), 
1568                                         SourceLocation(), SourceLocation());
1569 }
1570
1571 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1572   // The class interface might be NULL if we are working with invalid code.
1573   if (const ObjCInterfaceDecl *ID = getClassInterface())
1574     return ID->FindCategoryDeclaration(getIdentifier());
1575   return 0;
1576 }
1577
1578
1579 void ObjCImplDecl::anchor() { }
1580
1581 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1582   // FIXME: The context should be correct before we get here.
1583   property->setLexicalDeclContext(this);
1584   addDecl(property);
1585 }
1586
1587 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1588   ASTContext &Ctx = getASTContext();
1589
1590   if (ObjCImplementationDecl *ImplD
1591         = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1592     if (IFace)
1593       Ctx.setObjCImplementation(IFace, ImplD);
1594
1595   } else if (ObjCCategoryImplDecl *ImplD =
1596              dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1597     if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1598       Ctx.setObjCImplementation(CD, ImplD);
1599   }
1600
1601   ClassInterface = IFace;
1602 }
1603
1604 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1605 /// properties implemented in this \@implementation block and returns
1606 /// the implemented property that uses it.
1607 ///
1608 ObjCPropertyImplDecl *ObjCImplDecl::
1609 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1610   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1611     ObjCPropertyImplDecl *PID = *i;
1612     if (PID->getPropertyIvarDecl() &&
1613         PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1614       return PID;
1615   }
1616   return 0;
1617 }
1618
1619 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1620 /// added to the list of those properties \@synthesized/\@dynamic in this
1621 /// category \@implementation block.
1622 ///
1623 ObjCPropertyImplDecl *ObjCImplDecl::
1624 FindPropertyImplDecl(IdentifierInfo *Id) const {
1625   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1626     ObjCPropertyImplDecl *PID = *i;
1627     if (PID->getPropertyDecl()->getIdentifier() == Id)
1628       return PID;
1629   }
1630   return 0;
1631 }
1632
1633 raw_ostream &clang::operator<<(raw_ostream &OS,
1634                                const ObjCCategoryImplDecl &CID) {
1635   OS << CID.getName();
1636   return OS;
1637 }
1638
1639 //===----------------------------------------------------------------------===//
1640 // ObjCImplementationDecl
1641 //===----------------------------------------------------------------------===//
1642
1643 void ObjCImplementationDecl::anchor() { }
1644
1645 ObjCImplementationDecl *
1646 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1647                                ObjCInterfaceDecl *ClassInterface,
1648                                ObjCInterfaceDecl *SuperDecl,
1649                                SourceLocation nameLoc,
1650                                SourceLocation atStartLoc,
1651                                SourceLocation superLoc,
1652                                SourceLocation IvarLBraceLoc,
1653                                SourceLocation IvarRBraceLoc) {
1654   if (ClassInterface && ClassInterface->hasDefinition())
1655     ClassInterface = ClassInterface->getDefinition();
1656   return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1657                                         nameLoc, atStartLoc, superLoc,
1658                                         IvarLBraceLoc, IvarRBraceLoc);
1659 }
1660
1661 ObjCImplementationDecl *
1662 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1663   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1664   return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(), 
1665                                           SourceLocation());
1666 }
1667
1668 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1669                                              CXXCtorInitializer ** initializers,
1670                                                  unsigned numInitializers) {
1671   if (numInitializers > 0) {
1672     NumIvarInitializers = numInitializers;
1673     CXXCtorInitializer **ivarInitializers =
1674     new (C) CXXCtorInitializer*[NumIvarInitializers];
1675     memcpy(ivarInitializers, initializers,
1676            numInitializers * sizeof(CXXCtorInitializer*));
1677     IvarInitializers = ivarInitializers;
1678   }
1679 }
1680
1681 raw_ostream &clang::operator<<(raw_ostream &OS,
1682                                const ObjCImplementationDecl &ID) {
1683   OS << ID.getName();
1684   return OS;
1685 }
1686
1687 //===----------------------------------------------------------------------===//
1688 // ObjCCompatibleAliasDecl
1689 //===----------------------------------------------------------------------===//
1690
1691 void ObjCCompatibleAliasDecl::anchor() { }
1692
1693 ObjCCompatibleAliasDecl *
1694 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1695                                 SourceLocation L,
1696                                 IdentifierInfo *Id,
1697                                 ObjCInterfaceDecl* AliasedClass) {
1698   return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1699 }
1700
1701 ObjCCompatibleAliasDecl *
1702 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1703   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1704   return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1705 }
1706
1707 //===----------------------------------------------------------------------===//
1708 // ObjCPropertyDecl
1709 //===----------------------------------------------------------------------===//
1710
1711 void ObjCPropertyDecl::anchor() { }
1712
1713 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1714                                            SourceLocation L,
1715                                            IdentifierInfo *Id,
1716                                            SourceLocation AtLoc,
1717                                            SourceLocation LParenLoc,
1718                                            TypeSourceInfo *T,
1719                                            PropertyControl propControl) {
1720   return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1721 }
1722
1723 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C, 
1724                                                        unsigned ID) {
1725   void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1726   return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1727                                     SourceLocation(),
1728                                     0);
1729 }
1730
1731 //===----------------------------------------------------------------------===//
1732 // ObjCPropertyImplDecl
1733 //===----------------------------------------------------------------------===//
1734
1735 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1736                                                    DeclContext *DC,
1737                                                    SourceLocation atLoc,
1738                                                    SourceLocation L,
1739                                                    ObjCPropertyDecl *property,
1740                                                    Kind PK,
1741                                                    ObjCIvarDecl *ivar,
1742                                                    SourceLocation ivarLoc) {
1743   return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1744                                       ivarLoc);
1745 }
1746
1747 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C, 
1748                                                                unsigned ID) {
1749   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1750   return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1751                                         0, Dynamic, 0, SourceLocation());
1752 }
1753
1754 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1755   SourceLocation EndLoc = getLocation();
1756   if (IvarLoc.isValid())
1757     EndLoc = IvarLoc;
1758
1759   return SourceRange(AtLoc, EndLoc);
1760 }