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