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