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 ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
447                                      bool isInstance,
448                                      bool shallowCategoryLookup) const {
449   // FIXME: Should make sure no callers ever do this.
450   if (!hasDefinition())
451     return 0;
452 
453   const ObjCInterfaceDecl* ClassDecl = this;
454   ObjCMethodDecl *MethodDecl = 0;
455 
456   if (data().ExternallyCompleted)
457     LoadExternalDefinition();
458 
459   while (ClassDecl != NULL) {
460     if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
461       return MethodDecl;
462 
463     // Didn't find one yet - look through protocols.
464     for (ObjCInterfaceDecl::protocol_iterator I = ClassDecl->protocol_begin(),
465                                               E = ClassDecl->protocol_end();
466            I != E; ++I)
467       if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
468         return MethodDecl;
469 
470     // Didn't find one yet - now look through categories.
471     for (ObjCInterfaceDecl::visible_categories_iterator
472            Cat = ClassDecl->visible_categories_begin(),
473            CatEnd = ClassDecl->visible_categories_end();
474          Cat != CatEnd; ++Cat) {
475       if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
476         return MethodDecl;
477 
478       if (!shallowCategoryLookup) {
479         // Didn't find one yet - look through protocols.
480         const ObjCList<ObjCProtocolDecl> &Protocols =
481           Cat->getReferencedProtocols();
482         for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
483              E = Protocols.end(); I != E; ++I)
484           if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
485             return MethodDecl;
486       }
487     }
488 
489     ClassDecl = ClassDecl->getSuperClass();
490   }
491   return NULL;
492 }
493 
494 // Will search "local" class/category implementations for a method decl.
495 // If failed, then we search in class's root for an instance method.
496 // Returns 0 if no method is found.
497 ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
498                                    const Selector &Sel,
499                                    bool Instance) const {
500   // FIXME: Should make sure no callers ever do this.
501   if (!hasDefinition())
502     return 0;
503 
504   if (data().ExternallyCompleted)
505     LoadExternalDefinition();
506 
507   ObjCMethodDecl *Method = 0;
508   if (ObjCImplementationDecl *ImpDecl = getImplementation())
509     Method = Instance ? ImpDecl->getInstanceMethod(Sel)
510                       : ImpDecl->getClassMethod(Sel);
511 
512   // Look through local category implementations associated with the class.
513   if (!Method)
514     Method = Instance ? getCategoryInstanceMethod(Sel)
515                       : getCategoryClassMethod(Sel);
516 
517   // Before we give up, check if the selector is an instance method.
518   // But only in the root. This matches gcc's behavior and what the
519   // runtime expects.
520   if (!Instance && !Method && !getSuperClass()) {
521     Method = lookupInstanceMethod(Sel);
522     // Look through local category implementations associated
523     // with the root class.
524     if (!Method)
525       Method = lookupPrivateMethod(Sel, true);
526   }
527 
528   if (!Method && getSuperClass())
529     return getSuperClass()->lookupPrivateMethod(Sel, Instance);
530   return Method;
531 }
532 
533 //===----------------------------------------------------------------------===//
534 // ObjCMethodDecl
535 //===----------------------------------------------------------------------===//
536 
537 ObjCMethodDecl *ObjCMethodDecl::Create(ASTContext &C,
538                                        SourceLocation beginLoc,
539                                        SourceLocation endLoc,
540                                        Selector SelInfo, QualType T,
541                                        TypeSourceInfo *ResultTInfo,
542                                        DeclContext *contextDecl,
543                                        bool isInstance,
544                                        bool isVariadic,
545                                        bool isPropertyAccessor,
546                                        bool isImplicitlyDeclared,
547                                        bool isDefined,
548                                        ImplementationControl impControl,
549                                        bool HasRelatedResultType) {
550   return new (C) ObjCMethodDecl(beginLoc, endLoc,
551                                 SelInfo, T, ResultTInfo, contextDecl,
552                                 isInstance, isVariadic, isPropertyAccessor,
553                                 isImplicitlyDeclared, isDefined,
554                                 impControl,
555                                 HasRelatedResultType);
556 }
557 
558 ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
559   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCMethodDecl));
560   return new (Mem) ObjCMethodDecl(SourceLocation(), SourceLocation(),
561                                   Selector(), QualType(), 0, 0);
562 }
563 
564 Stmt *ObjCMethodDecl::getBody() const {
565   return Body.get(getASTContext().getExternalSource());
566 }
567 
568 void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
569   assert(PrevMethod);
570   getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
571   IsRedeclaration = true;
572   PrevMethod->HasRedeclaration = true;
573 }
574 
575 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
576                                          ArrayRef<ParmVarDecl*> Params,
577                                          ArrayRef<SourceLocation> SelLocs) {
578   ParamsAndSelLocs = 0;
579   NumParams = Params.size();
580   if (Params.empty() && SelLocs.empty())
581     return;
582 
583   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
584                   sizeof(SourceLocation) * SelLocs.size();
585   ParamsAndSelLocs = C.Allocate(Size);
586   std::copy(Params.begin(), Params.end(), getParams());
587   std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
588 }
589 
590 void ObjCMethodDecl::getSelectorLocs(
591                                SmallVectorImpl<SourceLocation> &SelLocs) const {
592   for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
593     SelLocs.push_back(getSelectorLoc(i));
594 }
595 
596 void ObjCMethodDecl::setMethodParams(ASTContext &C,
597                                      ArrayRef<ParmVarDecl*> Params,
598                                      ArrayRef<SourceLocation> SelLocs) {
599   assert((!SelLocs.empty() || isImplicit()) &&
600          "No selector locs for non-implicit method");
601   if (isImplicit())
602     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
603 
604   SelLocsKind = hasStandardSelectorLocs(getSelector(), SelLocs, Params,
605                                         DeclEndLoc);
606   if (SelLocsKind != SelLoc_NonStandard)
607     return setParamsAndSelLocs(C, Params, ArrayRef<SourceLocation>());
608 
609   setParamsAndSelLocs(C, Params, SelLocs);
610 }
611 
612 /// \brief A definition will return its interface declaration.
613 /// An interface declaration will return its definition.
614 /// Otherwise it will return itself.
615 ObjCMethodDecl *ObjCMethodDecl::getNextRedeclaration() {
616   ASTContext &Ctx = getASTContext();
617   ObjCMethodDecl *Redecl = 0;
618   if (HasRedeclaration)
619     Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
620   if (Redecl)
621     return Redecl;
622 
623   Decl *CtxD = cast<Decl>(getDeclContext());
624 
625   if (ObjCInterfaceDecl *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
626     if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
627       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
628 
629   } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
630     if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
631       Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
632 
633   } else if (ObjCImplementationDecl *ImplD =
634                dyn_cast<ObjCImplementationDecl>(CtxD)) {
635     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
636       Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
637 
638   } else if (ObjCCategoryImplDecl *CImplD =
639                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
640     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
641       Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
642   }
643 
644   if (!Redecl && isRedeclaration()) {
645     // This is the last redeclaration, go back to the first method.
646     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
647                                                     isInstanceMethod());
648   }
649 
650   return Redecl ? Redecl : this;
651 }
652 
653 ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
654   Decl *CtxD = cast<Decl>(getDeclContext());
655 
656   if (ObjCImplementationDecl *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
657     if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
658       if (ObjCMethodDecl *MD = IFD->getMethod(getSelector(),
659                                               isInstanceMethod()))
660         return MD;
661 
662   } else if (ObjCCategoryImplDecl *CImplD =
663                dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
664     if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
665       if (ObjCMethodDecl *MD = CatD->getMethod(getSelector(),
666                                                isInstanceMethod()))
667         return MD;
668   }
669 
670   if (isRedeclaration())
671     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
672                                                     isInstanceMethod());
673 
674   return this;
675 }
676 
677 SourceLocation ObjCMethodDecl::getLocEnd() const {
678   if (Stmt *Body = getBody())
679     return Body->getLocEnd();
680   return DeclEndLoc;
681 }
682 
683 ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
684   ObjCMethodFamily family = static_cast<ObjCMethodFamily>(Family);
685   if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
686     return family;
687 
688   // Check for an explicit attribute.
689   if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
690     // The unfortunate necessity of mapping between enums here is due
691     // to the attributes framework.
692     switch (attr->getFamily()) {
693     case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
694     case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
695     case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
696     case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
697     case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
698     case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
699     }
700     Family = static_cast<unsigned>(family);
701     return family;
702   }
703 
704   family = getSelector().getMethodFamily();
705   switch (family) {
706   case OMF_None: break;
707 
708   // init only has a conventional meaning for an instance method, and
709   // it has to return an object.
710   case OMF_init:
711     if (!isInstanceMethod() || !getResultType()->isObjCObjectPointerType())
712       family = OMF_None;
713     break;
714 
715   // alloc/copy/new have a conventional meaning for both class and
716   // instance methods, but they require an object return.
717   case OMF_alloc:
718   case OMF_copy:
719   case OMF_mutableCopy:
720   case OMF_new:
721     if (!getResultType()->isObjCObjectPointerType())
722       family = OMF_None;
723     break;
724 
725   // These selectors have a conventional meaning only for instance methods.
726   case OMF_dealloc:
727   case OMF_finalize:
728   case OMF_retain:
729   case OMF_release:
730   case OMF_autorelease:
731   case OMF_retainCount:
732   case OMF_self:
733     if (!isInstanceMethod())
734       family = OMF_None;
735     break;
736 
737   case OMF_performSelector:
738     if (!isInstanceMethod() ||
739         !getResultType()->isObjCIdType())
740       family = OMF_None;
741     else {
742       unsigned noParams = param_size();
743       if (noParams < 1 || noParams > 3)
744         family = OMF_None;
745       else {
746         ObjCMethodDecl::arg_type_iterator it = arg_type_begin();
747         QualType ArgT = (*it);
748         if (!ArgT->isObjCSelType()) {
749           family = OMF_None;
750           break;
751         }
752         while (--noParams) {
753           it++;
754           ArgT = (*it);
755           if (!ArgT->isObjCIdType()) {
756             family = OMF_None;
757             break;
758           }
759         }
760       }
761     }
762     break;
763 
764   }
765 
766   // Cache the result.
767   Family = static_cast<unsigned>(family);
768   return family;
769 }
770 
771 void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
772                                           const ObjCInterfaceDecl *OID) {
773   QualType selfTy;
774   if (isInstanceMethod()) {
775     // There may be no interface context due to error in declaration
776     // of the interface (which has been reported). Recover gracefully.
777     if (OID) {
778       selfTy = Context.getObjCInterfaceType(OID);
779       selfTy = Context.getObjCObjectPointerType(selfTy);
780     } else {
781       selfTy = Context.getObjCIdType();
782     }
783   } else // we have a factory method.
784     selfTy = Context.getObjCClassType();
785 
786   bool selfIsPseudoStrong = false;
787   bool selfIsConsumed = false;
788 
789   if (Context.getLangOpts().ObjCAutoRefCount) {
790     if (isInstanceMethod()) {
791       selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
792 
793       // 'self' is always __strong.  It's actually pseudo-strong except
794       // in init methods (or methods labeled ns_consumes_self), though.
795       Qualifiers qs;
796       qs.setObjCLifetime(Qualifiers::OCL_Strong);
797       selfTy = Context.getQualifiedType(selfTy, qs);
798 
799       // In addition, 'self' is const unless this is an init method.
800       if (getMethodFamily() != OMF_init && !selfIsConsumed) {
801         selfTy = selfTy.withConst();
802         selfIsPseudoStrong = true;
803       }
804     }
805     else {
806       assert(isClassMethod());
807       // 'self' is always const in class methods.
808       selfTy = selfTy.withConst();
809       selfIsPseudoStrong = true;
810     }
811   }
812 
813   ImplicitParamDecl *self
814     = ImplicitParamDecl::Create(Context, this, SourceLocation(),
815                                 &Context.Idents.get("self"), selfTy);
816   setSelfDecl(self);
817 
818   if (selfIsConsumed)
819     self->addAttr(new (Context) NSConsumedAttr(SourceLocation(), Context));
820 
821   if (selfIsPseudoStrong)
822     self->setARCPseudoStrong(true);
823 
824   setCmdDecl(ImplicitParamDecl::Create(Context, this, SourceLocation(),
825                                        &Context.Idents.get("_cmd"),
826                                        Context.getObjCSelType()));
827 }
828 
829 ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
830   if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
831     return ID;
832   if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
833     return CD->getClassInterface();
834   if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
835     return IMD->getClassInterface();
836 
837   assert(!isa<ObjCProtocolDecl>(getDeclContext()) && "It's a protocol method");
838   llvm_unreachable("unknown method context");
839 }
840 
841 static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
842                                             const ObjCMethodDecl *Method,
843                                SmallVectorImpl<const ObjCMethodDecl *> &Methods,
844                                             bool MovedToSuper) {
845   if (!Container)
846     return;
847 
848   // In categories look for overriden methods from protocols. A method from
849   // category is not "overriden" since it is considered as the "same" method
850   // (same USR) as the one from the interface.
851   if (const ObjCCategoryDecl *
852         Category = dyn_cast<ObjCCategoryDecl>(Container)) {
853     // Check whether we have a matching method at this category but only if we
854     // are at the super class level.
855     if (MovedToSuper)
856       if (ObjCMethodDecl *
857             Overridden = Container->getMethod(Method->getSelector(),
858                                               Method->isInstanceMethod(),
859                                               /*AllowHidden=*/true))
860         if (Method != Overridden) {
861           // We found an override at this category; there is no need to look
862           // into its protocols.
863           Methods.push_back(Overridden);
864           return;
865         }
866 
867     for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
868                                           PEnd = Category->protocol_end();
869          P != PEnd; ++P)
870       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
871     return;
872   }
873 
874   // Check whether we have a matching method at this level.
875   if (const ObjCMethodDecl *
876         Overridden = Container->getMethod(Method->getSelector(),
877                                           Method->isInstanceMethod(),
878                                           /*AllowHidden=*/true))
879     if (Method != Overridden) {
880       // We found an override at this level; there is no need to look
881       // into other protocols or categories.
882       Methods.push_back(Overridden);
883       return;
884     }
885 
886   if (const ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
887     for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
888                                           PEnd = Protocol->protocol_end();
889          P != PEnd; ++P)
890       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
891   }
892 
893   if (const ObjCInterfaceDecl *
894         Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
895     for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
896                                            PEnd = Interface->protocol_end();
897          P != PEnd; ++P)
898       CollectOverriddenMethodsRecurse(*P, Method, Methods, MovedToSuper);
899 
900     for (ObjCInterfaceDecl::known_categories_iterator
901            Cat = Interface->known_categories_begin(),
902            CatEnd = Interface->known_categories_end();
903          Cat != CatEnd; ++Cat) {
904       CollectOverriddenMethodsRecurse(*Cat, Method, Methods,
905                                       MovedToSuper);
906     }
907 
908     if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
909       return CollectOverriddenMethodsRecurse(Super, Method, Methods,
910                                              /*MovedToSuper=*/true);
911   }
912 }
913 
914 static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
915                                             const ObjCMethodDecl *Method,
916                              SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
917   CollectOverriddenMethodsRecurse(Container, Method, Methods,
918                                   /*MovedToSuper=*/false);
919 }
920 
921 static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
922                           SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
923   assert(Method->isOverriding());
924 
925   if (const ObjCProtocolDecl *
926         ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
927     CollectOverriddenMethods(ProtD, Method, overridden);
928 
929   } else if (const ObjCImplDecl *
930                IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
931     const ObjCInterfaceDecl *ID = IMD->getClassInterface();
932     if (!ID)
933       return;
934     // Start searching for overridden methods using the method from the
935     // interface as starting point.
936     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
937                                                     Method->isInstanceMethod(),
938                                                     /*AllowHidden=*/true))
939       Method = IFaceMeth;
940     CollectOverriddenMethods(ID, Method, overridden);
941 
942   } else if (const ObjCCategoryDecl *
943                CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
944     const ObjCInterfaceDecl *ID = CatD->getClassInterface();
945     if (!ID)
946       return;
947     // Start searching for overridden methods using the method from the
948     // interface as starting point.
949     if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
950                                                      Method->isInstanceMethod(),
951                                                      /*AllowHidden=*/true))
952       Method = IFaceMeth;
953     CollectOverriddenMethods(ID, Method, overridden);
954 
955   } else {
956     CollectOverriddenMethods(
957                   dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
958                   Method, overridden);
959   }
960 }
961 
962 void ObjCMethodDecl::getOverriddenMethods(
963                     SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
964   const ObjCMethodDecl *Method = this;
965 
966   if (Method->isRedeclaration()) {
967     Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
968                    getMethod(Method->getSelector(), Method->isInstanceMethod());
969   }
970 
971   if (Method->isOverriding()) {
972     collectOverriddenMethodsSlow(Method, Overridden);
973     assert(!Overridden.empty() &&
974            "ObjCMethodDecl's overriding bit is not as expected");
975   }
976 }
977 
978 const ObjCPropertyDecl *
979 ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
980   Selector Sel = getSelector();
981   unsigned NumArgs = Sel.getNumArgs();
982   if (NumArgs > 1)
983     return 0;
984 
985   if (!isInstanceMethod() || getMethodFamily() != OMF_None)
986     return 0;
987 
988   if (isPropertyAccessor()) {
989     const ObjCContainerDecl *Container = cast<ObjCContainerDecl>(getParent());
990     // If container is class extension, find its primary class.
991     if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Container))
992       if (CatDecl->IsClassExtension())
993         Container = CatDecl->getClassInterface();
994 
995     bool IsGetter = (NumArgs == 0);
996 
997     for (ObjCContainerDecl::prop_iterator I = Container->prop_begin(),
998                                           E = Container->prop_end();
999          I != E; ++I) {
1000       Selector NextSel = IsGetter ? (*I)->getGetterName()
1001                                   : (*I)->getSetterName();
1002       if (NextSel == Sel)
1003         return *I;
1004     }
1005 
1006     llvm_unreachable("Marked as a property accessor but no property found!");
1007   }
1008 
1009   if (!CheckOverrides)
1010     return 0;
1011 
1012   typedef SmallVector<const ObjCMethodDecl *, 8> OverridesTy;
1013   OverridesTy Overrides;
1014   getOverriddenMethods(Overrides);
1015   for (OverridesTy::const_iterator I = Overrides.begin(), E = Overrides.end();
1016        I != E; ++I) {
1017     if (const ObjCPropertyDecl *Prop = (*I)->findPropertyDecl(false))
1018       return Prop;
1019   }
1020 
1021   return 0;
1022 
1023 }
1024 
1025 //===----------------------------------------------------------------------===//
1026 // ObjCInterfaceDecl
1027 //===----------------------------------------------------------------------===//
1028 
1029 ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1030                                              DeclContext *DC,
1031                                              SourceLocation atLoc,
1032                                              IdentifierInfo *Id,
1033                                              ObjCInterfaceDecl *PrevDecl,
1034                                              SourceLocation ClassLoc,
1035                                              bool isInternal){
1036   ObjCInterfaceDecl *Result = new (C) ObjCInterfaceDecl(DC, atLoc, Id, ClassLoc,
1037                                                         PrevDecl, isInternal);
1038   Result->Data.setInt(!C.getLangOpts().Modules);
1039   C.getObjCInterfaceType(Result, PrevDecl);
1040   return Result;
1041 }
1042 
1043 ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(ASTContext &C,
1044                                                          unsigned ID) {
1045   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCInterfaceDecl));
1046   ObjCInterfaceDecl *Result = new (Mem) ObjCInterfaceDecl(0, SourceLocation(),
1047                                                           0, SourceLocation(),
1048                                                           0, false);
1049   Result->Data.setInt(!C.getLangOpts().Modules);
1050   return Result;
1051 }
1052 
1053 ObjCInterfaceDecl::
1054 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
1055                   SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1056                   bool isInternal)
1057   : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, atLoc),
1058     TypeForDecl(0), Data()
1059 {
1060   setPreviousDeclaration(PrevDecl);
1061 
1062   // Copy the 'data' pointer over.
1063   if (PrevDecl)
1064     Data = PrevDecl->Data;
1065 
1066   setImplicit(isInternal);
1067 }
1068 
1069 void ObjCInterfaceDecl::LoadExternalDefinition() const {
1070   assert(data().ExternallyCompleted && "Class is not externally completed");
1071   data().ExternallyCompleted = false;
1072   getASTContext().getExternalSource()->CompleteType(
1073                                         const_cast<ObjCInterfaceDecl *>(this));
1074 }
1075 
1076 void ObjCInterfaceDecl::setExternallyCompleted() {
1077   assert(getASTContext().getExternalSource() &&
1078          "Class can't be externally completed without an external source");
1079   assert(hasDefinition() &&
1080          "Forward declarations can't be externally completed");
1081   data().ExternallyCompleted = true;
1082 }
1083 
1084 ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1085   if (const ObjCInterfaceDecl *Def = getDefinition()) {
1086     if (data().ExternallyCompleted)
1087       LoadExternalDefinition();
1088 
1089     return getASTContext().getObjCImplementation(
1090              const_cast<ObjCInterfaceDecl*>(Def));
1091   }
1092 
1093   // FIXME: Should make sure no callers ever do this.
1094   return 0;
1095 }
1096 
1097 void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1098   getASTContext().setObjCImplementation(getDefinition(), ImplD);
1099 }
1100 
1101 namespace {
1102   struct SynthesizeIvarChunk {
1103     uint64_t Size;
1104     ObjCIvarDecl *Ivar;
1105     SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1106       : Size(size), Ivar(ivar) {}
1107   };
1108 
1109   bool operator<(const SynthesizeIvarChunk & LHS,
1110                  const SynthesizeIvarChunk &RHS) {
1111       return LHS.Size < RHS.Size;
1112   }
1113 }
1114 
1115 /// all_declared_ivar_begin - return first ivar declared in this class,
1116 /// its extensions and its implementation. Lazily build the list on first
1117 /// access.
1118 ///
1119 /// Caveat: The list returned by this method reflects the current
1120 /// state of the parser. The cache will be updated for every ivar
1121 /// added by an extension or the implementation when they are
1122 /// encountered.
1123 /// See also ObjCIvarDecl::Create().
1124 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1125   // FIXME: Should make sure no callers ever do this.
1126   if (!hasDefinition())
1127     return 0;
1128 
1129   ObjCIvarDecl *curIvar = 0;
1130   if (!data().IvarList) {
1131     if (!ivar_empty()) {
1132       ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1133       data().IvarList = *I; ++I;
1134       for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1135         curIvar->setNextIvar(*I);
1136     }
1137 
1138     for (ObjCInterfaceDecl::known_extensions_iterator
1139            Ext = known_extensions_begin(),
1140            ExtEnd = known_extensions_end();
1141          Ext != ExtEnd; ++Ext) {
1142       if (!Ext->ivar_empty()) {
1143         ObjCCategoryDecl::ivar_iterator
1144           I = Ext->ivar_begin(),
1145           E = Ext->ivar_end();
1146         if (!data().IvarList) {
1147           data().IvarList = *I; ++I;
1148           curIvar = data().IvarList;
1149         }
1150         for ( ;I != E; curIvar = *I, ++I)
1151           curIvar->setNextIvar(*I);
1152       }
1153     }
1154     data().IvarListMissingImplementation = true;
1155   }
1156 
1157   // cached and complete!
1158   if (!data().IvarListMissingImplementation)
1159       return data().IvarList;
1160 
1161   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1162     data().IvarListMissingImplementation = false;
1163     if (!ImplDecl->ivar_empty()) {
1164       SmallVector<SynthesizeIvarChunk, 16> layout;
1165       for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
1166            E = ImplDecl->ivar_end(); I != E; ++I) {
1167         ObjCIvarDecl *IV = *I;
1168         if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1169           layout.push_back(SynthesizeIvarChunk(
1170                              IV->getASTContext().getTypeSize(IV->getType()), IV));
1171           continue;
1172         }
1173         if (!data().IvarList)
1174           data().IvarList = *I;
1175         else
1176           curIvar->setNextIvar(*I);
1177         curIvar = *I;
1178       }
1179 
1180       if (!layout.empty()) {
1181         // Order synthesized ivars by their size.
1182         std::stable_sort(layout.begin(), layout.end());
1183         unsigned Ix = 0, EIx = layout.size();
1184         if (!data().IvarList) {
1185           data().IvarList = layout[0].Ivar; Ix++;
1186           curIvar = data().IvarList;
1187         }
1188         for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1189           curIvar->setNextIvar(layout[Ix].Ivar);
1190       }
1191     }
1192   }
1193   return data().IvarList;
1194 }
1195 
1196 /// FindCategoryDeclaration - Finds category declaration in the list of
1197 /// categories for this class and returns it. Name of the category is passed
1198 /// in 'CategoryId'. If category not found, return 0;
1199 ///
1200 ObjCCategoryDecl *
1201 ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1202   // FIXME: Should make sure no callers ever do this.
1203   if (!hasDefinition())
1204     return 0;
1205 
1206   if (data().ExternallyCompleted)
1207     LoadExternalDefinition();
1208 
1209   for (visible_categories_iterator Cat = visible_categories_begin(),
1210                                    CatEnd = visible_categories_end();
1211        Cat != CatEnd;
1212        ++Cat) {
1213     if (Cat->getIdentifier() == CategoryId)
1214       return *Cat;
1215   }
1216 
1217   return 0;
1218 }
1219 
1220 ObjCMethodDecl *
1221 ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1222   for (visible_categories_iterator Cat = visible_categories_begin(),
1223                                    CatEnd = visible_categories_end();
1224        Cat != CatEnd;
1225        ++Cat) {
1226     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1227       if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1228         return MD;
1229   }
1230 
1231   return 0;
1232 }
1233 
1234 ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1235   for (visible_categories_iterator Cat = visible_categories_begin(),
1236                                    CatEnd = visible_categories_end();
1237        Cat != CatEnd;
1238        ++Cat) {
1239     if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1240       if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1241         return MD;
1242   }
1243 
1244   return 0;
1245 }
1246 
1247 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1248 /// has been implemented in IDecl class, its super class or categories (if
1249 /// lookupCategory is true).
1250 bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1251                                     bool lookupCategory,
1252                                     bool RHSIsQualifiedID) {
1253   if (!hasDefinition())
1254     return false;
1255 
1256   ObjCInterfaceDecl *IDecl = this;
1257   // 1st, look up the class.
1258   for (ObjCInterfaceDecl::protocol_iterator
1259         PI = IDecl->protocol_begin(), E = IDecl->protocol_end(); PI != E; ++PI){
1260     if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1261       return true;
1262     // This is dubious and is added to be compatible with gcc.  In gcc, it is
1263     // also allowed assigning a protocol-qualified 'id' type to a LHS object
1264     // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1265     // object. This IMO, should be a bug.
1266     // FIXME: Treat this as an extension, and flag this as an error when GCC
1267     // extensions are not enabled.
1268     if (RHSIsQualifiedID &&
1269         getASTContext().ProtocolCompatibleWithProtocol(*PI, lProto))
1270       return true;
1271   }
1272 
1273   // 2nd, look up the category.
1274   if (lookupCategory)
1275     for (visible_categories_iterator Cat = visible_categories_begin(),
1276                                      CatEnd = visible_categories_end();
1277          Cat != CatEnd;
1278          ++Cat) {
1279       for (ObjCCategoryDecl::protocol_iterator PI = Cat->protocol_begin(),
1280                                                E = Cat->protocol_end();
1281            PI != E; ++PI)
1282         if (getASTContext().ProtocolCompatibleWithProtocol(lProto, *PI))
1283           return true;
1284     }
1285 
1286   // 3rd, look up the super class(s)
1287   if (IDecl->getSuperClass())
1288     return
1289   IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1290                                                   RHSIsQualifiedID);
1291 
1292   return false;
1293 }
1294 
1295 //===----------------------------------------------------------------------===//
1296 // ObjCIvarDecl
1297 //===----------------------------------------------------------------------===//
1298 
1299 void ObjCIvarDecl::anchor() { }
1300 
1301 ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1302                                    SourceLocation StartLoc,
1303                                    SourceLocation IdLoc, IdentifierInfo *Id,
1304                                    QualType T, TypeSourceInfo *TInfo,
1305                                    AccessControl ac, Expr *BW,
1306                                    bool synthesized) {
1307   if (DC) {
1308     // Ivar's can only appear in interfaces, implementations (via synthesized
1309     // properties), and class extensions (via direct declaration, or synthesized
1310     // properties).
1311     //
1312     // FIXME: This should really be asserting this:
1313     //   (isa<ObjCCategoryDecl>(DC) &&
1314     //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1315     // but unfortunately we sometimes place ivars into non-class extension
1316     // categories on error. This breaks an AST invariant, and should not be
1317     // fixed.
1318     assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1319             isa<ObjCCategoryDecl>(DC)) &&
1320            "Invalid ivar decl context!");
1321     // Once a new ivar is created in any of class/class-extension/implementation
1322     // decl contexts, the previously built IvarList must be rebuilt.
1323     ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1324     if (!ID) {
1325       if (ObjCImplementationDecl *IM = dyn_cast<ObjCImplementationDecl>(DC))
1326         ID = IM->getClassInterface();
1327       else
1328         ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1329     }
1330     ID->setIvarList(0);
1331   }
1332 
1333   return new (C) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo,
1334                               ac, BW, synthesized);
1335 }
1336 
1337 ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1338   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCIvarDecl));
1339   return new (Mem) ObjCIvarDecl(0, SourceLocation(), SourceLocation(), 0,
1340                                 QualType(), 0, ObjCIvarDecl::None, 0, false);
1341 }
1342 
1343 const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1344   const ObjCContainerDecl *DC = cast<ObjCContainerDecl>(getDeclContext());
1345 
1346   switch (DC->getKind()) {
1347   default:
1348   case ObjCCategoryImpl:
1349   case ObjCProtocol:
1350     llvm_unreachable("invalid ivar container!");
1351 
1352     // Ivars can only appear in class extension categories.
1353   case ObjCCategory: {
1354     const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(DC);
1355     assert(CD->IsClassExtension() && "invalid container for ivar!");
1356     return CD->getClassInterface();
1357   }
1358 
1359   case ObjCImplementation:
1360     return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1361 
1362   case ObjCInterface:
1363     return cast<ObjCInterfaceDecl>(DC);
1364   }
1365 }
1366 
1367 //===----------------------------------------------------------------------===//
1368 // ObjCAtDefsFieldDecl
1369 //===----------------------------------------------------------------------===//
1370 
1371 void ObjCAtDefsFieldDecl::anchor() { }
1372 
1373 ObjCAtDefsFieldDecl
1374 *ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1375                              SourceLocation StartLoc,  SourceLocation IdLoc,
1376                              IdentifierInfo *Id, QualType T, Expr *BW) {
1377   return new (C) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1378 }
1379 
1380 ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1381                                                              unsigned ID) {
1382   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCAtDefsFieldDecl));
1383   return new (Mem) ObjCAtDefsFieldDecl(0, SourceLocation(), SourceLocation(),
1384                                        0, QualType(), 0);
1385 }
1386 
1387 //===----------------------------------------------------------------------===//
1388 // ObjCProtocolDecl
1389 //===----------------------------------------------------------------------===//
1390 
1391 void ObjCProtocolDecl::anchor() { }
1392 
1393 ObjCProtocolDecl::ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1394                                    SourceLocation nameLoc,
1395                                    SourceLocation atStartLoc,
1396                                    ObjCProtocolDecl *PrevDecl)
1397   : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc), Data()
1398 {
1399   setPreviousDeclaration(PrevDecl);
1400   if (PrevDecl)
1401     Data = PrevDecl->Data;
1402 }
1403 
1404 ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1405                                            IdentifierInfo *Id,
1406                                            SourceLocation nameLoc,
1407                                            SourceLocation atStartLoc,
1408                                            ObjCProtocolDecl *PrevDecl) {
1409   ObjCProtocolDecl *Result
1410     = new (C) ObjCProtocolDecl(DC, Id, nameLoc, atStartLoc, PrevDecl);
1411   Result->Data.setInt(!C.getLangOpts().Modules);
1412   return Result;
1413 }
1414 
1415 ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1416                                                        unsigned ID) {
1417   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCProtocolDecl));
1418   ObjCProtocolDecl *Result = new (Mem) ObjCProtocolDecl(0, 0, SourceLocation(),
1419                                                         SourceLocation(), 0);
1420   Result->Data.setInt(!C.getLangOpts().Modules);
1421   return Result;
1422 }
1423 
1424 ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1425   ObjCProtocolDecl *PDecl = this;
1426 
1427   if (Name == getIdentifier())
1428     return PDecl;
1429 
1430   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1431     if ((PDecl = (*I)->lookupProtocolNamed(Name)))
1432       return PDecl;
1433 
1434   return NULL;
1435 }
1436 
1437 // lookupMethod - Lookup a instance/class method in the protocol and protocols
1438 // it inherited.
1439 ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1440                                                bool isInstance) const {
1441   ObjCMethodDecl *MethodDecl = NULL;
1442 
1443   // If there is no definition or the definition is hidden, we don't find
1444   // anything.
1445   const ObjCProtocolDecl *Def = getDefinition();
1446   if (!Def || Def->isHidden())
1447     return NULL;
1448 
1449   if ((MethodDecl = getMethod(Sel, isInstance)))
1450     return MethodDecl;
1451 
1452   for (protocol_iterator I = protocol_begin(), E = protocol_end(); I != E; ++I)
1453     if ((MethodDecl = (*I)->lookupMethod(Sel, isInstance)))
1454       return MethodDecl;
1455   return NULL;
1456 }
1457 
1458 void ObjCProtocolDecl::allocateDefinitionData() {
1459   assert(!Data.getPointer() && "Protocol already has a definition!");
1460   Data.setPointer(new (getASTContext()) DefinitionData);
1461   Data.getPointer()->Definition = this;
1462 }
1463 
1464 void ObjCProtocolDecl::startDefinition() {
1465   allocateDefinitionData();
1466 
1467   // Update all of the declarations with a pointer to the definition.
1468   for (redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1469        RD != RDEnd; ++RD)
1470     RD->Data = this->Data;
1471 }
1472 
1473 void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1474                                                     PropertyDeclOrder &PO) const {
1475 
1476   if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1477     for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1478          E = PDecl->prop_end(); P != E; ++P) {
1479       ObjCPropertyDecl *Prop = *P;
1480       // Insert into PM if not there already.
1481       PM.insert(std::make_pair(Prop->getIdentifier(), Prop));
1482       PO.push_back(Prop);
1483     }
1484     // Scan through protocol's protocols.
1485     for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1486          E = PDecl->protocol_end(); PI != E; ++PI)
1487       (*PI)->collectPropertiesToImplement(PM, PO);
1488   }
1489 }
1490 
1491 
1492 //===----------------------------------------------------------------------===//
1493 // ObjCCategoryDecl
1494 //===----------------------------------------------------------------------===//
1495 
1496 void ObjCCategoryDecl::anchor() { }
1497 
1498 ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
1499                                            SourceLocation AtLoc,
1500                                            SourceLocation ClassNameLoc,
1501                                            SourceLocation CategoryNameLoc,
1502                                            IdentifierInfo *Id,
1503                                            ObjCInterfaceDecl *IDecl,
1504                                            SourceLocation IvarLBraceLoc,
1505                                            SourceLocation IvarRBraceLoc) {
1506   ObjCCategoryDecl *CatDecl = new (C) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc,
1507                                                        CategoryNameLoc, Id,
1508                                                        IDecl,
1509                                                        IvarLBraceLoc, IvarRBraceLoc);
1510   if (IDecl) {
1511     // Link this category into its class's category list.
1512     CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
1513     if (IDecl->hasDefinition()) {
1514       IDecl->setCategoryListRaw(CatDecl);
1515       if (ASTMutationListener *L = C.getASTMutationListener())
1516         L->AddedObjCCategoryToInterface(CatDecl, IDecl);
1517     }
1518   }
1519 
1520   return CatDecl;
1521 }
1522 
1523 ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
1524                                                        unsigned ID) {
1525   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryDecl));
1526   return new (Mem) ObjCCategoryDecl(0, SourceLocation(), SourceLocation(),
1527                                     SourceLocation(), 0, 0);
1528 }
1529 
1530 ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
1531   return getASTContext().getObjCImplementation(
1532                                            const_cast<ObjCCategoryDecl*>(this));
1533 }
1534 
1535 void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
1536   getASTContext().setObjCImplementation(this, ImplD);
1537 }
1538 
1539 
1540 //===----------------------------------------------------------------------===//
1541 // ObjCCategoryImplDecl
1542 //===----------------------------------------------------------------------===//
1543 
1544 void ObjCCategoryImplDecl::anchor() { }
1545 
1546 ObjCCategoryImplDecl *
1547 ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
1548                              IdentifierInfo *Id,
1549                              ObjCInterfaceDecl *ClassInterface,
1550                              SourceLocation nameLoc,
1551                              SourceLocation atStartLoc,
1552                              SourceLocation CategoryNameLoc) {
1553   if (ClassInterface && ClassInterface->hasDefinition())
1554     ClassInterface = ClassInterface->getDefinition();
1555   return new (C) ObjCCategoryImplDecl(DC, Id, ClassInterface,
1556                                       nameLoc, atStartLoc, CategoryNameLoc);
1557 }
1558 
1559 ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
1560                                                                unsigned ID) {
1561   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCategoryImplDecl));
1562   return new (Mem) ObjCCategoryImplDecl(0, 0, 0, SourceLocation(),
1563                                         SourceLocation(), SourceLocation());
1564 }
1565 
1566 ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
1567   // The class interface might be NULL if we are working with invalid code.
1568   if (const ObjCInterfaceDecl *ID = getClassInterface())
1569     return ID->FindCategoryDeclaration(getIdentifier());
1570   return 0;
1571 }
1572 
1573 
1574 void ObjCImplDecl::anchor() { }
1575 
1576 void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
1577   // FIXME: The context should be correct before we get here.
1578   property->setLexicalDeclContext(this);
1579   addDecl(property);
1580 }
1581 
1582 void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
1583   ASTContext &Ctx = getASTContext();
1584 
1585   if (ObjCImplementationDecl *ImplD
1586         = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
1587     if (IFace)
1588       Ctx.setObjCImplementation(IFace, ImplD);
1589 
1590   } else if (ObjCCategoryImplDecl *ImplD =
1591              dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
1592     if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
1593       Ctx.setObjCImplementation(CD, ImplD);
1594   }
1595 
1596   ClassInterface = IFace;
1597 }
1598 
1599 /// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
1600 /// properties implemented in this \@implementation block and returns
1601 /// the implemented property that uses it.
1602 ///
1603 ObjCPropertyImplDecl *ObjCImplDecl::
1604 FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
1605   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1606     ObjCPropertyImplDecl *PID = *i;
1607     if (PID->getPropertyIvarDecl() &&
1608         PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
1609       return PID;
1610   }
1611   return 0;
1612 }
1613 
1614 /// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
1615 /// added to the list of those properties \@synthesized/\@dynamic in this
1616 /// category \@implementation block.
1617 ///
1618 ObjCPropertyImplDecl *ObjCImplDecl::
1619 FindPropertyImplDecl(IdentifierInfo *Id) const {
1620   for (propimpl_iterator i = propimpl_begin(), e = propimpl_end(); i != e; ++i){
1621     ObjCPropertyImplDecl *PID = *i;
1622     if (PID->getPropertyDecl()->getIdentifier() == Id)
1623       return PID;
1624   }
1625   return 0;
1626 }
1627 
1628 raw_ostream &clang::operator<<(raw_ostream &OS,
1629                                const ObjCCategoryImplDecl &CID) {
1630   OS << CID.getName();
1631   return OS;
1632 }
1633 
1634 //===----------------------------------------------------------------------===//
1635 // ObjCImplementationDecl
1636 //===----------------------------------------------------------------------===//
1637 
1638 void ObjCImplementationDecl::anchor() { }
1639 
1640 ObjCImplementationDecl *
1641 ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
1642                                ObjCInterfaceDecl *ClassInterface,
1643                                ObjCInterfaceDecl *SuperDecl,
1644                                SourceLocation nameLoc,
1645                                SourceLocation atStartLoc,
1646                                SourceLocation IvarLBraceLoc,
1647                                SourceLocation IvarRBraceLoc) {
1648   if (ClassInterface && ClassInterface->hasDefinition())
1649     ClassInterface = ClassInterface->getDefinition();
1650   return new (C) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
1651                                         nameLoc, atStartLoc,
1652                                         IvarLBraceLoc, IvarRBraceLoc);
1653 }
1654 
1655 ObjCImplementationDecl *
1656 ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1657   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCImplementationDecl));
1658   return new (Mem) ObjCImplementationDecl(0, 0, 0, SourceLocation(),
1659                                           SourceLocation());
1660 }
1661 
1662 void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1663                                              CXXCtorInitializer ** initializers,
1664                                                  unsigned numInitializers) {
1665   if (numInitializers > 0) {
1666     NumIvarInitializers = numInitializers;
1667     CXXCtorInitializer **ivarInitializers =
1668     new (C) CXXCtorInitializer*[NumIvarInitializers];
1669     memcpy(ivarInitializers, initializers,
1670            numInitializers * sizeof(CXXCtorInitializer*));
1671     IvarInitializers = ivarInitializers;
1672   }
1673 }
1674 
1675 raw_ostream &clang::operator<<(raw_ostream &OS,
1676                                const ObjCImplementationDecl &ID) {
1677   OS << ID.getName();
1678   return OS;
1679 }
1680 
1681 //===----------------------------------------------------------------------===//
1682 // ObjCCompatibleAliasDecl
1683 //===----------------------------------------------------------------------===//
1684 
1685 void ObjCCompatibleAliasDecl::anchor() { }
1686 
1687 ObjCCompatibleAliasDecl *
1688 ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
1689                                 SourceLocation L,
1690                                 IdentifierInfo *Id,
1691                                 ObjCInterfaceDecl* AliasedClass) {
1692   return new (C) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
1693 }
1694 
1695 ObjCCompatibleAliasDecl *
1696 ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1697   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCCompatibleAliasDecl));
1698   return new (Mem) ObjCCompatibleAliasDecl(0, SourceLocation(), 0, 0);
1699 }
1700 
1701 //===----------------------------------------------------------------------===//
1702 // ObjCPropertyDecl
1703 //===----------------------------------------------------------------------===//
1704 
1705 void ObjCPropertyDecl::anchor() { }
1706 
1707 ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
1708                                            SourceLocation L,
1709                                            IdentifierInfo *Id,
1710                                            SourceLocation AtLoc,
1711                                            SourceLocation LParenLoc,
1712                                            TypeSourceInfo *T,
1713                                            PropertyControl propControl) {
1714   return new (C) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T);
1715 }
1716 
1717 ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
1718                                                        unsigned ID) {
1719   void * Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyDecl));
1720   return new (Mem) ObjCPropertyDecl(0, SourceLocation(), 0, SourceLocation(),
1721                                     SourceLocation(),
1722                                     0);
1723 }
1724 
1725 //===----------------------------------------------------------------------===//
1726 // ObjCPropertyImplDecl
1727 //===----------------------------------------------------------------------===//
1728 
1729 ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
1730                                                    DeclContext *DC,
1731                                                    SourceLocation atLoc,
1732                                                    SourceLocation L,
1733                                                    ObjCPropertyDecl *property,
1734                                                    Kind PK,
1735                                                    ObjCIvarDecl *ivar,
1736                                                    SourceLocation ivarLoc) {
1737   return new (C) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
1738                                       ivarLoc);
1739 }
1740 
1741 ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
1742                                                                unsigned ID) {
1743   void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ObjCPropertyImplDecl));
1744   return new (Mem) ObjCPropertyImplDecl(0, SourceLocation(), SourceLocation(),
1745                                         0, Dynamic, 0, SourceLocation());
1746 }
1747 
1748 SourceRange ObjCPropertyImplDecl::getSourceRange() const {
1749   SourceLocation EndLoc = getLocation();
1750   if (IvarLoc.isValid())
1751     EndLoc = IvarLoc;
1752 
1753   return SourceRange(AtLoc, EndLoc);
1754 }
1755