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