1 //===--- DeclCXX.cpp - C++ 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 C++ related Decl classes.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTLambda.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/TypeLoc.h"
22 #include "clang/Basic/IdentifierTable.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 using namespace clang;
26 
27 //===----------------------------------------------------------------------===//
28 // Decl Allocation/Deallocation Method Implementations
29 //===----------------------------------------------------------------------===//
30 
31 void AccessSpecDecl::anchor() { }
32 
33 AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34   return new (C, ID) AccessSpecDecl(EmptyShell());
35 }
36 
37 void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
38   ExternalASTSource *Source = C.getExternalSource();
39   assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
40   assert(Source && "getFromExternalSource with no external source");
41 
42   for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
43     I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
44         reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
45   Impl.Decls.setLazy(false);
46 }
47 
48 CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
49     : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
50       Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
51       Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
52       HasPrivateFields(false), HasProtectedFields(false),
53       HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
54       HasOnlyCMembers(true), HasInClassInitializer(false),
55       HasUninitializedReferenceMember(false), HasUninitializedFields(false),
56       NeedOverloadResolutionForMoveConstructor(false),
57       NeedOverloadResolutionForMoveAssignment(false),
58       NeedOverloadResolutionForDestructor(false),
59       DefaultedMoveConstructorIsDeleted(false),
60       DefaultedMoveAssignmentIsDeleted(false),
61       DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
62       DeclaredNonTrivialSpecialMembers(0), HasIrrelevantDestructor(true),
63       HasConstexprNonCopyMoveConstructor(false),
64       DefaultedDefaultConstructorIsConstexpr(true),
65       HasConstexprDefaultConstructor(false),
66       HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
67       UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
68       ImplicitCopyConstructorHasConstParam(true),
69       ImplicitCopyAssignmentHasConstParam(true),
70       HasDeclaredCopyConstructorWithConstParam(false),
71       HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
72       IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
73       VBases(), Definition(D), FirstFriend() {}
74 
75 CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
76   return Bases.get(Definition->getASTContext().getExternalSource());
77 }
78 
79 CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
80   return VBases.get(Definition->getASTContext().getExternalSource());
81 }
82 
83 CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
84                              DeclContext *DC, SourceLocation StartLoc,
85                              SourceLocation IdLoc, IdentifierInfo *Id,
86                              CXXRecordDecl *PrevDecl)
87     : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
88       DefinitionData(PrevDecl ? PrevDecl->DefinitionData
89                               : DefinitionDataPtr(this)),
90       TemplateOrInstantiation() {}
91 
92 CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
93                                      DeclContext *DC, SourceLocation StartLoc,
94                                      SourceLocation IdLoc, IdentifierInfo *Id,
95                                      CXXRecordDecl* PrevDecl,
96                                      bool DelayTypeCreation) {
97   CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
98                                                IdLoc, Id, PrevDecl);
99   R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
100 
101   // FIXME: DelayTypeCreation seems like such a hack
102   if (!DelayTypeCreation)
103     C.getTypeDeclType(R, PrevDecl);
104   return R;
105 }
106 
107 CXXRecordDecl *
108 CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
109                             TypeSourceInfo *Info, SourceLocation Loc,
110                             bool Dependent, bool IsGeneric,
111                             LambdaCaptureDefault CaptureDefault) {
112   CXXRecordDecl *R =
113       new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
114                                 nullptr, nullptr);
115   R->IsBeingDefined = true;
116   R->DefinitionData =
117       new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
118                                           CaptureDefault);
119   R->MayHaveOutOfDateDef = false;
120   R->setImplicit(true);
121   C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
122   return R;
123 }
124 
125 CXXRecordDecl *
126 CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
127   CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
128       CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
129       nullptr, nullptr);
130   R->MayHaveOutOfDateDef = false;
131   return R;
132 }
133 
134 void
135 CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
136                         unsigned NumBases) {
137   ASTContext &C = getASTContext();
138 
139   if (!data().Bases.isOffset() && data().NumBases > 0)
140     C.Deallocate(data().getBases());
141 
142   if (NumBases) {
143     // C++ [dcl.init.aggr]p1:
144     //   An aggregate is [...] a class with [...] no base classes [...].
145     data().Aggregate = false;
146 
147     // C++ [class]p4:
148     //   A POD-struct is an aggregate class...
149     data().PlainOldData = false;
150   }
151 
152   // The set of seen virtual base types.
153   llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
154 
155   // The virtual bases of this class.
156   SmallVector<const CXXBaseSpecifier *, 8> VBases;
157 
158   data().Bases = new(C) CXXBaseSpecifier [NumBases];
159   data().NumBases = NumBases;
160   for (unsigned i = 0; i < NumBases; ++i) {
161     data().getBases()[i] = *Bases[i];
162     // Keep track of inherited vbases for this base class.
163     const CXXBaseSpecifier *Base = Bases[i];
164     QualType BaseType = Base->getType();
165     // Skip dependent types; we can't do any checking on them now.
166     if (BaseType->isDependentType())
167       continue;
168     CXXRecordDecl *BaseClassDecl
169       = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
170 
171     // A class with a non-empty base class is not empty.
172     // FIXME: Standard ref?
173     if (!BaseClassDecl->isEmpty()) {
174       if (!data().Empty) {
175         // C++0x [class]p7:
176         //   A standard-layout class is a class that:
177         //    [...]
178         //    -- either has no non-static data members in the most derived
179         //       class and at most one base class with non-static data members,
180         //       or has no base classes with non-static data members, and
181         // If this is the second non-empty base, then neither of these two
182         // clauses can be true.
183         data().IsStandardLayout = false;
184       }
185 
186       data().Empty = false;
187       data().HasNoNonEmptyBases = false;
188     }
189 
190     // C++ [class.virtual]p1:
191     //   A class that declares or inherits a virtual function is called a
192     //   polymorphic class.
193     if (BaseClassDecl->isPolymorphic())
194       data().Polymorphic = true;
195 
196     // C++0x [class]p7:
197     //   A standard-layout class is a class that: [...]
198     //    -- has no non-standard-layout base classes
199     if (!BaseClassDecl->isStandardLayout())
200       data().IsStandardLayout = false;
201 
202     // Record if this base is the first non-literal field or base.
203     if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
204       data().HasNonLiteralTypeFieldsOrBases = true;
205 
206     // Now go through all virtual bases of this base and add them.
207     for (const auto &VBase : BaseClassDecl->vbases()) {
208       // Add this base if it's not already in the list.
209       if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
210         VBases.push_back(&VBase);
211 
212         // C++11 [class.copy]p8:
213         //   The implicitly-declared copy constructor for a class X will have
214         //   the form 'X::X(const X&)' if each [...] virtual base class B of X
215         //   has a copy constructor whose first parameter is of type
216         //   'const B&' or 'const volatile B&' [...]
217         if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
218           if (!VBaseDecl->hasCopyConstructorWithConstParam())
219             data().ImplicitCopyConstructorHasConstParam = false;
220       }
221     }
222 
223     if (Base->isVirtual()) {
224       // Add this base if it's not already in the list.
225       if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
226         VBases.push_back(Base);
227 
228       // C++0x [meta.unary.prop] is_empty:
229       //    T is a class type, but not a union type, with ... no virtual base
230       //    classes
231       data().Empty = false;
232 
233       // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
234       //   A [default constructor, copy/move constructor, or copy/move assignment
235       //   operator for a class X] is trivial [...] if:
236       //    -- class X has [...] no virtual base classes
237       data().HasTrivialSpecialMembers &= SMF_Destructor;
238 
239       // C++0x [class]p7:
240       //   A standard-layout class is a class that: [...]
241       //    -- has [...] no virtual base classes
242       data().IsStandardLayout = false;
243 
244       // C++11 [dcl.constexpr]p4:
245       //   In the definition of a constexpr constructor [...]
246       //    -- the class shall not have any virtual base classes
247       data().DefaultedDefaultConstructorIsConstexpr = false;
248     } else {
249       // C++ [class.ctor]p5:
250       //   A default constructor is trivial [...] if:
251       //    -- all the direct base classes of its class have trivial default
252       //       constructors.
253       if (!BaseClassDecl->hasTrivialDefaultConstructor())
254         data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
255 
256       // C++0x [class.copy]p13:
257       //   A copy/move constructor for class X is trivial if [...]
258       //    [...]
259       //    -- the constructor selected to copy/move each direct base class
260       //       subobject is trivial, and
261       if (!BaseClassDecl->hasTrivialCopyConstructor())
262         data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
263       // If the base class doesn't have a simple move constructor, we'll eagerly
264       // declare it and perform overload resolution to determine which function
265       // it actually calls. If it does have a simple move constructor, this
266       // check is correct.
267       if (!BaseClassDecl->hasTrivialMoveConstructor())
268         data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
269 
270       // C++0x [class.copy]p27:
271       //   A copy/move assignment operator for class X is trivial if [...]
272       //    [...]
273       //    -- the assignment operator selected to copy/move each direct base
274       //       class subobject is trivial, and
275       if (!BaseClassDecl->hasTrivialCopyAssignment())
276         data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
277       // If the base class doesn't have a simple move assignment, we'll eagerly
278       // declare it and perform overload resolution to determine which function
279       // it actually calls. If it does have a simple move assignment, this
280       // check is correct.
281       if (!BaseClassDecl->hasTrivialMoveAssignment())
282         data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
283 
284       // C++11 [class.ctor]p6:
285       //   If that user-written default constructor would satisfy the
286       //   requirements of a constexpr constructor, the implicitly-defined
287       //   default constructor is constexpr.
288       if (!BaseClassDecl->hasConstexprDefaultConstructor())
289         data().DefaultedDefaultConstructorIsConstexpr = false;
290     }
291 
292     // C++ [class.ctor]p3:
293     //   A destructor is trivial if all the direct base classes of its class
294     //   have trivial destructors.
295     if (!BaseClassDecl->hasTrivialDestructor())
296       data().HasTrivialSpecialMembers &= ~SMF_Destructor;
297 
298     if (!BaseClassDecl->hasIrrelevantDestructor())
299       data().HasIrrelevantDestructor = false;
300 
301     // C++11 [class.copy]p18:
302     //   The implicitly-declared copy assignment oeprator for a class X will
303     //   have the form 'X& X::operator=(const X&)' if each direct base class B
304     //   of X has a copy assignment operator whose parameter is of type 'const
305     //   B&', 'const volatile B&', or 'B' [...]
306     if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
307       data().ImplicitCopyAssignmentHasConstParam = false;
308 
309     // C++11 [class.copy]p8:
310     //   The implicitly-declared copy constructor for a class X will have
311     //   the form 'X::X(const X&)' if each direct [...] base class B of X
312     //   has a copy constructor whose first parameter is of type
313     //   'const B&' or 'const volatile B&' [...]
314     if (!BaseClassDecl->hasCopyConstructorWithConstParam())
315       data().ImplicitCopyConstructorHasConstParam = false;
316 
317     // A class has an Objective-C object member if... or any of its bases
318     // has an Objective-C object member.
319     if (BaseClassDecl->hasObjectMember())
320       setHasObjectMember(true);
321 
322     if (BaseClassDecl->hasVolatileMember())
323       setHasVolatileMember(true);
324 
325     // Keep track of the presence of mutable fields.
326     if (BaseClassDecl->hasMutableFields())
327       data().HasMutableFields = true;
328 
329     if (BaseClassDecl->hasUninitializedReferenceMember())
330       data().HasUninitializedReferenceMember = true;
331 
332     if (!BaseClassDecl->allowConstDefaultInit())
333       data().HasUninitializedFields = true;
334 
335     addedClassSubobject(BaseClassDecl);
336   }
337 
338   if (VBases.empty()) {
339     data().IsParsingBaseSpecifiers = false;
340     return;
341   }
342 
343   // Create base specifier for any direct or indirect virtual bases.
344   data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
345   data().NumVBases = VBases.size();
346   for (int I = 0, E = VBases.size(); I != E; ++I) {
347     QualType Type = VBases[I]->getType();
348     if (!Type->isDependentType())
349       addedClassSubobject(Type->getAsCXXRecordDecl());
350     data().getVBases()[I] = *VBases[I];
351   }
352 
353   data().IsParsingBaseSpecifiers = false;
354 }
355 
356 void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
357   // C++11 [class.copy]p11:
358   //   A defaulted copy/move constructor for a class X is defined as
359   //   deleted if X has:
360   //    -- a direct or virtual base class B that cannot be copied/moved [...]
361   //    -- a non-static data member of class type M (or array thereof)
362   //       that cannot be copied or moved [...]
363   if (!Subobj->hasSimpleMoveConstructor())
364     data().NeedOverloadResolutionForMoveConstructor = true;
365 
366   // C++11 [class.copy]p23:
367   //   A defaulted copy/move assignment operator for a class X is defined as
368   //   deleted if X has:
369   //    -- a direct or virtual base class B that cannot be copied/moved [...]
370   //    -- a non-static data member of class type M (or array thereof)
371   //        that cannot be copied or moved [...]
372   if (!Subobj->hasSimpleMoveAssignment())
373     data().NeedOverloadResolutionForMoveAssignment = true;
374 
375   // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
376   //   A defaulted [ctor or dtor] for a class X is defined as
377   //   deleted if X has:
378   //    -- any direct or virtual base class [...] has a type with a destructor
379   //       that is deleted or inaccessible from the defaulted [ctor or dtor].
380   //    -- any non-static data member has a type with a destructor
381   //       that is deleted or inaccessible from the defaulted [ctor or dtor].
382   if (!Subobj->hasSimpleDestructor()) {
383     data().NeedOverloadResolutionForMoveConstructor = true;
384     data().NeedOverloadResolutionForDestructor = true;
385   }
386 }
387 
388 bool CXXRecordDecl::hasAnyDependentBases() const {
389   if (!isDependentContext())
390     return false;
391 
392   return !forallBases([](const CXXRecordDecl *) { return true; });
393 }
394 
395 bool CXXRecordDecl::isTriviallyCopyable() const {
396   // C++0x [class]p5:
397   //   A trivially copyable class is a class that:
398   //   -- has no non-trivial copy constructors,
399   if (hasNonTrivialCopyConstructor()) return false;
400   //   -- has no non-trivial move constructors,
401   if (hasNonTrivialMoveConstructor()) return false;
402   //   -- has no non-trivial copy assignment operators,
403   if (hasNonTrivialCopyAssignment()) return false;
404   //   -- has no non-trivial move assignment operators, and
405   if (hasNonTrivialMoveAssignment()) return false;
406   //   -- has a trivial destructor.
407   if (!hasTrivialDestructor()) return false;
408 
409   return true;
410 }
411 
412 void CXXRecordDecl::markedVirtualFunctionPure() {
413   // C++ [class.abstract]p2:
414   //   A class is abstract if it has at least one pure virtual function.
415   data().Abstract = true;
416 }
417 
418 void CXXRecordDecl::addedMember(Decl *D) {
419   if (!D->isImplicit() &&
420       !isa<FieldDecl>(D) &&
421       !isa<IndirectFieldDecl>(D) &&
422       (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
423         cast<TagDecl>(D)->getTagKind() == TTK_Interface))
424     data().HasOnlyCMembers = false;
425 
426   // Ignore friends and invalid declarations.
427   if (D->getFriendObjectKind() || D->isInvalidDecl())
428     return;
429 
430   FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
431   if (FunTmpl)
432     D = FunTmpl->getTemplatedDecl();
433 
434   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
435     if (Method->isVirtual()) {
436       // C++ [dcl.init.aggr]p1:
437       //   An aggregate is an array or a class with [...] no virtual functions.
438       data().Aggregate = false;
439 
440       // C++ [class]p4:
441       //   A POD-struct is an aggregate class...
442       data().PlainOldData = false;
443 
444       // Virtual functions make the class non-empty.
445       // FIXME: Standard ref?
446       data().Empty = false;
447 
448       // C++ [class.virtual]p1:
449       //   A class that declares or inherits a virtual function is called a
450       //   polymorphic class.
451       data().Polymorphic = true;
452 
453       // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
454       //   A [default constructor, copy/move constructor, or copy/move
455       //   assignment operator for a class X] is trivial [...] if:
456       //    -- class X has no virtual functions [...]
457       data().HasTrivialSpecialMembers &= SMF_Destructor;
458 
459       // C++0x [class]p7:
460       //   A standard-layout class is a class that: [...]
461       //    -- has no virtual functions
462       data().IsStandardLayout = false;
463     }
464   }
465 
466   // Notify the listener if an implicit member was added after the definition
467   // was completed.
468   if (!isBeingDefined() && D->isImplicit())
469     if (ASTMutationListener *L = getASTMutationListener())
470       L->AddedCXXImplicitMember(data().Definition, D);
471 
472   // The kind of special member this declaration is, if any.
473   unsigned SMKind = 0;
474 
475   // Handle constructors.
476   if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
477     if (!Constructor->isImplicit()) {
478       // Note that we have a user-declared constructor.
479       data().UserDeclaredConstructor = true;
480 
481       // C++ [class]p4:
482       //   A POD-struct is an aggregate class [...]
483       // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
484       // type is technically an aggregate in C++0x since it wouldn't be in 03.
485       data().PlainOldData = false;
486     }
487 
488     // Technically, "user-provided" is only defined for special member
489     // functions, but the intent of the standard is clearly that it should apply
490     // to all functions.
491     bool UserProvided = Constructor->isUserProvided();
492 
493     if (Constructor->isDefaultConstructor()) {
494       SMKind |= SMF_DefaultConstructor;
495 
496       if (UserProvided)
497         data().UserProvidedDefaultConstructor = true;
498       if (Constructor->isConstexpr())
499         data().HasConstexprDefaultConstructor = true;
500     }
501 
502     if (!FunTmpl) {
503       unsigned Quals;
504       if (Constructor->isCopyConstructor(Quals)) {
505         SMKind |= SMF_CopyConstructor;
506 
507         if (Quals & Qualifiers::Const)
508           data().HasDeclaredCopyConstructorWithConstParam = true;
509       } else if (Constructor->isMoveConstructor())
510         SMKind |= SMF_MoveConstructor;
511     }
512 
513     // Record if we see any constexpr constructors which are neither copy
514     // nor move constructors.
515     if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
516       data().HasConstexprNonCopyMoveConstructor = true;
517 
518     // C++ [dcl.init.aggr]p1:
519     //   An aggregate is an array or a class with no user-declared
520     //   constructors [...].
521     // C++11 [dcl.init.aggr]p1:
522     //   An aggregate is an array or a class with no user-provided
523     //   constructors [...].
524     if (getASTContext().getLangOpts().CPlusPlus11
525           ? UserProvided : !Constructor->isImplicit())
526       data().Aggregate = false;
527   }
528 
529   // Handle destructors.
530   if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
531     SMKind |= SMF_Destructor;
532 
533     if (DD->isUserProvided())
534       data().HasIrrelevantDestructor = false;
535     // If the destructor is explicitly defaulted and not trivial or not public
536     // or if the destructor is deleted, we clear HasIrrelevantDestructor in
537     // finishedDefaultedOrDeletedMember.
538 
539     // C++11 [class.dtor]p5:
540     //   A destructor is trivial if [...] the destructor is not virtual.
541     if (DD->isVirtual())
542       data().HasTrivialSpecialMembers &= ~SMF_Destructor;
543   }
544 
545   // Handle member functions.
546   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
547     if (Method->isCopyAssignmentOperator()) {
548       SMKind |= SMF_CopyAssignment;
549 
550       const ReferenceType *ParamTy =
551         Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
552       if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
553         data().HasDeclaredCopyAssignmentWithConstParam = true;
554     }
555 
556     if (Method->isMoveAssignmentOperator())
557       SMKind |= SMF_MoveAssignment;
558 
559     // Keep the list of conversion functions up-to-date.
560     if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
561       // FIXME: We use the 'unsafe' accessor for the access specifier here,
562       // because Sema may not have set it yet. That's really just a misdesign
563       // in Sema. However, LLDB *will* have set the access specifier correctly,
564       // and adds declarations after the class is technically completed,
565       // so completeDefinition()'s overriding of the access specifiers doesn't
566       // work.
567       AccessSpecifier AS = Conversion->getAccessUnsafe();
568 
569       if (Conversion->getPrimaryTemplate()) {
570         // We don't record specializations.
571       } else {
572         ASTContext &Ctx = getASTContext();
573         ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
574         NamedDecl *Primary =
575             FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
576         if (Primary->getPreviousDecl())
577           Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
578                               Primary, AS);
579         else
580           Conversions.addDecl(Ctx, Primary, AS);
581       }
582     }
583 
584     if (SMKind) {
585       // If this is the first declaration of a special member, we no longer have
586       // an implicit trivial special member.
587       data().HasTrivialSpecialMembers &=
588         data().DeclaredSpecialMembers | ~SMKind;
589 
590       if (!Method->isImplicit() && !Method->isUserProvided()) {
591         // This method is user-declared but not user-provided. We can't work out
592         // whether it's trivial yet (not until we get to the end of the class).
593         // We'll handle this method in finishedDefaultedOrDeletedMember.
594       } else if (Method->isTrivial())
595         data().HasTrivialSpecialMembers |= SMKind;
596       else
597         data().DeclaredNonTrivialSpecialMembers |= SMKind;
598 
599       // Note when we have declared a declared special member, and suppress the
600       // implicit declaration of this special member.
601       data().DeclaredSpecialMembers |= SMKind;
602 
603       if (!Method->isImplicit()) {
604         data().UserDeclaredSpecialMembers |= SMKind;
605 
606         // C++03 [class]p4:
607         //   A POD-struct is an aggregate class that has [...] no user-defined
608         //   copy assignment operator and no user-defined destructor.
609         //
610         // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
611         // aggregates could not have any constructors, clear it even for an
612         // explicitly defaulted or deleted constructor.
613         // type is technically an aggregate in C++0x since it wouldn't be in 03.
614         //
615         // Also, a user-declared move assignment operator makes a class non-POD.
616         // This is an extension in C++03.
617         data().PlainOldData = false;
618       }
619     }
620 
621     return;
622   }
623 
624   // Handle non-static data members.
625   if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
626     // C++ [class.bit]p2:
627     //   A declaration for a bit-field that omits the identifier declares an
628     //   unnamed bit-field. Unnamed bit-fields are not members and cannot be
629     //   initialized.
630     if (Field->isUnnamedBitfield())
631       return;
632 
633     // C++ [dcl.init.aggr]p1:
634     //   An aggregate is an array or a class (clause 9) with [...] no
635     //   private or protected non-static data members (clause 11).
636     //
637     // A POD must be an aggregate.
638     if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
639       data().Aggregate = false;
640       data().PlainOldData = false;
641     }
642 
643     // C++0x [class]p7:
644     //   A standard-layout class is a class that:
645     //    [...]
646     //    -- has the same access control for all non-static data members,
647     switch (D->getAccess()) {
648     case AS_private:    data().HasPrivateFields = true;   break;
649     case AS_protected:  data().HasProtectedFields = true; break;
650     case AS_public:     data().HasPublicFields = true;    break;
651     case AS_none:       llvm_unreachable("Invalid access specifier");
652     };
653     if ((data().HasPrivateFields + data().HasProtectedFields +
654          data().HasPublicFields) > 1)
655       data().IsStandardLayout = false;
656 
657     // Keep track of the presence of mutable fields.
658     if (Field->isMutable())
659       data().HasMutableFields = true;
660 
661     // C++11 [class.union]p8, DR1460:
662     //   If X is a union, a non-static data member of X that is not an anonymous
663     //   union is a variant member of X.
664     if (isUnion() && !Field->isAnonymousStructOrUnion())
665       data().HasVariantMembers = true;
666 
667     // C++0x [class]p9:
668     //   A POD struct is a class that is both a trivial class and a
669     //   standard-layout class, and has no non-static data members of type
670     //   non-POD struct, non-POD union (or array of such types).
671     //
672     // Automatic Reference Counting: the presence of a member of Objective-C pointer type
673     // that does not explicitly have no lifetime makes the class a non-POD.
674     ASTContext &Context = getASTContext();
675     QualType T = Context.getBaseElementType(Field->getType());
676     if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
677       if (!Context.getLangOpts().ObjCAutoRefCount) {
678         setHasObjectMember(true);
679       } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
680         // Objective-C Automatic Reference Counting:
681         //   If a class has a non-static data member of Objective-C pointer
682         //   type (or array thereof), it is a non-POD type and its
683         //   default constructor (if any), copy constructor, move constructor,
684         //   copy assignment operator, move assignment operator, and destructor are
685         //   non-trivial.
686         setHasObjectMember(true);
687         struct DefinitionData &Data = data();
688         Data.PlainOldData = false;
689         Data.HasTrivialSpecialMembers = 0;
690         Data.HasIrrelevantDestructor = false;
691       }
692     } else if (!T.isCXX98PODType(Context))
693       data().PlainOldData = false;
694 
695     if (T->isReferenceType()) {
696       if (!Field->hasInClassInitializer())
697         data().HasUninitializedReferenceMember = true;
698 
699       // C++0x [class]p7:
700       //   A standard-layout class is a class that:
701       //    -- has no non-static data members of type [...] reference,
702       data().IsStandardLayout = false;
703     }
704 
705     if (!Field->hasInClassInitializer() && !Field->isMutable()) {
706       if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
707         if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
708           data().HasUninitializedFields = true;
709       } else {
710         data().HasUninitializedFields = true;
711       }
712     }
713 
714     // Record if this field is the first non-literal or volatile field or base.
715     if (!T->isLiteralType(Context) || T.isVolatileQualified())
716       data().HasNonLiteralTypeFieldsOrBases = true;
717 
718     if (Field->hasInClassInitializer() ||
719         (Field->isAnonymousStructOrUnion() &&
720          Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
721       data().HasInClassInitializer = true;
722 
723       // C++11 [class]p5:
724       //   A default constructor is trivial if [...] no non-static data member
725       //   of its class has a brace-or-equal-initializer.
726       data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
727 
728       // C++11 [dcl.init.aggr]p1:
729       //   An aggregate is a [...] class with [...] no
730       //   brace-or-equal-initializers for non-static data members.
731       //
732       // This rule was removed in C++1y.
733       if (!getASTContext().getLangOpts().CPlusPlus14)
734         data().Aggregate = false;
735 
736       // C++11 [class]p10:
737       //   A POD struct is [...] a trivial class.
738       data().PlainOldData = false;
739     }
740 
741     // C++11 [class.copy]p23:
742     //   A defaulted copy/move assignment operator for a class X is defined
743     //   as deleted if X has:
744     //    -- a non-static data member of reference type
745     if (T->isReferenceType())
746       data().DefaultedMoveAssignmentIsDeleted = true;
747 
748     if (const RecordType *RecordTy = T->getAs<RecordType>()) {
749       CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
750       if (FieldRec->getDefinition()) {
751         addedClassSubobject(FieldRec);
752 
753         // We may need to perform overload resolution to determine whether a
754         // field can be moved if it's const or volatile qualified.
755         if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
756           data().NeedOverloadResolutionForMoveConstructor = true;
757           data().NeedOverloadResolutionForMoveAssignment = true;
758         }
759 
760         // C++11 [class.ctor]p5, C++11 [class.copy]p11:
761         //   A defaulted [special member] for a class X is defined as
762         //   deleted if:
763         //    -- X is a union-like class that has a variant member with a
764         //       non-trivial [corresponding special member]
765         if (isUnion()) {
766           if (FieldRec->hasNonTrivialMoveConstructor())
767             data().DefaultedMoveConstructorIsDeleted = true;
768           if (FieldRec->hasNonTrivialMoveAssignment())
769             data().DefaultedMoveAssignmentIsDeleted = true;
770           if (FieldRec->hasNonTrivialDestructor())
771             data().DefaultedDestructorIsDeleted = true;
772         }
773 
774         // C++0x [class.ctor]p5:
775         //   A default constructor is trivial [...] if:
776         //    -- for all the non-static data members of its class that are of
777         //       class type (or array thereof), each such class has a trivial
778         //       default constructor.
779         if (!FieldRec->hasTrivialDefaultConstructor())
780           data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
781 
782         // C++0x [class.copy]p13:
783         //   A copy/move constructor for class X is trivial if [...]
784         //    [...]
785         //    -- for each non-static data member of X that is of class type (or
786         //       an array thereof), the constructor selected to copy/move that
787         //       member is trivial;
788         if (!FieldRec->hasTrivialCopyConstructor())
789           data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
790         // If the field doesn't have a simple move constructor, we'll eagerly
791         // declare the move constructor for this class and we'll decide whether
792         // it's trivial then.
793         if (!FieldRec->hasTrivialMoveConstructor())
794           data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
795 
796         // C++0x [class.copy]p27:
797         //   A copy/move assignment operator for class X is trivial if [...]
798         //    [...]
799         //    -- for each non-static data member of X that is of class type (or
800         //       an array thereof), the assignment operator selected to
801         //       copy/move that member is trivial;
802         if (!FieldRec->hasTrivialCopyAssignment())
803           data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
804         // If the field doesn't have a simple move assignment, we'll eagerly
805         // declare the move assignment for this class and we'll decide whether
806         // it's trivial then.
807         if (!FieldRec->hasTrivialMoveAssignment())
808           data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
809 
810         if (!FieldRec->hasTrivialDestructor())
811           data().HasTrivialSpecialMembers &= ~SMF_Destructor;
812         if (!FieldRec->hasIrrelevantDestructor())
813           data().HasIrrelevantDestructor = false;
814         if (FieldRec->hasObjectMember())
815           setHasObjectMember(true);
816         if (FieldRec->hasVolatileMember())
817           setHasVolatileMember(true);
818 
819         // C++0x [class]p7:
820         //   A standard-layout class is a class that:
821         //    -- has no non-static data members of type non-standard-layout
822         //       class (or array of such types) [...]
823         if (!FieldRec->isStandardLayout())
824           data().IsStandardLayout = false;
825 
826         // C++0x [class]p7:
827         //   A standard-layout class is a class that:
828         //    [...]
829         //    -- has no base classes of the same type as the first non-static
830         //       data member.
831         // We don't want to expend bits in the state of the record decl
832         // tracking whether this is the first non-static data member so we
833         // cheat a bit and use some of the existing state: the empty bit.
834         // Virtual bases and virtual methods make a class non-empty, but they
835         // also make it non-standard-layout so we needn't check here.
836         // A non-empty base class may leave the class standard-layout, but not
837         // if we have arrived here, and have at least one non-static data
838         // member. If IsStandardLayout remains true, then the first non-static
839         // data member must come through here with Empty still true, and Empty
840         // will subsequently be set to false below.
841         if (data().IsStandardLayout && data().Empty) {
842           for (const auto &BI : bases()) {
843             if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
844               data().IsStandardLayout = false;
845               break;
846             }
847           }
848         }
849 
850         // Keep track of the presence of mutable fields.
851         if (FieldRec->hasMutableFields())
852           data().HasMutableFields = true;
853 
854         // C++11 [class.copy]p13:
855         //   If the implicitly-defined constructor would satisfy the
856         //   requirements of a constexpr constructor, the implicitly-defined
857         //   constructor is constexpr.
858         // C++11 [dcl.constexpr]p4:
859         //    -- every constructor involved in initializing non-static data
860         //       members [...] shall be a constexpr constructor
861         if (!Field->hasInClassInitializer() &&
862             !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
863           // The standard requires any in-class initializer to be a constant
864           // expression. We consider this to be a defect.
865           data().DefaultedDefaultConstructorIsConstexpr = false;
866 
867         // C++11 [class.copy]p8:
868         //   The implicitly-declared copy constructor for a class X will have
869         //   the form 'X::X(const X&)' if [...] for all the non-static data
870         //   members of X that are of a class type M (or array thereof), each
871         //   such class type has a copy constructor whose first parameter is
872         //   of type 'const M&' or 'const volatile M&'.
873         if (!FieldRec->hasCopyConstructorWithConstParam())
874           data().ImplicitCopyConstructorHasConstParam = false;
875 
876         // C++11 [class.copy]p18:
877         //   The implicitly-declared copy assignment oeprator for a class X will
878         //   have the form 'X& X::operator=(const X&)' if [...] for all the
879         //   non-static data members of X that are of a class type M (or array
880         //   thereof), each such class type has a copy assignment operator whose
881         //   parameter is of type 'const M&', 'const volatile M&' or 'M'.
882         if (!FieldRec->hasCopyAssignmentWithConstParam())
883           data().ImplicitCopyAssignmentHasConstParam = false;
884 
885         if (FieldRec->hasUninitializedReferenceMember() &&
886             !Field->hasInClassInitializer())
887           data().HasUninitializedReferenceMember = true;
888 
889         // C++11 [class.union]p8, DR1460:
890         //   a non-static data member of an anonymous union that is a member of
891         //   X is also a variant member of X.
892         if (FieldRec->hasVariantMembers() &&
893             Field->isAnonymousStructOrUnion())
894           data().HasVariantMembers = true;
895       }
896     } else {
897       // Base element type of field is a non-class type.
898       if (!T->isLiteralType(Context) ||
899           (!Field->hasInClassInitializer() && !isUnion()))
900         data().DefaultedDefaultConstructorIsConstexpr = false;
901 
902       // C++11 [class.copy]p23:
903       //   A defaulted copy/move assignment operator for a class X is defined
904       //   as deleted if X has:
905       //    -- a non-static data member of const non-class type (or array
906       //       thereof)
907       if (T.isConstQualified())
908         data().DefaultedMoveAssignmentIsDeleted = true;
909     }
910 
911     // C++0x [class]p7:
912     //   A standard-layout class is a class that:
913     //    [...]
914     //    -- either has no non-static data members in the most derived
915     //       class and at most one base class with non-static data members,
916     //       or has no base classes with non-static data members, and
917     // At this point we know that we have a non-static data member, so the last
918     // clause holds.
919     if (!data().HasNoNonEmptyBases)
920       data().IsStandardLayout = false;
921 
922     // If this is not a zero-length bit-field, then the class is not empty.
923     if (data().Empty) {
924       if (!Field->isBitField() ||
925           (!Field->getBitWidth()->isTypeDependent() &&
926            !Field->getBitWidth()->isValueDependent() &&
927            Field->getBitWidthValue(Context) != 0))
928         data().Empty = false;
929     }
930   }
931 
932   // Handle using declarations of conversion functions.
933   if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
934     if (Shadow->getDeclName().getNameKind()
935           == DeclarationName::CXXConversionFunctionName) {
936       ASTContext &Ctx = getASTContext();
937       data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
938     }
939   }
940 }
941 
942 void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
943   assert(!D->isImplicit() && !D->isUserProvided());
944 
945   // The kind of special member this declaration is, if any.
946   unsigned SMKind = 0;
947 
948   if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
949     if (Constructor->isDefaultConstructor()) {
950       SMKind |= SMF_DefaultConstructor;
951       if (Constructor->isConstexpr())
952         data().HasConstexprDefaultConstructor = true;
953     }
954     if (Constructor->isCopyConstructor())
955       SMKind |= SMF_CopyConstructor;
956     else if (Constructor->isMoveConstructor())
957       SMKind |= SMF_MoveConstructor;
958     else if (Constructor->isConstexpr())
959       // We may now know that the constructor is constexpr.
960       data().HasConstexprNonCopyMoveConstructor = true;
961   } else if (isa<CXXDestructorDecl>(D)) {
962     SMKind |= SMF_Destructor;
963     if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
964       data().HasIrrelevantDestructor = false;
965   } else if (D->isCopyAssignmentOperator())
966     SMKind |= SMF_CopyAssignment;
967   else if (D->isMoveAssignmentOperator())
968     SMKind |= SMF_MoveAssignment;
969 
970   // Update which trivial / non-trivial special members we have.
971   // addedMember will have skipped this step for this member.
972   if (D->isTrivial())
973     data().HasTrivialSpecialMembers |= SMKind;
974   else
975     data().DeclaredNonTrivialSpecialMembers |= SMKind;
976 }
977 
978 bool CXXRecordDecl::isCLike() const {
979   if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
980       !TemplateOrInstantiation.isNull())
981     return false;
982   if (!hasDefinition())
983     return true;
984 
985   return isPOD() && data().HasOnlyCMembers;
986 }
987 
988 bool CXXRecordDecl::isGenericLambda() const {
989   if (!isLambda()) return false;
990   return getLambdaData().IsGenericLambda;
991 }
992 
993 CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
994   if (!isLambda()) return nullptr;
995   DeclarationName Name =
996     getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
997   DeclContext::lookup_result Calls = lookup(Name);
998 
999   assert(!Calls.empty() && "Missing lambda call operator!");
1000   assert(Calls.size() == 1 && "More than one lambda call operator!");
1001 
1002   NamedDecl *CallOp = Calls.front();
1003   if (FunctionTemplateDecl *CallOpTmpl =
1004                     dyn_cast<FunctionTemplateDecl>(CallOp))
1005     return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
1006 
1007   return cast<CXXMethodDecl>(CallOp);
1008 }
1009 
1010 CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
1011   if (!isLambda()) return nullptr;
1012   DeclarationName Name =
1013     &getASTContext().Idents.get(getLambdaStaticInvokerName());
1014   DeclContext::lookup_result Invoker = lookup(Name);
1015   if (Invoker.empty()) return nullptr;
1016   assert(Invoker.size() == 1 && "More than one static invoker operator!");
1017   NamedDecl *InvokerFun = Invoker.front();
1018   if (FunctionTemplateDecl *InvokerTemplate =
1019                   dyn_cast<FunctionTemplateDecl>(InvokerFun))
1020     return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1021 
1022   return cast<CXXMethodDecl>(InvokerFun);
1023 }
1024 
1025 void CXXRecordDecl::getCaptureFields(
1026        llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
1027        FieldDecl *&ThisCapture) const {
1028   Captures.clear();
1029   ThisCapture = nullptr;
1030 
1031   LambdaDefinitionData &Lambda = getLambdaData();
1032   RecordDecl::field_iterator Field = field_begin();
1033   for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
1034        C != CEnd; ++C, ++Field) {
1035     if (C->capturesThis())
1036       ThisCapture = *Field;
1037     else if (C->capturesVariable())
1038       Captures[C->getCapturedVar()] = *Field;
1039   }
1040   assert(Field == field_end());
1041 }
1042 
1043 TemplateParameterList *
1044 CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1045   if (!isLambda()) return nullptr;
1046   CXXMethodDecl *CallOp = getLambdaCallOperator();
1047   if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1048     return Tmpl->getTemplateParameters();
1049   return nullptr;
1050 }
1051 
1052 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1053   QualType T =
1054       cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1055           ->getConversionType();
1056   return Context.getCanonicalType(T);
1057 }
1058 
1059 /// Collect the visible conversions of a base class.
1060 ///
1061 /// \param Record a base class of the class we're considering
1062 /// \param InVirtual whether this base class is a virtual base (or a base
1063 ///   of a virtual base)
1064 /// \param Access the access along the inheritance path to this base
1065 /// \param ParentHiddenTypes the conversions provided by the inheritors
1066 ///   of this base
1067 /// \param Output the set to which to add conversions from non-virtual bases
1068 /// \param VOutput the set to which to add conversions from virtual bases
1069 /// \param HiddenVBaseCs the set of conversions which were hidden in a
1070 ///   virtual base along some inheritance path
1071 static void CollectVisibleConversions(ASTContext &Context,
1072                                       CXXRecordDecl *Record,
1073                                       bool InVirtual,
1074                                       AccessSpecifier Access,
1075                   const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1076                                       ASTUnresolvedSet &Output,
1077                                       UnresolvedSetImpl &VOutput,
1078                            llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1079   // The set of types which have conversions in this class or its
1080   // subclasses.  As an optimization, we don't copy the derived set
1081   // unless it might change.
1082   const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1083   llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1084 
1085   // Collect the direct conversions and figure out which conversions
1086   // will be hidden in the subclasses.
1087   CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1088   CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1089   if (ConvI != ConvE) {
1090     HiddenTypesBuffer = ParentHiddenTypes;
1091     HiddenTypes = &HiddenTypesBuffer;
1092 
1093     for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
1094       CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1095       bool Hidden = ParentHiddenTypes.count(ConvType);
1096       if (!Hidden)
1097         HiddenTypesBuffer.insert(ConvType);
1098 
1099       // If this conversion is hidden and we're in a virtual base,
1100       // remember that it's hidden along some inheritance path.
1101       if (Hidden && InVirtual)
1102         HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1103 
1104       // If this conversion isn't hidden, add it to the appropriate output.
1105       else if (!Hidden) {
1106         AccessSpecifier IAccess
1107           = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1108 
1109         if (InVirtual)
1110           VOutput.addDecl(I.getDecl(), IAccess);
1111         else
1112           Output.addDecl(Context, I.getDecl(), IAccess);
1113       }
1114     }
1115   }
1116 
1117   // Collect information recursively from any base classes.
1118   for (const auto &I : Record->bases()) {
1119     const RecordType *RT = I.getType()->getAs<RecordType>();
1120     if (!RT) continue;
1121 
1122     AccessSpecifier BaseAccess
1123       = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1124     bool BaseInVirtual = InVirtual || I.isVirtual();
1125 
1126     CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1127     CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1128                               *HiddenTypes, Output, VOutput, HiddenVBaseCs);
1129   }
1130 }
1131 
1132 /// Collect the visible conversions of a class.
1133 ///
1134 /// This would be extremely straightforward if it weren't for virtual
1135 /// bases.  It might be worth special-casing that, really.
1136 static void CollectVisibleConversions(ASTContext &Context,
1137                                       CXXRecordDecl *Record,
1138                                       ASTUnresolvedSet &Output) {
1139   // The collection of all conversions in virtual bases that we've
1140   // found.  These will be added to the output as long as they don't
1141   // appear in the hidden-conversions set.
1142   UnresolvedSet<8> VBaseCs;
1143 
1144   // The set of conversions in virtual bases that we've determined to
1145   // be hidden.
1146   llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1147 
1148   // The set of types hidden by classes derived from this one.
1149   llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1150 
1151   // Go ahead and collect the direct conversions and add them to the
1152   // hidden-types set.
1153   CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1154   CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1155   Output.append(Context, ConvI, ConvE);
1156   for (; ConvI != ConvE; ++ConvI)
1157     HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
1158 
1159   // Recursively collect conversions from base classes.
1160   for (const auto &I : Record->bases()) {
1161     const RecordType *RT = I.getType()->getAs<RecordType>();
1162     if (!RT) continue;
1163 
1164     CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1165                               I.isVirtual(), I.getAccessSpecifier(),
1166                               HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1167   }
1168 
1169   // Add any unhidden conversions provided by virtual bases.
1170   for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1171          I != E; ++I) {
1172     if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1173       Output.addDecl(Context, I.getDecl(), I.getAccess());
1174   }
1175 }
1176 
1177 /// getVisibleConversionFunctions - get all conversion functions visible
1178 /// in current class; including conversion function templates.
1179 llvm::iterator_range<CXXRecordDecl::conversion_iterator>
1180 CXXRecordDecl::getVisibleConversionFunctions() {
1181   ASTContext &Ctx = getASTContext();
1182 
1183   ASTUnresolvedSet *Set;
1184   if (bases_begin() == bases_end()) {
1185     // If root class, all conversions are visible.
1186     Set = &data().Conversions.get(Ctx);
1187   } else {
1188     Set = &data().VisibleConversions.get(Ctx);
1189     // If visible conversion list is not evaluated, evaluate it.
1190     if (!data().ComputedVisibleConversions) {
1191       CollectVisibleConversions(Ctx, this, *Set);
1192       data().ComputedVisibleConversions = true;
1193     }
1194   }
1195   return llvm::make_range(Set->begin(), Set->end());
1196 }
1197 
1198 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1199   // This operation is O(N) but extremely rare.  Sema only uses it to
1200   // remove UsingShadowDecls in a class that were followed by a direct
1201   // declaration, e.g.:
1202   //   class A : B {
1203   //     using B::operator int;
1204   //     operator int();
1205   //   };
1206   // This is uncommon by itself and even more uncommon in conjunction
1207   // with sufficiently large numbers of directly-declared conversions
1208   // that asymptotic behavior matters.
1209 
1210   ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
1211   for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1212     if (Convs[I].getDecl() == ConvDecl) {
1213       Convs.erase(I);
1214       assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1215              && "conversion was found multiple times in unresolved set");
1216       return;
1217     }
1218   }
1219 
1220   llvm_unreachable("conversion not found in set!");
1221 }
1222 
1223 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
1224   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1225     return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1226 
1227   return nullptr;
1228 }
1229 
1230 MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1231   return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1232 }
1233 
1234 void
1235 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1236                                              TemplateSpecializationKind TSK) {
1237   assert(TemplateOrInstantiation.isNull() &&
1238          "Previous template or instantiation?");
1239   assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
1240   TemplateOrInstantiation
1241     = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1242 }
1243 
1244 ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1245   return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1246 }
1247 
1248 void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1249   TemplateOrInstantiation = Template;
1250 }
1251 
1252 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1253   if (const ClassTemplateSpecializationDecl *Spec
1254         = dyn_cast<ClassTemplateSpecializationDecl>(this))
1255     return Spec->getSpecializationKind();
1256 
1257   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1258     return MSInfo->getTemplateSpecializationKind();
1259 
1260   return TSK_Undeclared;
1261 }
1262 
1263 void
1264 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1265   if (ClassTemplateSpecializationDecl *Spec
1266       = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1267     Spec->setSpecializationKind(TSK);
1268     return;
1269   }
1270 
1271   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1272     MSInfo->setTemplateSpecializationKind(TSK);
1273     return;
1274   }
1275 
1276   llvm_unreachable("Not a class template or member class specialization");
1277 }
1278 
1279 const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1280   // If it's a class template specialization, find the template or partial
1281   // specialization from which it was instantiated.
1282   if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1283     auto From = TD->getInstantiatedFrom();
1284     if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1285       while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1286         if (NewCTD->isMemberSpecialization())
1287           break;
1288         CTD = NewCTD;
1289       }
1290       return CTD->getTemplatedDecl()->getDefinition();
1291     }
1292     if (auto *CTPSD =
1293             From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1294       while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1295         if (NewCTPSD->isMemberSpecialization())
1296           break;
1297         CTPSD = NewCTPSD;
1298       }
1299       return CTPSD->getDefinition();
1300     }
1301   }
1302 
1303   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1304     if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1305       const CXXRecordDecl *RD = this;
1306       while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1307         RD = NewRD;
1308       return RD->getDefinition();
1309     }
1310   }
1311 
1312   assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1313          "couldn't find pattern for class template instantiation");
1314   return nullptr;
1315 }
1316 
1317 CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1318   ASTContext &Context = getASTContext();
1319   QualType ClassType = Context.getTypeDeclType(this);
1320 
1321   DeclarationName Name
1322     = Context.DeclarationNames.getCXXDestructorName(
1323                                           Context.getCanonicalType(ClassType));
1324 
1325   DeclContext::lookup_result R = lookup(Name);
1326   if (R.empty())
1327     return nullptr;
1328 
1329   CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
1330   return Dtor;
1331 }
1332 
1333 bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1334   // Destructor is noreturn.
1335   if (const CXXDestructorDecl *Destructor = getDestructor())
1336     if (Destructor->isNoReturn())
1337       return true;
1338 
1339   // Check base classes destructor for noreturn.
1340   for (const auto &Base : bases())
1341     if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1342       return true;
1343 
1344   // Check fields for noreturn.
1345   for (const auto *Field : fields())
1346     if (const CXXRecordDecl *RD =
1347             Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1348       if (RD->isAnyDestructorNoReturn())
1349         return true;
1350 
1351   // All destructors are not noreturn.
1352   return false;
1353 }
1354 
1355 void CXXRecordDecl::completeDefinition() {
1356   completeDefinition(nullptr);
1357 }
1358 
1359 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1360   RecordDecl::completeDefinition();
1361 
1362   // If the class may be abstract (but hasn't been marked as such), check for
1363   // any pure final overriders.
1364   if (mayBeAbstract()) {
1365     CXXFinalOverriderMap MyFinalOverriders;
1366     if (!FinalOverriders) {
1367       getFinalOverriders(MyFinalOverriders);
1368       FinalOverriders = &MyFinalOverriders;
1369     }
1370 
1371     bool Done = false;
1372     for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1373                                      MEnd = FinalOverriders->end();
1374          M != MEnd && !Done; ++M) {
1375       for (OverridingMethods::iterator SO = M->second.begin(),
1376                                     SOEnd = M->second.end();
1377            SO != SOEnd && !Done; ++SO) {
1378         assert(SO->second.size() > 0 &&
1379                "All virtual functions have overridding virtual functions");
1380 
1381         // C++ [class.abstract]p4:
1382         //   A class is abstract if it contains or inherits at least one
1383         //   pure virtual function for which the final overrider is pure
1384         //   virtual.
1385         if (SO->second.front().Method->isPure()) {
1386           data().Abstract = true;
1387           Done = true;
1388           break;
1389         }
1390       }
1391     }
1392   }
1393 
1394   // Set access bits correctly on the directly-declared conversions.
1395   for (conversion_iterator I = conversion_begin(), E = conversion_end();
1396        I != E; ++I)
1397     I.setAccess((*I)->getAccess());
1398 }
1399 
1400 bool CXXRecordDecl::mayBeAbstract() const {
1401   if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1402       isDependentContext())
1403     return false;
1404 
1405   for (const auto &B : bases()) {
1406     CXXRecordDecl *BaseDecl
1407       = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
1408     if (BaseDecl->isAbstract())
1409       return true;
1410   }
1411 
1412   return false;
1413 }
1414 
1415 void CXXMethodDecl::anchor() { }
1416 
1417 bool CXXMethodDecl::isStatic() const {
1418   const CXXMethodDecl *MD = getCanonicalDecl();
1419 
1420   if (MD->getStorageClass() == SC_Static)
1421     return true;
1422 
1423   OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1424   return isStaticOverloadedOperator(OOK);
1425 }
1426 
1427 static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1428                                  const CXXMethodDecl *BaseMD) {
1429   for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1430          E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1431     const CXXMethodDecl *MD = *I;
1432     if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1433       return true;
1434     if (recursivelyOverrides(MD, BaseMD))
1435       return true;
1436   }
1437   return false;
1438 }
1439 
1440 CXXMethodDecl *
1441 CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1442                                              bool MayBeBase) {
1443   if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1444     return this;
1445 
1446   // Lookup doesn't work for destructors, so handle them separately.
1447   if (isa<CXXDestructorDecl>(this)) {
1448     CXXMethodDecl *MD = RD->getDestructor();
1449     if (MD) {
1450       if (recursivelyOverrides(MD, this))
1451         return MD;
1452       if (MayBeBase && recursivelyOverrides(this, MD))
1453         return MD;
1454     }
1455     return nullptr;
1456   }
1457 
1458   for (auto *ND : RD->lookup(getDeclName())) {
1459     CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
1460     if (!MD)
1461       continue;
1462     if (recursivelyOverrides(MD, this))
1463       return MD;
1464     if (MayBeBase && recursivelyOverrides(this, MD))
1465       return MD;
1466   }
1467 
1468   for (const auto &I : RD->bases()) {
1469     const RecordType *RT = I.getType()->getAs<RecordType>();
1470     if (!RT)
1471       continue;
1472     const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1473     CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1474     if (T)
1475       return T;
1476   }
1477 
1478   return nullptr;
1479 }
1480 
1481 CXXMethodDecl *
1482 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1483                       SourceLocation StartLoc,
1484                       const DeclarationNameInfo &NameInfo,
1485                       QualType T, TypeSourceInfo *TInfo,
1486                       StorageClass SC, bool isInline,
1487                       bool isConstexpr, SourceLocation EndLocation) {
1488   return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1489                                    T, TInfo, SC, isInline, isConstexpr,
1490                                    EndLocation);
1491 }
1492 
1493 CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1494   return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
1495                                    DeclarationNameInfo(), QualType(), nullptr,
1496                                    SC_None, false, false, SourceLocation());
1497 }
1498 
1499 bool CXXMethodDecl::isUsualDeallocationFunction() const {
1500   if (getOverloadedOperator() != OO_Delete &&
1501       getOverloadedOperator() != OO_Array_Delete)
1502     return false;
1503 
1504   // C++ [basic.stc.dynamic.deallocation]p2:
1505   //   A template instance is never a usual deallocation function,
1506   //   regardless of its signature.
1507   if (getPrimaryTemplate())
1508     return false;
1509 
1510   // C++ [basic.stc.dynamic.deallocation]p2:
1511   //   If a class T has a member deallocation function named operator delete
1512   //   with exactly one parameter, then that function is a usual (non-placement)
1513   //   deallocation function. [...]
1514   if (getNumParams() == 1)
1515     return true;
1516 
1517   // C++ [basic.stc.dynamic.deallocation]p2:
1518   //   [...] If class T does not declare such an operator delete but does
1519   //   declare a member deallocation function named operator delete with
1520   //   exactly two parameters, the second of which has type std::size_t (18.1),
1521   //   then this function is a usual deallocation function.
1522   ASTContext &Context = getASTContext();
1523   if (getNumParams() != 2 ||
1524       !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1525                                       Context.getSizeType()))
1526     return false;
1527 
1528   // This function is a usual deallocation function if there are no
1529   // single-parameter deallocation functions of the same kind.
1530   DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1531   for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
1532        I != E; ++I) {
1533     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
1534       if (FD->getNumParams() == 1)
1535         return false;
1536   }
1537 
1538   return true;
1539 }
1540 
1541 bool CXXMethodDecl::isCopyAssignmentOperator() const {
1542   // C++0x [class.copy]p17:
1543   //  A user-declared copy assignment operator X::operator= is a non-static
1544   //  non-template member function of class X with exactly one parameter of
1545   //  type X, X&, const X&, volatile X& or const volatile X&.
1546   if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1547       /*non-static*/ isStatic() ||
1548       /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1549       getNumParams() != 1)
1550     return false;
1551 
1552   QualType ParamType = getParamDecl(0)->getType();
1553   if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1554     ParamType = Ref->getPointeeType();
1555 
1556   ASTContext &Context = getASTContext();
1557   QualType ClassType
1558     = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1559   return Context.hasSameUnqualifiedType(ClassType, ParamType);
1560 }
1561 
1562 bool CXXMethodDecl::isMoveAssignmentOperator() const {
1563   // C++0x [class.copy]p19:
1564   //  A user-declared move assignment operator X::operator= is a non-static
1565   //  non-template member function of class X with exactly one parameter of type
1566   //  X&&, const X&&, volatile X&&, or const volatile X&&.
1567   if (getOverloadedOperator() != OO_Equal || isStatic() ||
1568       getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1569       getNumParams() != 1)
1570     return false;
1571 
1572   QualType ParamType = getParamDecl(0)->getType();
1573   if (!isa<RValueReferenceType>(ParamType))
1574     return false;
1575   ParamType = ParamType->getPointeeType();
1576 
1577   ASTContext &Context = getASTContext();
1578   QualType ClassType
1579     = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1580   return Context.hasSameUnqualifiedType(ClassType, ParamType);
1581 }
1582 
1583 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
1584   assert(MD->isCanonicalDecl() && "Method is not canonical!");
1585   assert(!MD->getParent()->isDependentContext() &&
1586          "Can't add an overridden method to a class template!");
1587   assert(MD->isVirtual() && "Method is not virtual!");
1588 
1589   getASTContext().addOverriddenMethod(this, MD);
1590 }
1591 
1592 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
1593   if (isa<CXXConstructorDecl>(this)) return nullptr;
1594   return getASTContext().overridden_methods_begin(this);
1595 }
1596 
1597 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
1598   if (isa<CXXConstructorDecl>(this)) return nullptr;
1599   return getASTContext().overridden_methods_end(this);
1600 }
1601 
1602 unsigned CXXMethodDecl::size_overridden_methods() const {
1603   if (isa<CXXConstructorDecl>(this)) return 0;
1604   return getASTContext().overridden_methods_size(this);
1605 }
1606 
1607 QualType CXXMethodDecl::getThisType(ASTContext &C) const {
1608   // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1609   // If the member function is declared const, the type of this is const X*,
1610   // if the member function is declared volatile, the type of this is
1611   // volatile X*, and if the member function is declared const volatile,
1612   // the type of this is const volatile X*.
1613 
1614   assert(isInstance() && "No 'this' for static methods!");
1615 
1616   QualType ClassTy = C.getTypeDeclType(getParent());
1617   ClassTy = C.getQualifiedType(ClassTy,
1618                                Qualifiers::fromCVRMask(getTypeQualifiers()));
1619   return C.getPointerType(ClassTy);
1620 }
1621 
1622 bool CXXMethodDecl::hasInlineBody() const {
1623   // If this function is a template instantiation, look at the template from
1624   // which it was instantiated.
1625   const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1626   if (!CheckFn)
1627     CheckFn = this;
1628 
1629   const FunctionDecl *fn;
1630   return CheckFn->hasBody(fn) && !fn->isOutOfLine();
1631 }
1632 
1633 bool CXXMethodDecl::isLambdaStaticInvoker() const {
1634   const CXXRecordDecl *P = getParent();
1635   if (P->isLambda()) {
1636     if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1637       if (StaticInvoker == this) return true;
1638       if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1639         return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1640     }
1641   }
1642   return false;
1643 }
1644 
1645 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1646                                        TypeSourceInfo *TInfo, bool IsVirtual,
1647                                        SourceLocation L, Expr *Init,
1648                                        SourceLocation R,
1649                                        SourceLocation EllipsisLoc)
1650   : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
1651     LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1652     IsWritten(false), SourceOrderOrNumArrayIndices(0)
1653 {
1654 }
1655 
1656 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1657                                        FieldDecl *Member,
1658                                        SourceLocation MemberLoc,
1659                                        SourceLocation L, Expr *Init,
1660                                        SourceLocation R)
1661   : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1662     LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
1663     IsWritten(false), SourceOrderOrNumArrayIndices(0)
1664 {
1665 }
1666 
1667 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1668                                        IndirectFieldDecl *Member,
1669                                        SourceLocation MemberLoc,
1670                                        SourceLocation L, Expr *Init,
1671                                        SourceLocation R)
1672   : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1673     LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
1674     IsWritten(false), SourceOrderOrNumArrayIndices(0)
1675 {
1676 }
1677 
1678 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1679                                        TypeSourceInfo *TInfo,
1680                                        SourceLocation L, Expr *Init,
1681                                        SourceLocation R)
1682   : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1683     LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
1684     IsWritten(false), SourceOrderOrNumArrayIndices(0)
1685 {
1686 }
1687 
1688 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1689                                        FieldDecl *Member,
1690                                        SourceLocation MemberLoc,
1691                                        SourceLocation L, Expr *Init,
1692                                        SourceLocation R,
1693                                        VarDecl **Indices,
1694                                        unsigned NumIndices)
1695   : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
1696     LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
1697     IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
1698 {
1699   std::uninitialized_copy(Indices, Indices + NumIndices,
1700                           getTrailingObjects<VarDecl *>());
1701 }
1702 
1703 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1704                                                FieldDecl *Member,
1705                                                SourceLocation MemberLoc,
1706                                                SourceLocation L, Expr *Init,
1707                                                SourceLocation R,
1708                                                VarDecl **Indices,
1709                                                unsigned NumIndices) {
1710   void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
1711                                llvm::alignOf<CXXCtorInitializer>());
1712   return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1713                                       Indices, NumIndices);
1714 }
1715 
1716 TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
1717   if (isBaseInitializer())
1718     return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
1719   else
1720     return TypeLoc();
1721 }
1722 
1723 const Type *CXXCtorInitializer::getBaseClass() const {
1724   if (isBaseInitializer())
1725     return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
1726   else
1727     return nullptr;
1728 }
1729 
1730 SourceLocation CXXCtorInitializer::getSourceLocation() const {
1731   if (isInClassMemberInitializer())
1732     return getAnyMember()->getLocation();
1733 
1734   if (isAnyMemberInitializer())
1735     return getMemberLocation();
1736 
1737   if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1738     return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1739 
1740   return SourceLocation();
1741 }
1742 
1743 SourceRange CXXCtorInitializer::getSourceRange() const {
1744   if (isInClassMemberInitializer()) {
1745     FieldDecl *D = getAnyMember();
1746     if (Expr *I = D->getInClassInitializer())
1747       return I->getSourceRange();
1748     return SourceRange();
1749   }
1750 
1751   return SourceRange(getSourceLocation(), getRParenLoc());
1752 }
1753 
1754 void CXXConstructorDecl::anchor() { }
1755 
1756 CXXConstructorDecl *
1757 CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1758   return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
1759                                         DeclarationNameInfo(), QualType(),
1760                                         nullptr, false, false, false, false);
1761 }
1762 
1763 CXXConstructorDecl *
1764 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1765                            SourceLocation StartLoc,
1766                            const DeclarationNameInfo &NameInfo,
1767                            QualType T, TypeSourceInfo *TInfo,
1768                            bool isExplicit, bool isInline,
1769                            bool isImplicitlyDeclared, bool isConstexpr) {
1770   assert(NameInfo.getName().getNameKind()
1771          == DeclarationName::CXXConstructorName &&
1772          "Name must refer to a constructor");
1773   return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
1774                                         isExplicit, isInline,
1775                                         isImplicitlyDeclared, isConstexpr);
1776 }
1777 
1778 CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1779   return CtorInitializers.get(getASTContext().getExternalSource());
1780 }
1781 
1782 CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1783   assert(isDelegatingConstructor() && "Not a delegating constructor!");
1784   Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1785   if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1786     return Construct->getConstructor();
1787 
1788   return nullptr;
1789 }
1790 
1791 bool CXXConstructorDecl::isDefaultConstructor() const {
1792   // C++ [class.ctor]p5:
1793   //   A default constructor for a class X is a constructor of class
1794   //   X that can be called without an argument.
1795   return (getNumParams() == 0) ||
1796          (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
1797 }
1798 
1799 bool
1800 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
1801   return isCopyOrMoveConstructor(TypeQuals) &&
1802          getParamDecl(0)->getType()->isLValueReferenceType();
1803 }
1804 
1805 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1806   return isCopyOrMoveConstructor(TypeQuals) &&
1807     getParamDecl(0)->getType()->isRValueReferenceType();
1808 }
1809 
1810 /// \brief Determine whether this is a copy or move constructor.
1811 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
1812   // C++ [class.copy]p2:
1813   //   A non-template constructor for class X is a copy constructor
1814   //   if its first parameter is of type X&, const X&, volatile X& or
1815   //   const volatile X&, and either there are no other parameters
1816   //   or else all other parameters have default arguments (8.3.6).
1817   // C++0x [class.copy]p3:
1818   //   A non-template constructor for class X is a move constructor if its
1819   //   first parameter is of type X&&, const X&&, volatile X&&, or
1820   //   const volatile X&&, and either there are no other parameters or else
1821   //   all other parameters have default arguments.
1822   if ((getNumParams() < 1) ||
1823       (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1824       (getPrimaryTemplate() != nullptr) ||
1825       (getDescribedFunctionTemplate() != nullptr))
1826     return false;
1827 
1828   const ParmVarDecl *Param = getParamDecl(0);
1829 
1830   // Do we have a reference type?
1831   const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
1832   if (!ParamRefType)
1833     return false;
1834 
1835   // Is it a reference to our class type?
1836   ASTContext &Context = getASTContext();
1837 
1838   CanQualType PointeeType
1839     = Context.getCanonicalType(ParamRefType->getPointeeType());
1840   CanQualType ClassTy
1841     = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1842   if (PointeeType.getUnqualifiedType() != ClassTy)
1843     return false;
1844 
1845   // FIXME: other qualifiers?
1846 
1847   // We have a copy or move constructor.
1848   TypeQuals = PointeeType.getCVRQualifiers();
1849   return true;
1850 }
1851 
1852 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
1853   // C++ [class.conv.ctor]p1:
1854   //   A constructor declared without the function-specifier explicit
1855   //   that can be called with a single parameter specifies a
1856   //   conversion from the type of its first parameter to the type of
1857   //   its class. Such a constructor is called a converting
1858   //   constructor.
1859   if (isExplicit() && !AllowExplicit)
1860     return false;
1861 
1862   return (getNumParams() == 0 &&
1863           getType()->getAs<FunctionProtoType>()->isVariadic()) ||
1864          (getNumParams() == 1) ||
1865          (getNumParams() > 1 &&
1866           (getParamDecl(1)->hasDefaultArg() ||
1867            getParamDecl(1)->isParameterPack()));
1868 }
1869 
1870 bool CXXConstructorDecl::isSpecializationCopyingObject() const {
1871   if ((getNumParams() < 1) ||
1872       (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1873       (getDescribedFunctionTemplate() != nullptr))
1874     return false;
1875 
1876   const ParmVarDecl *Param = getParamDecl(0);
1877 
1878   ASTContext &Context = getASTContext();
1879   CanQualType ParamType = Context.getCanonicalType(Param->getType());
1880 
1881   // Is it the same as our our class type?
1882   CanQualType ClassTy
1883     = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1884   if (ParamType.getUnqualifiedType() != ClassTy)
1885     return false;
1886 
1887   return true;
1888 }
1889 
1890 const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1891   // Hack: we store the inherited constructor in the overridden method table
1892   method_iterator It = getASTContext().overridden_methods_begin(this);
1893   if (It == getASTContext().overridden_methods_end(this))
1894     return nullptr;
1895 
1896   return cast<CXXConstructorDecl>(*It);
1897 }
1898 
1899 void
1900 CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1901   // Hack: we store the inherited constructor in the overridden method table
1902   assert(getASTContext().overridden_methods_size(this) == 0 &&
1903          "Base ctor already set.");
1904   getASTContext().addOverriddenMethod(this, BaseCtor);
1905 }
1906 
1907 void CXXDestructorDecl::anchor() { }
1908 
1909 CXXDestructorDecl *
1910 CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1911   return new (C, ID)
1912       CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1913                         QualType(), nullptr, false, false);
1914 }
1915 
1916 CXXDestructorDecl *
1917 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1918                           SourceLocation StartLoc,
1919                           const DeclarationNameInfo &NameInfo,
1920                           QualType T, TypeSourceInfo *TInfo,
1921                           bool isInline, bool isImplicitlyDeclared) {
1922   assert(NameInfo.getName().getNameKind()
1923          == DeclarationName::CXXDestructorName &&
1924          "Name must refer to a destructor");
1925   return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
1926                                        isInline, isImplicitlyDeclared);
1927 }
1928 
1929 void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1930   auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1931   if (OD && !First->OperatorDelete) {
1932     First->OperatorDelete = OD;
1933     if (auto *L = getASTMutationListener())
1934       L->ResolvedOperatorDelete(First, OD);
1935   }
1936 }
1937 
1938 void CXXConversionDecl::anchor() { }
1939 
1940 CXXConversionDecl *
1941 CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1942   return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
1943                                        DeclarationNameInfo(), QualType(),
1944                                        nullptr, false, false, false,
1945                                        SourceLocation());
1946 }
1947 
1948 CXXConversionDecl *
1949 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
1950                           SourceLocation StartLoc,
1951                           const DeclarationNameInfo &NameInfo,
1952                           QualType T, TypeSourceInfo *TInfo,
1953                           bool isInline, bool isExplicit,
1954                           bool isConstexpr, SourceLocation EndLocation) {
1955   assert(NameInfo.getName().getNameKind()
1956          == DeclarationName::CXXConversionFunctionName &&
1957          "Name must refer to a conversion function");
1958   return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
1959                                        isInline, isExplicit, isConstexpr,
1960                                        EndLocation);
1961 }
1962 
1963 bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1964   return isImplicit() && getParent()->isLambda() &&
1965          getConversionType()->isBlockPointerType();
1966 }
1967 
1968 void LinkageSpecDecl::anchor() { }
1969 
1970 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
1971                                          DeclContext *DC,
1972                                          SourceLocation ExternLoc,
1973                                          SourceLocation LangLoc,
1974                                          LanguageIDs Lang,
1975                                          bool HasBraces) {
1976   return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
1977 }
1978 
1979 LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1980                                                      unsigned ID) {
1981   return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
1982                                      SourceLocation(), lang_c, false);
1983 }
1984 
1985 void UsingDirectiveDecl::anchor() { }
1986 
1987 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1988                                                SourceLocation L,
1989                                                SourceLocation NamespaceLoc,
1990                                            NestedNameSpecifierLoc QualifierLoc,
1991                                                SourceLocation IdentLoc,
1992                                                NamedDecl *Used,
1993                                                DeclContext *CommonAncestor) {
1994   if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1995     Used = NS->getOriginalNamespace();
1996   return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1997                                         IdentLoc, Used, CommonAncestor);
1998 }
1999 
2000 UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2001                                                            unsigned ID) {
2002   return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2003                                         SourceLocation(),
2004                                         NestedNameSpecifierLoc(),
2005                                         SourceLocation(), nullptr, nullptr);
2006 }
2007 
2008 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2009   if (NamespaceAliasDecl *NA =
2010         dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2011     return NA->getNamespace();
2012   return cast_or_null<NamespaceDecl>(NominatedNamespace);
2013 }
2014 
2015 NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2016                              SourceLocation StartLoc, SourceLocation IdLoc,
2017                              IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2018     : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2019       redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2020       AnonOrFirstNamespaceAndInline(nullptr, Inline) {
2021   setPreviousDecl(PrevDecl);
2022 
2023   if (PrevDecl)
2024     AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2025 }
2026 
2027 NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2028                                      bool Inline, SourceLocation StartLoc,
2029                                      SourceLocation IdLoc, IdentifierInfo *Id,
2030                                      NamespaceDecl *PrevDecl) {
2031   return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2032                                    PrevDecl);
2033 }
2034 
2035 NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2036   return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
2037                                    SourceLocation(), nullptr, nullptr);
2038 }
2039 
2040 NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2041   if (isFirstDecl())
2042     return this;
2043 
2044   return AnonOrFirstNamespaceAndInline.getPointer();
2045 }
2046 
2047 const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2048   if (isFirstDecl())
2049     return this;
2050 
2051   return AnonOrFirstNamespaceAndInline.getPointer();
2052 }
2053 
2054 bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2055 
2056 NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2057   return getNextRedeclaration();
2058 }
2059 NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2060   return getPreviousDecl();
2061 }
2062 NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2063   return getMostRecentDecl();
2064 }
2065 
2066 void NamespaceAliasDecl::anchor() { }
2067 
2068 NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2069   return getNextRedeclaration();
2070 }
2071 NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2072   return getPreviousDecl();
2073 }
2074 NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2075   return getMostRecentDecl();
2076 }
2077 
2078 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
2079                                                SourceLocation UsingLoc,
2080                                                SourceLocation AliasLoc,
2081                                                IdentifierInfo *Alias,
2082                                            NestedNameSpecifierLoc QualifierLoc,
2083                                                SourceLocation IdentLoc,
2084                                                NamedDecl *Namespace) {
2085   // FIXME: Preserve the aliased namespace as written.
2086   if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2087     Namespace = NS->getOriginalNamespace();
2088   return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
2089                                         QualifierLoc, IdentLoc, Namespace);
2090 }
2091 
2092 NamespaceAliasDecl *
2093 NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2094   return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
2095                                         SourceLocation(), nullptr,
2096                                         NestedNameSpecifierLoc(),
2097                                         SourceLocation(), nullptr);
2098 }
2099 
2100 void UsingShadowDecl::anchor() { }
2101 
2102 UsingShadowDecl *
2103 UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2104   return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2105                                      nullptr, nullptr);
2106 }
2107 
2108 UsingDecl *UsingShadowDecl::getUsingDecl() const {
2109   const UsingShadowDecl *Shadow = this;
2110   while (const UsingShadowDecl *NextShadow =
2111          dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2112     Shadow = NextShadow;
2113   return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2114 }
2115 
2116 void UsingDecl::anchor() { }
2117 
2118 void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2119   assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2120          "declaration already in set");
2121   assert(S->getUsingDecl() == this);
2122 
2123   if (FirstUsingShadow.getPointer())
2124     S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2125   FirstUsingShadow.setPointer(S);
2126 }
2127 
2128 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2129   assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2130          "declaration not in set");
2131   assert(S->getUsingDecl() == this);
2132 
2133   // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2134 
2135   if (FirstUsingShadow.getPointer() == S) {
2136     FirstUsingShadow.setPointer(
2137       dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
2138     S->UsingOrNextShadow = this;
2139     return;
2140   }
2141 
2142   UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
2143   while (Prev->UsingOrNextShadow != S)
2144     Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2145   Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2146   S->UsingOrNextShadow = this;
2147 }
2148 
2149 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2150                              NestedNameSpecifierLoc QualifierLoc,
2151                              const DeclarationNameInfo &NameInfo,
2152                              bool HasTypename) {
2153   return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
2154 }
2155 
2156 UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2157   return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2158                                NestedNameSpecifierLoc(), DeclarationNameInfo(),
2159                                false);
2160 }
2161 
2162 SourceRange UsingDecl::getSourceRange() const {
2163   SourceLocation Begin = isAccessDeclaration()
2164     ? getQualifierLoc().getBeginLoc() : UsingLocation;
2165   return SourceRange(Begin, getNameInfo().getEndLoc());
2166 }
2167 
2168 void UnresolvedUsingValueDecl::anchor() { }
2169 
2170 UnresolvedUsingValueDecl *
2171 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2172                                  SourceLocation UsingLoc,
2173                                  NestedNameSpecifierLoc QualifierLoc,
2174                                  const DeclarationNameInfo &NameInfo) {
2175   return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2176                                               QualifierLoc, NameInfo);
2177 }
2178 
2179 UnresolvedUsingValueDecl *
2180 UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2181   return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2182                                               SourceLocation(),
2183                                               NestedNameSpecifierLoc(),
2184                                               DeclarationNameInfo());
2185 }
2186 
2187 SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2188   SourceLocation Begin = isAccessDeclaration()
2189     ? getQualifierLoc().getBeginLoc() : UsingLocation;
2190   return SourceRange(Begin, getNameInfo().getEndLoc());
2191 }
2192 
2193 void UnresolvedUsingTypenameDecl::anchor() { }
2194 
2195 UnresolvedUsingTypenameDecl *
2196 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2197                                     SourceLocation UsingLoc,
2198                                     SourceLocation TypenameLoc,
2199                                     NestedNameSpecifierLoc QualifierLoc,
2200                                     SourceLocation TargetNameLoc,
2201                                     DeclarationName TargetName) {
2202   return new (C, DC) UnresolvedUsingTypenameDecl(
2203       DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2204       TargetName.getAsIdentifierInfo());
2205 }
2206 
2207 UnresolvedUsingTypenameDecl *
2208 UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2209   return new (C, ID) UnresolvedUsingTypenameDecl(
2210       nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2211       SourceLocation(), nullptr);
2212 }
2213 
2214 void StaticAssertDecl::anchor() { }
2215 
2216 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
2217                                            SourceLocation StaticAssertLoc,
2218                                            Expr *AssertExpr,
2219                                            StringLiteral *Message,
2220                                            SourceLocation RParenLoc,
2221                                            bool Failed) {
2222   return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2223                                       RParenLoc, Failed);
2224 }
2225 
2226 StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2227                                                        unsigned ID) {
2228   return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2229                                       nullptr, SourceLocation(), false);
2230 }
2231 
2232 MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2233                                        SourceLocation L, DeclarationName N,
2234                                        QualType T, TypeSourceInfo *TInfo,
2235                                        SourceLocation StartL,
2236                                        IdentifierInfo *Getter,
2237                                        IdentifierInfo *Setter) {
2238   return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2239 }
2240 
2241 MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2242                                                    unsigned ID) {
2243   return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2244                                     DeclarationName(), QualType(), nullptr,
2245                                     SourceLocation(), nullptr, nullptr);
2246 }
2247 
2248 static const char *getAccessName(AccessSpecifier AS) {
2249   switch (AS) {
2250     case AS_none:
2251       llvm_unreachable("Invalid access specifier!");
2252     case AS_public:
2253       return "public";
2254     case AS_private:
2255       return "private";
2256     case AS_protected:
2257       return "protected";
2258   }
2259   llvm_unreachable("Invalid access specifier!");
2260 }
2261 
2262 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2263                                            AccessSpecifier AS) {
2264   return DB << getAccessName(AS);
2265 }
2266 
2267 const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2268                                            AccessSpecifier AS) {
2269   return DB << getAccessName(AS);
2270 }
2271