xref: /openbsd-src/gnu/llvm/clang/lib/AST/DeclCXX.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file implements the C++ related Decl classes.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
14e5dd7070Spatrick #include "clang/AST/ASTContext.h"
15e5dd7070Spatrick #include "clang/AST/ASTLambda.h"
16e5dd7070Spatrick #include "clang/AST/ASTMutationListener.h"
17e5dd7070Spatrick #include "clang/AST/ASTUnresolvedSet.h"
18e5dd7070Spatrick #include "clang/AST/Attr.h"
19e5dd7070Spatrick #include "clang/AST/CXXInheritance.h"
20e5dd7070Spatrick #include "clang/AST/DeclBase.h"
21e5dd7070Spatrick #include "clang/AST/DeclTemplate.h"
22e5dd7070Spatrick #include "clang/AST/DeclarationName.h"
23e5dd7070Spatrick #include "clang/AST/Expr.h"
24e5dd7070Spatrick #include "clang/AST/ExprCXX.h"
25e5dd7070Spatrick #include "clang/AST/LambdaCapture.h"
26e5dd7070Spatrick #include "clang/AST/NestedNameSpecifier.h"
27e5dd7070Spatrick #include "clang/AST/ODRHash.h"
28e5dd7070Spatrick #include "clang/AST/Type.h"
29e5dd7070Spatrick #include "clang/AST/TypeLoc.h"
30e5dd7070Spatrick #include "clang/AST/UnresolvedSet.h"
31e5dd7070Spatrick #include "clang/Basic/Diagnostic.h"
32e5dd7070Spatrick #include "clang/Basic/IdentifierTable.h"
33e5dd7070Spatrick #include "clang/Basic/LLVM.h"
34e5dd7070Spatrick #include "clang/Basic/LangOptions.h"
35e5dd7070Spatrick #include "clang/Basic/OperatorKinds.h"
36e5dd7070Spatrick #include "clang/Basic/PartialDiagnostic.h"
37e5dd7070Spatrick #include "clang/Basic/SourceLocation.h"
38e5dd7070Spatrick #include "clang/Basic/Specifiers.h"
39*12c85518Srobert #include "clang/Basic/TargetInfo.h"
40e5dd7070Spatrick #include "llvm/ADT/SmallPtrSet.h"
41e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
42e5dd7070Spatrick #include "llvm/ADT/iterator_range.h"
43e5dd7070Spatrick #include "llvm/Support/Casting.h"
44e5dd7070Spatrick #include "llvm/Support/ErrorHandling.h"
45ec727ea7Spatrick #include "llvm/Support/Format.h"
46e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
47e5dd7070Spatrick #include <algorithm>
48e5dd7070Spatrick #include <cassert>
49e5dd7070Spatrick #include <cstddef>
50e5dd7070Spatrick #include <cstdint>
51e5dd7070Spatrick 
52e5dd7070Spatrick using namespace clang;
53e5dd7070Spatrick 
54e5dd7070Spatrick //===----------------------------------------------------------------------===//
55e5dd7070Spatrick // Decl Allocation/Deallocation Method Implementations
56e5dd7070Spatrick //===----------------------------------------------------------------------===//
57e5dd7070Spatrick 
anchor()58e5dd7070Spatrick void AccessSpecDecl::anchor() {}
59e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)60e5dd7070Spatrick AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
61e5dd7070Spatrick   return new (C, ID) AccessSpecDecl(EmptyShell());
62e5dd7070Spatrick }
63e5dd7070Spatrick 
getFromExternalSource(ASTContext & C) const64e5dd7070Spatrick void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
65e5dd7070Spatrick   ExternalASTSource *Source = C.getExternalSource();
66e5dd7070Spatrick   assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
67e5dd7070Spatrick   assert(Source && "getFromExternalSource with no external source");
68e5dd7070Spatrick 
69e5dd7070Spatrick   for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
70e5dd7070Spatrick     I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
71e5dd7070Spatrick         reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
72e5dd7070Spatrick   Impl.Decls.setLazy(false);
73e5dd7070Spatrick }
74e5dd7070Spatrick 
DefinitionData(CXXRecordDecl * D)75e5dd7070Spatrick CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
76e5dd7070Spatrick     : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
77e5dd7070Spatrick       Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
78e5dd7070Spatrick       Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
79e5dd7070Spatrick       HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
80e5dd7070Spatrick       HasPrivateFields(false), HasProtectedFields(false),
81e5dd7070Spatrick       HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
82*12c85518Srobert       HasOnlyCMembers(true), HasInitMethod(false), HasInClassInitializer(false),
83e5dd7070Spatrick       HasUninitializedReferenceMember(false), HasUninitializedFields(false),
84*12c85518Srobert       HasInheritedConstructor(false), HasInheritedDefaultConstructor(false),
85a9ac8606Spatrick       HasInheritedAssignment(false),
86e5dd7070Spatrick       NeedOverloadResolutionForCopyConstructor(false),
87e5dd7070Spatrick       NeedOverloadResolutionForMoveConstructor(false),
88ec727ea7Spatrick       NeedOverloadResolutionForCopyAssignment(false),
89e5dd7070Spatrick       NeedOverloadResolutionForMoveAssignment(false),
90e5dd7070Spatrick       NeedOverloadResolutionForDestructor(false),
91e5dd7070Spatrick       DefaultedCopyConstructorIsDeleted(false),
92e5dd7070Spatrick       DefaultedMoveConstructorIsDeleted(false),
93ec727ea7Spatrick       DefaultedCopyAssignmentIsDeleted(false),
94e5dd7070Spatrick       DefaultedMoveAssignmentIsDeleted(false),
95e5dd7070Spatrick       DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
96e5dd7070Spatrick       HasTrivialSpecialMembersForCall(SMF_All),
97e5dd7070Spatrick       DeclaredNonTrivialSpecialMembers(0),
98e5dd7070Spatrick       DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
99e5dd7070Spatrick       HasConstexprNonCopyMoveConstructor(false),
100e5dd7070Spatrick       HasDefaultedDefaultConstructor(false),
101e5dd7070Spatrick       DefaultedDefaultConstructorIsConstexpr(true),
102e5dd7070Spatrick       HasConstexprDefaultConstructor(false),
103e5dd7070Spatrick       DefaultedDestructorIsConstexpr(true),
104a9ac8606Spatrick       HasNonLiteralTypeFieldsOrBases(false), StructuralIfLiteral(true),
105e5dd7070Spatrick       UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
106e5dd7070Spatrick       ImplicitCopyConstructorCanHaveConstParamForVBase(true),
107e5dd7070Spatrick       ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
108e5dd7070Spatrick       ImplicitCopyAssignmentHasConstParam(true),
109e5dd7070Spatrick       HasDeclaredCopyConstructorWithConstParam(false),
110a9ac8606Spatrick       HasDeclaredCopyAssignmentWithConstParam(false),
111a9ac8606Spatrick       IsAnyDestructorNoReturn(false), IsLambda(false),
112e5dd7070Spatrick       IsParsingBaseSpecifiers(false), ComputedVisibleConversions(false),
113e5dd7070Spatrick       HasODRHash(false), Definition(D) {}
114e5dd7070Spatrick 
getBasesSlowCase() const115e5dd7070Spatrick CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
116e5dd7070Spatrick   return Bases.get(Definition->getASTContext().getExternalSource());
117e5dd7070Spatrick }
118e5dd7070Spatrick 
getVBasesSlowCase() const119e5dd7070Spatrick CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
120e5dd7070Spatrick   return VBases.get(Definition->getASTContext().getExternalSource());
121e5dd7070Spatrick }
122e5dd7070Spatrick 
CXXRecordDecl(Kind K,TagKind TK,const ASTContext & C,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,IdentifierInfo * Id,CXXRecordDecl * PrevDecl)123e5dd7070Spatrick CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
124e5dd7070Spatrick                              DeclContext *DC, SourceLocation StartLoc,
125e5dd7070Spatrick                              SourceLocation IdLoc, IdentifierInfo *Id,
126e5dd7070Spatrick                              CXXRecordDecl *PrevDecl)
127e5dd7070Spatrick     : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
128e5dd7070Spatrick       DefinitionData(PrevDecl ? PrevDecl->DefinitionData
129e5dd7070Spatrick                               : nullptr) {}
130e5dd7070Spatrick 
Create(const ASTContext & C,TagKind TK,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,IdentifierInfo * Id,CXXRecordDecl * PrevDecl,bool DelayTypeCreation)131e5dd7070Spatrick CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
132e5dd7070Spatrick                                      DeclContext *DC, SourceLocation StartLoc,
133e5dd7070Spatrick                                      SourceLocation IdLoc, IdentifierInfo *Id,
134e5dd7070Spatrick                                      CXXRecordDecl *PrevDecl,
135e5dd7070Spatrick                                      bool DelayTypeCreation) {
136e5dd7070Spatrick   auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
137e5dd7070Spatrick                                       PrevDecl);
138e5dd7070Spatrick   R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
139e5dd7070Spatrick 
140e5dd7070Spatrick   // FIXME: DelayTypeCreation seems like such a hack
141e5dd7070Spatrick   if (!DelayTypeCreation)
142e5dd7070Spatrick     C.getTypeDeclType(R, PrevDecl);
143e5dd7070Spatrick   return R;
144e5dd7070Spatrick }
145e5dd7070Spatrick 
146e5dd7070Spatrick CXXRecordDecl *
CreateLambda(const ASTContext & C,DeclContext * DC,TypeSourceInfo * Info,SourceLocation Loc,unsigned DependencyKind,bool IsGeneric,LambdaCaptureDefault CaptureDefault)147e5dd7070Spatrick CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
148e5dd7070Spatrick                             TypeSourceInfo *Info, SourceLocation Loc,
149*12c85518Srobert                             unsigned DependencyKind, bool IsGeneric,
150e5dd7070Spatrick                             LambdaCaptureDefault CaptureDefault) {
151e5dd7070Spatrick   auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
152e5dd7070Spatrick                                       nullptr, nullptr);
153e5dd7070Spatrick   R->setBeingDefined(true);
154*12c85518Srobert   R->DefinitionData = new (C) struct LambdaDefinitionData(
155*12c85518Srobert       R, Info, DependencyKind, IsGeneric, CaptureDefault);
156e5dd7070Spatrick   R->setMayHaveOutOfDateDef(false);
157e5dd7070Spatrick   R->setImplicit(true);
158*12c85518Srobert 
159e5dd7070Spatrick   C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
160e5dd7070Spatrick   return R;
161e5dd7070Spatrick }
162e5dd7070Spatrick 
163e5dd7070Spatrick CXXRecordDecl *
CreateDeserialized(const ASTContext & C,unsigned ID)164e5dd7070Spatrick CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
165e5dd7070Spatrick   auto *R = new (C, ID) CXXRecordDecl(
166e5dd7070Spatrick       CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
167e5dd7070Spatrick       nullptr, nullptr);
168e5dd7070Spatrick   R->setMayHaveOutOfDateDef(false);
169e5dd7070Spatrick   return R;
170e5dd7070Spatrick }
171e5dd7070Spatrick 
172e5dd7070Spatrick /// Determine whether a class has a repeated base class. This is intended for
173e5dd7070Spatrick /// use when determining if a class is standard-layout, so makes no attempt to
174e5dd7070Spatrick /// handle virtual bases.
hasRepeatedBaseClass(const CXXRecordDecl * StartRD)175e5dd7070Spatrick static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
176e5dd7070Spatrick   llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
177e5dd7070Spatrick   SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
178e5dd7070Spatrick   while (!WorkList.empty()) {
179e5dd7070Spatrick     const CXXRecordDecl *RD = WorkList.pop_back_val();
180*12c85518Srobert     if (RD->getTypeForDecl()->isDependentType())
181*12c85518Srobert       continue;
182e5dd7070Spatrick     for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
183e5dd7070Spatrick       if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
184e5dd7070Spatrick         if (!SeenBaseTypes.insert(B).second)
185e5dd7070Spatrick           return true;
186e5dd7070Spatrick         WorkList.push_back(B);
187e5dd7070Spatrick       }
188e5dd7070Spatrick     }
189e5dd7070Spatrick   }
190e5dd7070Spatrick   return false;
191e5dd7070Spatrick }
192e5dd7070Spatrick 
193e5dd7070Spatrick void
setBases(CXXBaseSpecifier const * const * Bases,unsigned NumBases)194e5dd7070Spatrick CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
195e5dd7070Spatrick                         unsigned NumBases) {
196e5dd7070Spatrick   ASTContext &C = getASTContext();
197e5dd7070Spatrick 
198e5dd7070Spatrick   if (!data().Bases.isOffset() && data().NumBases > 0)
199e5dd7070Spatrick     C.Deallocate(data().getBases());
200e5dd7070Spatrick 
201e5dd7070Spatrick   if (NumBases) {
202e5dd7070Spatrick     if (!C.getLangOpts().CPlusPlus17) {
203e5dd7070Spatrick       // C++ [dcl.init.aggr]p1:
204e5dd7070Spatrick       //   An aggregate is [...] a class with [...] no base classes [...].
205e5dd7070Spatrick       data().Aggregate = false;
206e5dd7070Spatrick     }
207e5dd7070Spatrick 
208e5dd7070Spatrick     // C++ [class]p4:
209e5dd7070Spatrick     //   A POD-struct is an aggregate class...
210e5dd7070Spatrick     data().PlainOldData = false;
211e5dd7070Spatrick   }
212e5dd7070Spatrick 
213e5dd7070Spatrick   // The set of seen virtual base types.
214e5dd7070Spatrick   llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
215e5dd7070Spatrick 
216e5dd7070Spatrick   // The virtual bases of this class.
217e5dd7070Spatrick   SmallVector<const CXXBaseSpecifier *, 8> VBases;
218e5dd7070Spatrick 
219e5dd7070Spatrick   data().Bases = new(C) CXXBaseSpecifier [NumBases];
220e5dd7070Spatrick   data().NumBases = NumBases;
221e5dd7070Spatrick   for (unsigned i = 0; i < NumBases; ++i) {
222e5dd7070Spatrick     data().getBases()[i] = *Bases[i];
223e5dd7070Spatrick     // Keep track of inherited vbases for this base class.
224e5dd7070Spatrick     const CXXBaseSpecifier *Base = Bases[i];
225e5dd7070Spatrick     QualType BaseType = Base->getType();
226e5dd7070Spatrick     // Skip dependent types; we can't do any checking on them now.
227e5dd7070Spatrick     if (BaseType->isDependentType())
228e5dd7070Spatrick       continue;
229e5dd7070Spatrick     auto *BaseClassDecl =
230e5dd7070Spatrick         cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
231e5dd7070Spatrick 
232e5dd7070Spatrick     // C++2a [class]p7:
233e5dd7070Spatrick     //   A standard-layout class is a class that:
234e5dd7070Spatrick     //    [...]
235e5dd7070Spatrick     //    -- has all non-static data members and bit-fields in the class and
236e5dd7070Spatrick     //       its base classes first declared in the same class
237e5dd7070Spatrick     if (BaseClassDecl->data().HasBasesWithFields ||
238e5dd7070Spatrick         !BaseClassDecl->field_empty()) {
239e5dd7070Spatrick       if (data().HasBasesWithFields)
240e5dd7070Spatrick         // Two bases have members or bit-fields: not standard-layout.
241e5dd7070Spatrick         data().IsStandardLayout = false;
242e5dd7070Spatrick       data().HasBasesWithFields = true;
243e5dd7070Spatrick     }
244e5dd7070Spatrick 
245e5dd7070Spatrick     // C++11 [class]p7:
246e5dd7070Spatrick     //   A standard-layout class is a class that:
247e5dd7070Spatrick     //     -- [...] has [...] at most one base class with non-static data
248e5dd7070Spatrick     //        members
249e5dd7070Spatrick     if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
250e5dd7070Spatrick         BaseClassDecl->hasDirectFields()) {
251e5dd7070Spatrick       if (data().HasBasesWithNonStaticDataMembers)
252e5dd7070Spatrick         data().IsCXX11StandardLayout = false;
253e5dd7070Spatrick       data().HasBasesWithNonStaticDataMembers = true;
254e5dd7070Spatrick     }
255e5dd7070Spatrick 
256e5dd7070Spatrick     if (!BaseClassDecl->isEmpty()) {
257e5dd7070Spatrick       // C++14 [meta.unary.prop]p4:
258e5dd7070Spatrick       //   T is a class type [...] with [...] no base class B for which
259e5dd7070Spatrick       //   is_empty<B>::value is false.
260e5dd7070Spatrick       data().Empty = false;
261e5dd7070Spatrick     }
262e5dd7070Spatrick 
263e5dd7070Spatrick     // C++1z [dcl.init.agg]p1:
264e5dd7070Spatrick     //   An aggregate is a class with [...] no private or protected base classes
265a9ac8606Spatrick     if (Base->getAccessSpecifier() != AS_public) {
266e5dd7070Spatrick       data().Aggregate = false;
267e5dd7070Spatrick 
268a9ac8606Spatrick       // C++20 [temp.param]p7:
269a9ac8606Spatrick       //   A structural type is [...] a literal class type with [...] all base
270a9ac8606Spatrick       //   classes [...] public
271a9ac8606Spatrick       data().StructuralIfLiteral = false;
272a9ac8606Spatrick     }
273a9ac8606Spatrick 
274e5dd7070Spatrick     // C++ [class.virtual]p1:
275e5dd7070Spatrick     //   A class that declares or inherits a virtual function is called a
276e5dd7070Spatrick     //   polymorphic class.
277e5dd7070Spatrick     if (BaseClassDecl->isPolymorphic()) {
278e5dd7070Spatrick       data().Polymorphic = true;
279e5dd7070Spatrick 
280e5dd7070Spatrick       //   An aggregate is a class with [...] no virtual functions.
281e5dd7070Spatrick       data().Aggregate = false;
282e5dd7070Spatrick     }
283e5dd7070Spatrick 
284e5dd7070Spatrick     // C++0x [class]p7:
285e5dd7070Spatrick     //   A standard-layout class is a class that: [...]
286e5dd7070Spatrick     //    -- has no non-standard-layout base classes
287e5dd7070Spatrick     if (!BaseClassDecl->isStandardLayout())
288e5dd7070Spatrick       data().IsStandardLayout = false;
289e5dd7070Spatrick     if (!BaseClassDecl->isCXX11StandardLayout())
290e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
291e5dd7070Spatrick 
292e5dd7070Spatrick     // Record if this base is the first non-literal field or base.
293e5dd7070Spatrick     if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
294e5dd7070Spatrick       data().HasNonLiteralTypeFieldsOrBases = true;
295e5dd7070Spatrick 
296e5dd7070Spatrick     // Now go through all virtual bases of this base and add them.
297e5dd7070Spatrick     for (const auto &VBase : BaseClassDecl->vbases()) {
298e5dd7070Spatrick       // Add this base if it's not already in the list.
299e5dd7070Spatrick       if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
300e5dd7070Spatrick         VBases.push_back(&VBase);
301e5dd7070Spatrick 
302e5dd7070Spatrick         // C++11 [class.copy]p8:
303e5dd7070Spatrick         //   The implicitly-declared copy constructor for a class X will have
304e5dd7070Spatrick         //   the form 'X::X(const X&)' if each [...] virtual base class B of X
305e5dd7070Spatrick         //   has a copy constructor whose first parameter is of type
306e5dd7070Spatrick         //   'const B&' or 'const volatile B&' [...]
307e5dd7070Spatrick         if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
308e5dd7070Spatrick           if (!VBaseDecl->hasCopyConstructorWithConstParam())
309e5dd7070Spatrick             data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
310e5dd7070Spatrick 
311e5dd7070Spatrick         // C++1z [dcl.init.agg]p1:
312e5dd7070Spatrick         //   An aggregate is a class with [...] no virtual base classes
313e5dd7070Spatrick         data().Aggregate = false;
314e5dd7070Spatrick       }
315e5dd7070Spatrick     }
316e5dd7070Spatrick 
317e5dd7070Spatrick     if (Base->isVirtual()) {
318e5dd7070Spatrick       // Add this base if it's not already in the list.
319e5dd7070Spatrick       if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
320e5dd7070Spatrick         VBases.push_back(Base);
321e5dd7070Spatrick 
322e5dd7070Spatrick       // C++14 [meta.unary.prop] is_empty:
323e5dd7070Spatrick       //   T is a class type, but not a union type, with ... no virtual base
324e5dd7070Spatrick       //   classes
325e5dd7070Spatrick       data().Empty = false;
326e5dd7070Spatrick 
327e5dd7070Spatrick       // C++1z [dcl.init.agg]p1:
328e5dd7070Spatrick       //   An aggregate is a class with [...] no virtual base classes
329e5dd7070Spatrick       data().Aggregate = false;
330e5dd7070Spatrick 
331e5dd7070Spatrick       // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
332e5dd7070Spatrick       //   A [default constructor, copy/move constructor, or copy/move assignment
333e5dd7070Spatrick       //   operator for a class X] is trivial [...] if:
334e5dd7070Spatrick       //    -- class X has [...] no virtual base classes
335e5dd7070Spatrick       data().HasTrivialSpecialMembers &= SMF_Destructor;
336e5dd7070Spatrick       data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
337e5dd7070Spatrick 
338e5dd7070Spatrick       // C++0x [class]p7:
339e5dd7070Spatrick       //   A standard-layout class is a class that: [...]
340e5dd7070Spatrick       //    -- has [...] no virtual base classes
341e5dd7070Spatrick       data().IsStandardLayout = false;
342e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
343e5dd7070Spatrick 
344e5dd7070Spatrick       // C++20 [dcl.constexpr]p3:
345e5dd7070Spatrick       //   In the definition of a constexpr function [...]
346e5dd7070Spatrick       //    -- if the function is a constructor or destructor,
347e5dd7070Spatrick       //       its class shall not have any virtual base classes
348e5dd7070Spatrick       data().DefaultedDefaultConstructorIsConstexpr = false;
349e5dd7070Spatrick       data().DefaultedDestructorIsConstexpr = false;
350e5dd7070Spatrick 
351e5dd7070Spatrick       // C++1z [class.copy]p8:
352e5dd7070Spatrick       //   The implicitly-declared copy constructor for a class X will have
353e5dd7070Spatrick       //   the form 'X::X(const X&)' if each potentially constructed subobject
354e5dd7070Spatrick       //   has a copy constructor whose first parameter is of type
355e5dd7070Spatrick       //   'const B&' or 'const volatile B&' [...]
356e5dd7070Spatrick       if (!BaseClassDecl->hasCopyConstructorWithConstParam())
357e5dd7070Spatrick         data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
358e5dd7070Spatrick     } else {
359e5dd7070Spatrick       // C++ [class.ctor]p5:
360e5dd7070Spatrick       //   A default constructor is trivial [...] if:
361e5dd7070Spatrick       //    -- all the direct base classes of its class have trivial default
362e5dd7070Spatrick       //       constructors.
363e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialDefaultConstructor())
364e5dd7070Spatrick         data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
365e5dd7070Spatrick 
366e5dd7070Spatrick       // C++0x [class.copy]p13:
367e5dd7070Spatrick       //   A copy/move constructor for class X is trivial if [...]
368e5dd7070Spatrick       //    [...]
369e5dd7070Spatrick       //    -- the constructor selected to copy/move each direct base class
370e5dd7070Spatrick       //       subobject is trivial, and
371e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialCopyConstructor())
372e5dd7070Spatrick         data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
373e5dd7070Spatrick 
374e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
375e5dd7070Spatrick         data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
376e5dd7070Spatrick 
377e5dd7070Spatrick       // If the base class doesn't have a simple move constructor, we'll eagerly
378e5dd7070Spatrick       // declare it and perform overload resolution to determine which function
379e5dd7070Spatrick       // it actually calls. If it does have a simple move constructor, this
380e5dd7070Spatrick       // check is correct.
381e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialMoveConstructor())
382e5dd7070Spatrick         data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
383e5dd7070Spatrick 
384e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
385e5dd7070Spatrick         data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
386e5dd7070Spatrick 
387e5dd7070Spatrick       // C++0x [class.copy]p27:
388e5dd7070Spatrick       //   A copy/move assignment operator for class X is trivial if [...]
389e5dd7070Spatrick       //    [...]
390e5dd7070Spatrick       //    -- the assignment operator selected to copy/move each direct base
391e5dd7070Spatrick       //       class subobject is trivial, and
392e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialCopyAssignment())
393e5dd7070Spatrick         data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
394e5dd7070Spatrick       // If the base class doesn't have a simple move assignment, we'll eagerly
395e5dd7070Spatrick       // declare it and perform overload resolution to determine which function
396e5dd7070Spatrick       // it actually calls. If it does have a simple move assignment, this
397e5dd7070Spatrick       // check is correct.
398e5dd7070Spatrick       if (!BaseClassDecl->hasTrivialMoveAssignment())
399e5dd7070Spatrick         data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
400e5dd7070Spatrick 
401e5dd7070Spatrick       // C++11 [class.ctor]p6:
402e5dd7070Spatrick       //   If that user-written default constructor would satisfy the
403e5dd7070Spatrick       //   requirements of a constexpr constructor, the implicitly-defined
404e5dd7070Spatrick       //   default constructor is constexpr.
405e5dd7070Spatrick       if (!BaseClassDecl->hasConstexprDefaultConstructor())
406e5dd7070Spatrick         data().DefaultedDefaultConstructorIsConstexpr = false;
407e5dd7070Spatrick 
408e5dd7070Spatrick       // C++1z [class.copy]p8:
409e5dd7070Spatrick       //   The implicitly-declared copy constructor for a class X will have
410e5dd7070Spatrick       //   the form 'X::X(const X&)' if each potentially constructed subobject
411e5dd7070Spatrick       //   has a copy constructor whose first parameter is of type
412e5dd7070Spatrick       //   'const B&' or 'const volatile B&' [...]
413e5dd7070Spatrick       if (!BaseClassDecl->hasCopyConstructorWithConstParam())
414e5dd7070Spatrick         data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
415e5dd7070Spatrick     }
416e5dd7070Spatrick 
417e5dd7070Spatrick     // C++ [class.ctor]p3:
418e5dd7070Spatrick     //   A destructor is trivial if all the direct base classes of its class
419e5dd7070Spatrick     //   have trivial destructors.
420e5dd7070Spatrick     if (!BaseClassDecl->hasTrivialDestructor())
421e5dd7070Spatrick       data().HasTrivialSpecialMembers &= ~SMF_Destructor;
422e5dd7070Spatrick 
423e5dd7070Spatrick     if (!BaseClassDecl->hasTrivialDestructorForCall())
424e5dd7070Spatrick       data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
425e5dd7070Spatrick 
426e5dd7070Spatrick     if (!BaseClassDecl->hasIrrelevantDestructor())
427e5dd7070Spatrick       data().HasIrrelevantDestructor = false;
428e5dd7070Spatrick 
429a9ac8606Spatrick     if (BaseClassDecl->isAnyDestructorNoReturn())
430a9ac8606Spatrick       data().IsAnyDestructorNoReturn = true;
431a9ac8606Spatrick 
432e5dd7070Spatrick     // C++11 [class.copy]p18:
433e5dd7070Spatrick     //   The implicitly-declared copy assignment operator for a class X will
434e5dd7070Spatrick     //   have the form 'X& X::operator=(const X&)' if each direct base class B
435e5dd7070Spatrick     //   of X has a copy assignment operator whose parameter is of type 'const
436e5dd7070Spatrick     //   B&', 'const volatile B&', or 'B' [...]
437e5dd7070Spatrick     if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
438e5dd7070Spatrick       data().ImplicitCopyAssignmentHasConstParam = false;
439e5dd7070Spatrick 
440e5dd7070Spatrick     // A class has an Objective-C object member if... or any of its bases
441e5dd7070Spatrick     // has an Objective-C object member.
442e5dd7070Spatrick     if (BaseClassDecl->hasObjectMember())
443e5dd7070Spatrick       setHasObjectMember(true);
444e5dd7070Spatrick 
445e5dd7070Spatrick     if (BaseClassDecl->hasVolatileMember())
446e5dd7070Spatrick       setHasVolatileMember(true);
447e5dd7070Spatrick 
448e5dd7070Spatrick     if (BaseClassDecl->getArgPassingRestrictions() ==
449e5dd7070Spatrick         RecordDecl::APK_CanNeverPassInRegs)
450e5dd7070Spatrick       setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
451e5dd7070Spatrick 
452e5dd7070Spatrick     // Keep track of the presence of mutable fields.
453ec727ea7Spatrick     if (BaseClassDecl->hasMutableFields())
454e5dd7070Spatrick       data().HasMutableFields = true;
455e5dd7070Spatrick 
456e5dd7070Spatrick     if (BaseClassDecl->hasUninitializedReferenceMember())
457e5dd7070Spatrick       data().HasUninitializedReferenceMember = true;
458e5dd7070Spatrick 
459e5dd7070Spatrick     if (!BaseClassDecl->allowConstDefaultInit())
460e5dd7070Spatrick       data().HasUninitializedFields = true;
461e5dd7070Spatrick 
462e5dd7070Spatrick     addedClassSubobject(BaseClassDecl);
463e5dd7070Spatrick   }
464e5dd7070Spatrick 
465e5dd7070Spatrick   // C++2a [class]p7:
466e5dd7070Spatrick   //   A class S is a standard-layout class if it:
467e5dd7070Spatrick   //     -- has at most one base class subobject of any given type
468e5dd7070Spatrick   //
469e5dd7070Spatrick   // Note that we only need to check this for classes with more than one base
470e5dd7070Spatrick   // class. If there's only one base class, and it's standard layout, then
471e5dd7070Spatrick   // we know there are no repeated base classes.
472e5dd7070Spatrick   if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
473e5dd7070Spatrick     data().IsStandardLayout = false;
474e5dd7070Spatrick 
475e5dd7070Spatrick   if (VBases.empty()) {
476e5dd7070Spatrick     data().IsParsingBaseSpecifiers = false;
477e5dd7070Spatrick     return;
478e5dd7070Spatrick   }
479e5dd7070Spatrick 
480e5dd7070Spatrick   // Create base specifier for any direct or indirect virtual bases.
481e5dd7070Spatrick   data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
482e5dd7070Spatrick   data().NumVBases = VBases.size();
483e5dd7070Spatrick   for (int I = 0, E = VBases.size(); I != E; ++I) {
484e5dd7070Spatrick     QualType Type = VBases[I]->getType();
485e5dd7070Spatrick     if (!Type->isDependentType())
486e5dd7070Spatrick       addedClassSubobject(Type->getAsCXXRecordDecl());
487e5dd7070Spatrick     data().getVBases()[I] = *VBases[I];
488e5dd7070Spatrick   }
489e5dd7070Spatrick 
490e5dd7070Spatrick   data().IsParsingBaseSpecifiers = false;
491e5dd7070Spatrick }
492e5dd7070Spatrick 
getODRHash() const493e5dd7070Spatrick unsigned CXXRecordDecl::getODRHash() const {
494e5dd7070Spatrick   assert(hasDefinition() && "ODRHash only for records with definitions");
495e5dd7070Spatrick 
496e5dd7070Spatrick   // Previously calculated hash is stored in DefinitionData.
497e5dd7070Spatrick   if (DefinitionData->HasODRHash)
498e5dd7070Spatrick     return DefinitionData->ODRHash;
499e5dd7070Spatrick 
500e5dd7070Spatrick   // Only calculate hash on first call of getODRHash per record.
501e5dd7070Spatrick   ODRHash Hash;
502e5dd7070Spatrick   Hash.AddCXXRecordDecl(getDefinition());
503e5dd7070Spatrick   DefinitionData->HasODRHash = true;
504e5dd7070Spatrick   DefinitionData->ODRHash = Hash.CalculateHash();
505e5dd7070Spatrick 
506e5dd7070Spatrick   return DefinitionData->ODRHash;
507e5dd7070Spatrick }
508e5dd7070Spatrick 
addedClassSubobject(CXXRecordDecl * Subobj)509e5dd7070Spatrick void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
510e5dd7070Spatrick   // C++11 [class.copy]p11:
511e5dd7070Spatrick   //   A defaulted copy/move constructor for a class X is defined as
512e5dd7070Spatrick   //   deleted if X has:
513e5dd7070Spatrick   //    -- a direct or virtual base class B that cannot be copied/moved [...]
514e5dd7070Spatrick   //    -- a non-static data member of class type M (or array thereof)
515e5dd7070Spatrick   //       that cannot be copied or moved [...]
516e5dd7070Spatrick   if (!Subobj->hasSimpleCopyConstructor())
517e5dd7070Spatrick     data().NeedOverloadResolutionForCopyConstructor = true;
518e5dd7070Spatrick   if (!Subobj->hasSimpleMoveConstructor())
519e5dd7070Spatrick     data().NeedOverloadResolutionForMoveConstructor = true;
520e5dd7070Spatrick 
521e5dd7070Spatrick   // C++11 [class.copy]p23:
522e5dd7070Spatrick   //   A defaulted copy/move assignment operator for a class X is defined as
523e5dd7070Spatrick   //   deleted if X has:
524e5dd7070Spatrick   //    -- a direct or virtual base class B that cannot be copied/moved [...]
525e5dd7070Spatrick   //    -- a non-static data member of class type M (or array thereof)
526e5dd7070Spatrick   //        that cannot be copied or moved [...]
527ec727ea7Spatrick   if (!Subobj->hasSimpleCopyAssignment())
528ec727ea7Spatrick     data().NeedOverloadResolutionForCopyAssignment = true;
529e5dd7070Spatrick   if (!Subobj->hasSimpleMoveAssignment())
530e5dd7070Spatrick     data().NeedOverloadResolutionForMoveAssignment = true;
531e5dd7070Spatrick 
532e5dd7070Spatrick   // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
533e5dd7070Spatrick   //   A defaulted [ctor or dtor] for a class X is defined as
534e5dd7070Spatrick   //   deleted if X has:
535e5dd7070Spatrick   //    -- any direct or virtual base class [...] has a type with a destructor
536e5dd7070Spatrick   //       that is deleted or inaccessible from the defaulted [ctor or dtor].
537e5dd7070Spatrick   //    -- any non-static data member has a type with a destructor
538e5dd7070Spatrick   //       that is deleted or inaccessible from the defaulted [ctor or dtor].
539e5dd7070Spatrick   if (!Subobj->hasSimpleDestructor()) {
540e5dd7070Spatrick     data().NeedOverloadResolutionForCopyConstructor = true;
541e5dd7070Spatrick     data().NeedOverloadResolutionForMoveConstructor = true;
542e5dd7070Spatrick     data().NeedOverloadResolutionForDestructor = true;
543e5dd7070Spatrick   }
544e5dd7070Spatrick 
545e5dd7070Spatrick   // C++2a [dcl.constexpr]p4:
546e5dd7070Spatrick   //   The definition of a constexpr destructor [shall] satisfy the
547e5dd7070Spatrick   //   following requirement:
548e5dd7070Spatrick   //   -- for every subobject of class type or (possibly multi-dimensional)
549e5dd7070Spatrick   //      array thereof, that class type shall have a constexpr destructor
550e5dd7070Spatrick   if (!Subobj->hasConstexprDestructor())
551e5dd7070Spatrick     data().DefaultedDestructorIsConstexpr = false;
552a9ac8606Spatrick 
553a9ac8606Spatrick   // C++20 [temp.param]p7:
554a9ac8606Spatrick   //   A structural type is [...] a literal class type [for which] the types
555a9ac8606Spatrick   //   of all base classes and non-static data members are structural types or
556a9ac8606Spatrick   //   (possibly multi-dimensional) array thereof
557a9ac8606Spatrick   if (!Subobj->data().StructuralIfLiteral)
558a9ac8606Spatrick     data().StructuralIfLiteral = false;
559e5dd7070Spatrick }
560e5dd7070Spatrick 
hasConstexprDestructor() const561e5dd7070Spatrick bool CXXRecordDecl::hasConstexprDestructor() const {
562e5dd7070Spatrick   auto *Dtor = getDestructor();
563e5dd7070Spatrick   return Dtor ? Dtor->isConstexpr() : defaultedDestructorIsConstexpr();
564e5dd7070Spatrick }
565e5dd7070Spatrick 
hasAnyDependentBases() const566e5dd7070Spatrick bool CXXRecordDecl::hasAnyDependentBases() const {
567e5dd7070Spatrick   if (!isDependentContext())
568e5dd7070Spatrick     return false;
569e5dd7070Spatrick 
570e5dd7070Spatrick   return !forallBases([](const CXXRecordDecl *) { return true; });
571e5dd7070Spatrick }
572e5dd7070Spatrick 
isTriviallyCopyable() const573e5dd7070Spatrick bool CXXRecordDecl::isTriviallyCopyable() const {
574e5dd7070Spatrick   // C++0x [class]p5:
575e5dd7070Spatrick   //   A trivially copyable class is a class that:
576e5dd7070Spatrick   //   -- has no non-trivial copy constructors,
577e5dd7070Spatrick   if (hasNonTrivialCopyConstructor()) return false;
578e5dd7070Spatrick   //   -- has no non-trivial move constructors,
579e5dd7070Spatrick   if (hasNonTrivialMoveConstructor()) return false;
580e5dd7070Spatrick   //   -- has no non-trivial copy assignment operators,
581e5dd7070Spatrick   if (hasNonTrivialCopyAssignment()) return false;
582e5dd7070Spatrick   //   -- has no non-trivial move assignment operators, and
583e5dd7070Spatrick   if (hasNonTrivialMoveAssignment()) return false;
584e5dd7070Spatrick   //   -- has a trivial destructor.
585e5dd7070Spatrick   if (!hasTrivialDestructor()) return false;
586e5dd7070Spatrick 
587e5dd7070Spatrick   return true;
588e5dd7070Spatrick }
589e5dd7070Spatrick 
markedVirtualFunctionPure()590e5dd7070Spatrick void CXXRecordDecl::markedVirtualFunctionPure() {
591e5dd7070Spatrick   // C++ [class.abstract]p2:
592e5dd7070Spatrick   //   A class is abstract if it has at least one pure virtual function.
593e5dd7070Spatrick   data().Abstract = true;
594e5dd7070Spatrick }
595e5dd7070Spatrick 
hasSubobjectAtOffsetZeroOfEmptyBaseType(ASTContext & Ctx,const CXXRecordDecl * XFirst)596e5dd7070Spatrick bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
597e5dd7070Spatrick     ASTContext &Ctx, const CXXRecordDecl *XFirst) {
598e5dd7070Spatrick   if (!getNumBases())
599e5dd7070Spatrick     return false;
600e5dd7070Spatrick 
601e5dd7070Spatrick   llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
602e5dd7070Spatrick   llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
603e5dd7070Spatrick   SmallVector<const CXXRecordDecl*, 8> WorkList;
604e5dd7070Spatrick 
605e5dd7070Spatrick   // Visit a type that we have determined is an element of M(S).
606e5dd7070Spatrick   auto Visit = [&](const CXXRecordDecl *RD) -> bool {
607e5dd7070Spatrick     RD = RD->getCanonicalDecl();
608e5dd7070Spatrick 
609e5dd7070Spatrick     // C++2a [class]p8:
610e5dd7070Spatrick     //   A class S is a standard-layout class if it [...] has no element of the
611e5dd7070Spatrick     //   set M(S) of types as a base class.
612e5dd7070Spatrick     //
613e5dd7070Spatrick     // If we find a subobject of an empty type, it might also be a base class,
614e5dd7070Spatrick     // so we'll need to walk the base classes to check.
615e5dd7070Spatrick     if (!RD->data().HasBasesWithFields) {
616e5dd7070Spatrick       // Walk the bases the first time, stopping if we find the type. Build a
617e5dd7070Spatrick       // set of them so we don't need to walk them again.
618e5dd7070Spatrick       if (Bases.empty()) {
619e5dd7070Spatrick         bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
620e5dd7070Spatrick           Base = Base->getCanonicalDecl();
621e5dd7070Spatrick           if (RD == Base)
622e5dd7070Spatrick             return false;
623e5dd7070Spatrick           Bases.insert(Base);
624e5dd7070Spatrick           return true;
625e5dd7070Spatrick         });
626e5dd7070Spatrick         if (RDIsBase)
627e5dd7070Spatrick           return true;
628e5dd7070Spatrick       } else {
629e5dd7070Spatrick         if (Bases.count(RD))
630e5dd7070Spatrick           return true;
631e5dd7070Spatrick       }
632e5dd7070Spatrick     }
633e5dd7070Spatrick 
634e5dd7070Spatrick     if (M.insert(RD).second)
635e5dd7070Spatrick       WorkList.push_back(RD);
636e5dd7070Spatrick     return false;
637e5dd7070Spatrick   };
638e5dd7070Spatrick 
639e5dd7070Spatrick   if (Visit(XFirst))
640e5dd7070Spatrick     return true;
641e5dd7070Spatrick 
642e5dd7070Spatrick   while (!WorkList.empty()) {
643e5dd7070Spatrick     const CXXRecordDecl *X = WorkList.pop_back_val();
644e5dd7070Spatrick 
645e5dd7070Spatrick     // FIXME: We don't check the bases of X. That matches the standard, but
646e5dd7070Spatrick     // that sure looks like a wording bug.
647e5dd7070Spatrick 
648e5dd7070Spatrick     //   -- If X is a non-union class type with a non-static data member
649e5dd7070Spatrick     //      [recurse to each field] that is either of zero size or is the
650e5dd7070Spatrick     //      first non-static data member of X
651e5dd7070Spatrick     //   -- If X is a union type, [recurse to union members]
652e5dd7070Spatrick     bool IsFirstField = true;
653e5dd7070Spatrick     for (auto *FD : X->fields()) {
654e5dd7070Spatrick       // FIXME: Should we really care about the type of the first non-static
655e5dd7070Spatrick       // data member of a non-union if there are preceding unnamed bit-fields?
656e5dd7070Spatrick       if (FD->isUnnamedBitfield())
657e5dd7070Spatrick         continue;
658e5dd7070Spatrick 
659e5dd7070Spatrick       if (!IsFirstField && !FD->isZeroSize(Ctx))
660e5dd7070Spatrick         continue;
661e5dd7070Spatrick 
662e5dd7070Spatrick       //   -- If X is n array type, [visit the element type]
663e5dd7070Spatrick       QualType T = Ctx.getBaseElementType(FD->getType());
664e5dd7070Spatrick       if (auto *RD = T->getAsCXXRecordDecl())
665e5dd7070Spatrick         if (Visit(RD))
666e5dd7070Spatrick           return true;
667e5dd7070Spatrick 
668e5dd7070Spatrick       if (!X->isUnion())
669e5dd7070Spatrick         IsFirstField = false;
670e5dd7070Spatrick     }
671e5dd7070Spatrick   }
672e5dd7070Spatrick 
673e5dd7070Spatrick   return false;
674e5dd7070Spatrick }
675e5dd7070Spatrick 
lambdaIsDefaultConstructibleAndAssignable() const676e5dd7070Spatrick bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
677e5dd7070Spatrick   assert(isLambda() && "not a lambda");
678e5dd7070Spatrick 
679e5dd7070Spatrick   // C++2a [expr.prim.lambda.capture]p11:
680e5dd7070Spatrick   //   The closure type associated with a lambda-expression has no default
681e5dd7070Spatrick   //   constructor if the lambda-expression has a lambda-capture and a
682e5dd7070Spatrick   //   defaulted default constructor otherwise. It has a deleted copy
683e5dd7070Spatrick   //   assignment operator if the lambda-expression has a lambda-capture and
684e5dd7070Spatrick   //   defaulted copy and move assignment operators otherwise.
685e5dd7070Spatrick   //
686e5dd7070Spatrick   // C++17 [expr.prim.lambda]p21:
687e5dd7070Spatrick   //   The closure type associated with a lambda-expression has no default
688e5dd7070Spatrick   //   constructor and a deleted copy assignment operator.
689ec727ea7Spatrick   if (getLambdaCaptureDefault() != LCD_None || capture_size() != 0)
690e5dd7070Spatrick     return false;
691ec727ea7Spatrick   return getASTContext().getLangOpts().CPlusPlus20;
692e5dd7070Spatrick }
693e5dd7070Spatrick 
addedMember(Decl * D)694e5dd7070Spatrick void CXXRecordDecl::addedMember(Decl *D) {
695e5dd7070Spatrick   if (!D->isImplicit() &&
696e5dd7070Spatrick       !isa<FieldDecl>(D) &&
697e5dd7070Spatrick       !isa<IndirectFieldDecl>(D) &&
698e5dd7070Spatrick       (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
699e5dd7070Spatrick         cast<TagDecl>(D)->getTagKind() == TTK_Interface))
700e5dd7070Spatrick     data().HasOnlyCMembers = false;
701e5dd7070Spatrick 
702e5dd7070Spatrick   // Ignore friends and invalid declarations.
703e5dd7070Spatrick   if (D->getFriendObjectKind() || D->isInvalidDecl())
704e5dd7070Spatrick     return;
705e5dd7070Spatrick 
706e5dd7070Spatrick   auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
707e5dd7070Spatrick   if (FunTmpl)
708e5dd7070Spatrick     D = FunTmpl->getTemplatedDecl();
709e5dd7070Spatrick 
710e5dd7070Spatrick   // FIXME: Pass NamedDecl* to addedMember?
711e5dd7070Spatrick   Decl *DUnderlying = D;
712e5dd7070Spatrick   if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
713e5dd7070Spatrick     DUnderlying = ND->getUnderlyingDecl();
714e5dd7070Spatrick     if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
715e5dd7070Spatrick       DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
716e5dd7070Spatrick   }
717e5dd7070Spatrick 
718e5dd7070Spatrick   if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
719e5dd7070Spatrick     if (Method->isVirtual()) {
720e5dd7070Spatrick       // C++ [dcl.init.aggr]p1:
721e5dd7070Spatrick       //   An aggregate is an array or a class with [...] no virtual functions.
722e5dd7070Spatrick       data().Aggregate = false;
723e5dd7070Spatrick 
724e5dd7070Spatrick       // C++ [class]p4:
725e5dd7070Spatrick       //   A POD-struct is an aggregate class...
726e5dd7070Spatrick       data().PlainOldData = false;
727e5dd7070Spatrick 
728e5dd7070Spatrick       // C++14 [meta.unary.prop]p4:
729e5dd7070Spatrick       //   T is a class type [...] with [...] no virtual member functions...
730e5dd7070Spatrick       data().Empty = false;
731e5dd7070Spatrick 
732e5dd7070Spatrick       // C++ [class.virtual]p1:
733e5dd7070Spatrick       //   A class that declares or inherits a virtual function is called a
734e5dd7070Spatrick       //   polymorphic class.
735e5dd7070Spatrick       data().Polymorphic = true;
736e5dd7070Spatrick 
737e5dd7070Spatrick       // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
738e5dd7070Spatrick       //   A [default constructor, copy/move constructor, or copy/move
739e5dd7070Spatrick       //   assignment operator for a class X] is trivial [...] if:
740e5dd7070Spatrick       //    -- class X has no virtual functions [...]
741e5dd7070Spatrick       data().HasTrivialSpecialMembers &= SMF_Destructor;
742e5dd7070Spatrick       data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
743e5dd7070Spatrick 
744e5dd7070Spatrick       // C++0x [class]p7:
745e5dd7070Spatrick       //   A standard-layout class is a class that: [...]
746e5dd7070Spatrick       //    -- has no virtual functions
747e5dd7070Spatrick       data().IsStandardLayout = false;
748e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
749e5dd7070Spatrick     }
750e5dd7070Spatrick   }
751e5dd7070Spatrick 
752e5dd7070Spatrick   // Notify the listener if an implicit member was added after the definition
753e5dd7070Spatrick   // was completed.
754e5dd7070Spatrick   if (!isBeingDefined() && D->isImplicit())
755e5dd7070Spatrick     if (ASTMutationListener *L = getASTMutationListener())
756e5dd7070Spatrick       L->AddedCXXImplicitMember(data().Definition, D);
757e5dd7070Spatrick 
758e5dd7070Spatrick   // The kind of special member this declaration is, if any.
759e5dd7070Spatrick   unsigned SMKind = 0;
760e5dd7070Spatrick 
761e5dd7070Spatrick   // Handle constructors.
762e5dd7070Spatrick   if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
763e5dd7070Spatrick     if (Constructor->isInheritingConstructor()) {
764e5dd7070Spatrick       // Ignore constructor shadow declarations. They are lazily created and
765e5dd7070Spatrick       // so shouldn't affect any properties of the class.
766e5dd7070Spatrick     } else {
767e5dd7070Spatrick       if (!Constructor->isImplicit()) {
768e5dd7070Spatrick         // Note that we have a user-declared constructor.
769e5dd7070Spatrick         data().UserDeclaredConstructor = true;
770e5dd7070Spatrick 
771*12c85518Srobert         const TargetInfo &TI = getASTContext().getTargetInfo();
772*12c85518Srobert         if ((!Constructor->isDeleted() && !Constructor->isDefaulted()) ||
773*12c85518Srobert             !TI.areDefaultedSMFStillPOD(getLangOpts())) {
774e5dd7070Spatrick           // C++ [class]p4:
775e5dd7070Spatrick           //   A POD-struct is an aggregate class [...]
776e5dd7070Spatrick           // Since the POD bit is meant to be C++03 POD-ness, clear it even if
777e5dd7070Spatrick           // the type is technically an aggregate in C++0x since it wouldn't be
778e5dd7070Spatrick           // in 03.
779e5dd7070Spatrick           data().PlainOldData = false;
780e5dd7070Spatrick         }
781*12c85518Srobert       }
782e5dd7070Spatrick 
783e5dd7070Spatrick       if (Constructor->isDefaultConstructor()) {
784e5dd7070Spatrick         SMKind |= SMF_DefaultConstructor;
785e5dd7070Spatrick 
786e5dd7070Spatrick         if (Constructor->isUserProvided())
787e5dd7070Spatrick           data().UserProvidedDefaultConstructor = true;
788e5dd7070Spatrick         if (Constructor->isConstexpr())
789e5dd7070Spatrick           data().HasConstexprDefaultConstructor = true;
790e5dd7070Spatrick         if (Constructor->isDefaulted())
791e5dd7070Spatrick           data().HasDefaultedDefaultConstructor = true;
792e5dd7070Spatrick       }
793e5dd7070Spatrick 
794e5dd7070Spatrick       if (!FunTmpl) {
795e5dd7070Spatrick         unsigned Quals;
796e5dd7070Spatrick         if (Constructor->isCopyConstructor(Quals)) {
797e5dd7070Spatrick           SMKind |= SMF_CopyConstructor;
798e5dd7070Spatrick 
799e5dd7070Spatrick           if (Quals & Qualifiers::Const)
800e5dd7070Spatrick             data().HasDeclaredCopyConstructorWithConstParam = true;
801e5dd7070Spatrick         } else if (Constructor->isMoveConstructor())
802e5dd7070Spatrick           SMKind |= SMF_MoveConstructor;
803e5dd7070Spatrick       }
804e5dd7070Spatrick 
805e5dd7070Spatrick       // C++11 [dcl.init.aggr]p1: DR1518
806e5dd7070Spatrick       //   An aggregate is an array or a class with no user-provided [or]
807e5dd7070Spatrick       //   explicit [...] constructors
808e5dd7070Spatrick       // C++20 [dcl.init.aggr]p1:
809e5dd7070Spatrick       //   An aggregate is an array or a class with no user-declared [...]
810e5dd7070Spatrick       //   constructors
811ec727ea7Spatrick       if (getASTContext().getLangOpts().CPlusPlus20
812e5dd7070Spatrick               ? !Constructor->isImplicit()
813e5dd7070Spatrick               : (Constructor->isUserProvided() || Constructor->isExplicit()))
814e5dd7070Spatrick         data().Aggregate = false;
815e5dd7070Spatrick     }
816e5dd7070Spatrick   }
817e5dd7070Spatrick 
818e5dd7070Spatrick   // Handle constructors, including those inherited from base classes.
819e5dd7070Spatrick   if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
820e5dd7070Spatrick     // Record if we see any constexpr constructors which are neither copy
821e5dd7070Spatrick     // nor move constructors.
822e5dd7070Spatrick     // C++1z [basic.types]p10:
823e5dd7070Spatrick     //   [...] has at least one constexpr constructor or constructor template
824e5dd7070Spatrick     //   (possibly inherited from a base class) that is not a copy or move
825e5dd7070Spatrick     //   constructor [...]
826e5dd7070Spatrick     if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
827e5dd7070Spatrick       data().HasConstexprNonCopyMoveConstructor = true;
828a9ac8606Spatrick     if (!isa<CXXConstructorDecl>(D) && Constructor->isDefaultConstructor())
829a9ac8606Spatrick       data().HasInheritedDefaultConstructor = true;
830e5dd7070Spatrick   }
831e5dd7070Spatrick 
832e5dd7070Spatrick   // Handle member functions.
833e5dd7070Spatrick   if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
834*12c85518Srobert     if (isa<CXXDestructorDecl>(D))
835*12c85518Srobert       SMKind |= SMF_Destructor;
836*12c85518Srobert 
837e5dd7070Spatrick     if (Method->isCopyAssignmentOperator()) {
838e5dd7070Spatrick       SMKind |= SMF_CopyAssignment;
839e5dd7070Spatrick 
840e5dd7070Spatrick       const auto *ParamTy =
841e5dd7070Spatrick           Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
842e5dd7070Spatrick       if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
843e5dd7070Spatrick         data().HasDeclaredCopyAssignmentWithConstParam = true;
844e5dd7070Spatrick     }
845e5dd7070Spatrick 
846e5dd7070Spatrick     if (Method->isMoveAssignmentOperator())
847e5dd7070Spatrick       SMKind |= SMF_MoveAssignment;
848e5dd7070Spatrick 
849e5dd7070Spatrick     // Keep the list of conversion functions up-to-date.
850e5dd7070Spatrick     if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
851e5dd7070Spatrick       // FIXME: We use the 'unsafe' accessor for the access specifier here,
852e5dd7070Spatrick       // because Sema may not have set it yet. That's really just a misdesign
853e5dd7070Spatrick       // in Sema. However, LLDB *will* have set the access specifier correctly,
854e5dd7070Spatrick       // and adds declarations after the class is technically completed,
855e5dd7070Spatrick       // so completeDefinition()'s overriding of the access specifiers doesn't
856e5dd7070Spatrick       // work.
857e5dd7070Spatrick       AccessSpecifier AS = Conversion->getAccessUnsafe();
858e5dd7070Spatrick 
859e5dd7070Spatrick       if (Conversion->getPrimaryTemplate()) {
860e5dd7070Spatrick         // We don't record specializations.
861e5dd7070Spatrick       } else {
862e5dd7070Spatrick         ASTContext &Ctx = getASTContext();
863e5dd7070Spatrick         ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
864e5dd7070Spatrick         NamedDecl *Primary =
865e5dd7070Spatrick             FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
866e5dd7070Spatrick         if (Primary->getPreviousDecl())
867e5dd7070Spatrick           Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
868e5dd7070Spatrick                               Primary, AS);
869e5dd7070Spatrick         else
870e5dd7070Spatrick           Conversions.addDecl(Ctx, Primary, AS);
871e5dd7070Spatrick       }
872e5dd7070Spatrick     }
873e5dd7070Spatrick 
874e5dd7070Spatrick     if (SMKind) {
875e5dd7070Spatrick       // If this is the first declaration of a special member, we no longer have
876e5dd7070Spatrick       // an implicit trivial special member.
877e5dd7070Spatrick       data().HasTrivialSpecialMembers &=
878e5dd7070Spatrick           data().DeclaredSpecialMembers | ~SMKind;
879e5dd7070Spatrick       data().HasTrivialSpecialMembersForCall &=
880e5dd7070Spatrick           data().DeclaredSpecialMembers | ~SMKind;
881e5dd7070Spatrick 
882e5dd7070Spatrick       // Note when we have declared a declared special member, and suppress the
883e5dd7070Spatrick       // implicit declaration of this special member.
884e5dd7070Spatrick       data().DeclaredSpecialMembers |= SMKind;
885e5dd7070Spatrick       if (!Method->isImplicit()) {
886e5dd7070Spatrick         data().UserDeclaredSpecialMembers |= SMKind;
887e5dd7070Spatrick 
888*12c85518Srobert         const TargetInfo &TI = getASTContext().getTargetInfo();
889*12c85518Srobert         if ((!Method->isDeleted() && !Method->isDefaulted() &&
890*12c85518Srobert              SMKind != SMF_MoveAssignment) ||
891*12c85518Srobert             !TI.areDefaultedSMFStillPOD(getLangOpts())) {
892e5dd7070Spatrick           // C++03 [class]p4:
893e5dd7070Spatrick           //   A POD-struct is an aggregate class that has [...] no user-defined
894e5dd7070Spatrick           //   copy assignment operator and no user-defined destructor.
895e5dd7070Spatrick           //
896e5dd7070Spatrick           // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
897e5dd7070Spatrick           // aggregates could not have any constructors, clear it even for an
898e5dd7070Spatrick           // explicitly defaulted or deleted constructor.
899*12c85518Srobert           // type is technically an aggregate in C++0x since it wouldn't be in
900*12c85518Srobert           // 03.
901e5dd7070Spatrick           //
902*12c85518Srobert           // Also, a user-declared move assignment operator makes a class
903*12c85518Srobert           // non-POD. This is an extension in C++03.
904e5dd7070Spatrick           data().PlainOldData = false;
905e5dd7070Spatrick         }
906e5dd7070Spatrick       }
907*12c85518Srobert       // When instantiating a class, we delay updating the destructor and
908*12c85518Srobert       // triviality properties of the class until selecting a destructor and
909*12c85518Srobert       // computing the eligibility of its special member functions. This is
910*12c85518Srobert       // because there might be function constraints that we need to evaluate
911*12c85518Srobert       // and compare later in the instantiation.
912*12c85518Srobert       if (!Method->isIneligibleOrNotSelected()) {
913*12c85518Srobert         addedEligibleSpecialMemberFunction(Method, SMKind);
914*12c85518Srobert       }
915*12c85518Srobert     }
916e5dd7070Spatrick 
917e5dd7070Spatrick     return;
918e5dd7070Spatrick   }
919e5dd7070Spatrick 
920e5dd7070Spatrick   // Handle non-static data members.
921e5dd7070Spatrick   if (const auto *Field = dyn_cast<FieldDecl>(D)) {
922e5dd7070Spatrick     ASTContext &Context = getASTContext();
923e5dd7070Spatrick 
924e5dd7070Spatrick     // C++2a [class]p7:
925e5dd7070Spatrick     //   A standard-layout class is a class that:
926e5dd7070Spatrick     //    [...]
927e5dd7070Spatrick     //    -- has all non-static data members and bit-fields in the class and
928e5dd7070Spatrick     //       its base classes first declared in the same class
929e5dd7070Spatrick     if (data().HasBasesWithFields)
930e5dd7070Spatrick       data().IsStandardLayout = false;
931e5dd7070Spatrick 
932e5dd7070Spatrick     // C++ [class.bit]p2:
933e5dd7070Spatrick     //   A declaration for a bit-field that omits the identifier declares an
934e5dd7070Spatrick     //   unnamed bit-field. Unnamed bit-fields are not members and cannot be
935e5dd7070Spatrick     //   initialized.
936e5dd7070Spatrick     if (Field->isUnnamedBitfield()) {
937e5dd7070Spatrick       // C++ [meta.unary.prop]p4: [LWG2358]
938e5dd7070Spatrick       //   T is a class type [...] with [...] no unnamed bit-fields of non-zero
939e5dd7070Spatrick       //   length
940e5dd7070Spatrick       if (data().Empty && !Field->isZeroLengthBitField(Context) &&
941e5dd7070Spatrick           Context.getLangOpts().getClangABICompat() >
942e5dd7070Spatrick               LangOptions::ClangABI::Ver6)
943e5dd7070Spatrick         data().Empty = false;
944e5dd7070Spatrick       return;
945e5dd7070Spatrick     }
946e5dd7070Spatrick 
947e5dd7070Spatrick     // C++11 [class]p7:
948e5dd7070Spatrick     //   A standard-layout class is a class that:
949e5dd7070Spatrick     //    -- either has no non-static data members in the most derived class
950e5dd7070Spatrick     //       [...] or has no base classes with non-static data members
951e5dd7070Spatrick     if (data().HasBasesWithNonStaticDataMembers)
952e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
953e5dd7070Spatrick 
954e5dd7070Spatrick     // C++ [dcl.init.aggr]p1:
955e5dd7070Spatrick     //   An aggregate is an array or a class (clause 9) with [...] no
956e5dd7070Spatrick     //   private or protected non-static data members (clause 11).
957e5dd7070Spatrick     //
958e5dd7070Spatrick     // A POD must be an aggregate.
959e5dd7070Spatrick     if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
960e5dd7070Spatrick       data().Aggregate = false;
961e5dd7070Spatrick       data().PlainOldData = false;
962a9ac8606Spatrick 
963a9ac8606Spatrick       // C++20 [temp.param]p7:
964a9ac8606Spatrick       //   A structural type is [...] a literal class type [for which] all
965a9ac8606Spatrick       //   non-static data members are public
966a9ac8606Spatrick       data().StructuralIfLiteral = false;
967e5dd7070Spatrick     }
968e5dd7070Spatrick 
969e5dd7070Spatrick     // Track whether this is the first field. We use this when checking
970e5dd7070Spatrick     // whether the class is standard-layout below.
971e5dd7070Spatrick     bool IsFirstField = !data().HasPrivateFields &&
972e5dd7070Spatrick                         !data().HasProtectedFields && !data().HasPublicFields;
973e5dd7070Spatrick 
974e5dd7070Spatrick     // C++0x [class]p7:
975e5dd7070Spatrick     //   A standard-layout class is a class that:
976e5dd7070Spatrick     //    [...]
977e5dd7070Spatrick     //    -- has the same access control for all non-static data members,
978e5dd7070Spatrick     switch (D->getAccess()) {
979e5dd7070Spatrick     case AS_private:    data().HasPrivateFields = true;   break;
980e5dd7070Spatrick     case AS_protected:  data().HasProtectedFields = true; break;
981e5dd7070Spatrick     case AS_public:     data().HasPublicFields = true;    break;
982e5dd7070Spatrick     case AS_none:       llvm_unreachable("Invalid access specifier");
983e5dd7070Spatrick     };
984e5dd7070Spatrick     if ((data().HasPrivateFields + data().HasProtectedFields +
985e5dd7070Spatrick          data().HasPublicFields) > 1) {
986e5dd7070Spatrick       data().IsStandardLayout = false;
987e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
988e5dd7070Spatrick     }
989e5dd7070Spatrick 
990e5dd7070Spatrick     // Keep track of the presence of mutable fields.
991a9ac8606Spatrick     if (Field->isMutable()) {
992e5dd7070Spatrick       data().HasMutableFields = true;
993e5dd7070Spatrick 
994a9ac8606Spatrick       // C++20 [temp.param]p7:
995a9ac8606Spatrick       //   A structural type is [...] a literal class type [for which] all
996a9ac8606Spatrick       //   non-static data members are public
997a9ac8606Spatrick       data().StructuralIfLiteral = false;
998a9ac8606Spatrick     }
999a9ac8606Spatrick 
1000e5dd7070Spatrick     // C++11 [class.union]p8, DR1460:
1001e5dd7070Spatrick     //   If X is a union, a non-static data member of X that is not an anonymous
1002e5dd7070Spatrick     //   union is a variant member of X.
1003e5dd7070Spatrick     if (isUnion() && !Field->isAnonymousStructOrUnion())
1004e5dd7070Spatrick       data().HasVariantMembers = true;
1005e5dd7070Spatrick 
1006e5dd7070Spatrick     // C++0x [class]p9:
1007e5dd7070Spatrick     //   A POD struct is a class that is both a trivial class and a
1008e5dd7070Spatrick     //   standard-layout class, and has no non-static data members of type
1009e5dd7070Spatrick     //   non-POD struct, non-POD union (or array of such types).
1010e5dd7070Spatrick     //
1011e5dd7070Spatrick     // Automatic Reference Counting: the presence of a member of Objective-C pointer type
1012e5dd7070Spatrick     // that does not explicitly have no lifetime makes the class a non-POD.
1013e5dd7070Spatrick     QualType T = Context.getBaseElementType(Field->getType());
1014e5dd7070Spatrick     if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
1015e5dd7070Spatrick       if (T.hasNonTrivialObjCLifetime()) {
1016e5dd7070Spatrick         // Objective-C Automatic Reference Counting:
1017e5dd7070Spatrick         //   If a class has a non-static data member of Objective-C pointer
1018e5dd7070Spatrick         //   type (or array thereof), it is a non-POD type and its
1019e5dd7070Spatrick         //   default constructor (if any), copy constructor, move constructor,
1020e5dd7070Spatrick         //   copy assignment operator, move assignment operator, and destructor are
1021e5dd7070Spatrick         //   non-trivial.
1022e5dd7070Spatrick         setHasObjectMember(true);
1023e5dd7070Spatrick         struct DefinitionData &Data = data();
1024e5dd7070Spatrick         Data.PlainOldData = false;
1025e5dd7070Spatrick         Data.HasTrivialSpecialMembers = 0;
1026e5dd7070Spatrick 
1027e5dd7070Spatrick         // __strong or __weak fields do not make special functions non-trivial
1028e5dd7070Spatrick         // for the purpose of calls.
1029e5dd7070Spatrick         Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
1030e5dd7070Spatrick         if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
1031e5dd7070Spatrick           data().HasTrivialSpecialMembersForCall = 0;
1032e5dd7070Spatrick 
1033e5dd7070Spatrick         // Structs with __weak fields should never be passed directly.
1034e5dd7070Spatrick         if (LT == Qualifiers::OCL_Weak)
1035e5dd7070Spatrick           setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
1036e5dd7070Spatrick 
1037e5dd7070Spatrick         Data.HasIrrelevantDestructor = false;
1038e5dd7070Spatrick 
1039e5dd7070Spatrick         if (isUnion()) {
1040e5dd7070Spatrick           data().DefaultedCopyConstructorIsDeleted = true;
1041e5dd7070Spatrick           data().DefaultedMoveConstructorIsDeleted = true;
1042ec727ea7Spatrick           data().DefaultedCopyAssignmentIsDeleted = true;
1043e5dd7070Spatrick           data().DefaultedMoveAssignmentIsDeleted = true;
1044e5dd7070Spatrick           data().DefaultedDestructorIsDeleted = true;
1045e5dd7070Spatrick           data().NeedOverloadResolutionForCopyConstructor = true;
1046e5dd7070Spatrick           data().NeedOverloadResolutionForMoveConstructor = true;
1047ec727ea7Spatrick           data().NeedOverloadResolutionForCopyAssignment = true;
1048e5dd7070Spatrick           data().NeedOverloadResolutionForMoveAssignment = true;
1049e5dd7070Spatrick           data().NeedOverloadResolutionForDestructor = true;
1050e5dd7070Spatrick         }
1051e5dd7070Spatrick       } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1052e5dd7070Spatrick         setHasObjectMember(true);
1053e5dd7070Spatrick       }
1054e5dd7070Spatrick     } else if (!T.isCXX98PODType(Context))
1055e5dd7070Spatrick       data().PlainOldData = false;
1056e5dd7070Spatrick 
1057e5dd7070Spatrick     if (T->isReferenceType()) {
1058e5dd7070Spatrick       if (!Field->hasInClassInitializer())
1059e5dd7070Spatrick         data().HasUninitializedReferenceMember = true;
1060e5dd7070Spatrick 
1061e5dd7070Spatrick       // C++0x [class]p7:
1062e5dd7070Spatrick       //   A standard-layout class is a class that:
1063e5dd7070Spatrick       //    -- has no non-static data members of type [...] reference,
1064e5dd7070Spatrick       data().IsStandardLayout = false;
1065e5dd7070Spatrick       data().IsCXX11StandardLayout = false;
1066e5dd7070Spatrick 
1067e5dd7070Spatrick       // C++1z [class.copy.ctor]p10:
1068e5dd7070Spatrick       //   A defaulted copy constructor for a class X is defined as deleted if X has:
1069e5dd7070Spatrick       //    -- a non-static data member of rvalue reference type
1070e5dd7070Spatrick       if (T->isRValueReferenceType())
1071e5dd7070Spatrick         data().DefaultedCopyConstructorIsDeleted = true;
1072e5dd7070Spatrick     }
1073e5dd7070Spatrick 
1074e5dd7070Spatrick     if (!Field->hasInClassInitializer() && !Field->isMutable()) {
1075e5dd7070Spatrick       if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
1076e5dd7070Spatrick         if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
1077e5dd7070Spatrick           data().HasUninitializedFields = true;
1078e5dd7070Spatrick       } else {
1079e5dd7070Spatrick         data().HasUninitializedFields = true;
1080e5dd7070Spatrick       }
1081e5dd7070Spatrick     }
1082e5dd7070Spatrick 
1083e5dd7070Spatrick     // Record if this field is the first non-literal or volatile field or base.
1084e5dd7070Spatrick     if (!T->isLiteralType(Context) || T.isVolatileQualified())
1085e5dd7070Spatrick       data().HasNonLiteralTypeFieldsOrBases = true;
1086e5dd7070Spatrick 
1087e5dd7070Spatrick     if (Field->hasInClassInitializer() ||
1088e5dd7070Spatrick         (Field->isAnonymousStructOrUnion() &&
1089e5dd7070Spatrick          Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
1090e5dd7070Spatrick       data().HasInClassInitializer = true;
1091e5dd7070Spatrick 
1092e5dd7070Spatrick       // C++11 [class]p5:
1093e5dd7070Spatrick       //   A default constructor is trivial if [...] no non-static data member
1094e5dd7070Spatrick       //   of its class has a brace-or-equal-initializer.
1095e5dd7070Spatrick       data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
1096e5dd7070Spatrick 
1097e5dd7070Spatrick       // C++11 [dcl.init.aggr]p1:
1098e5dd7070Spatrick       //   An aggregate is a [...] class with [...] no
1099e5dd7070Spatrick       //   brace-or-equal-initializers for non-static data members.
1100e5dd7070Spatrick       //
1101e5dd7070Spatrick       // This rule was removed in C++14.
1102e5dd7070Spatrick       if (!getASTContext().getLangOpts().CPlusPlus14)
1103e5dd7070Spatrick         data().Aggregate = false;
1104e5dd7070Spatrick 
1105e5dd7070Spatrick       // C++11 [class]p10:
1106e5dd7070Spatrick       //   A POD struct is [...] a trivial class.
1107e5dd7070Spatrick       data().PlainOldData = false;
1108e5dd7070Spatrick     }
1109e5dd7070Spatrick 
1110e5dd7070Spatrick     // C++11 [class.copy]p23:
1111e5dd7070Spatrick     //   A defaulted copy/move assignment operator for a class X is defined
1112e5dd7070Spatrick     //   as deleted if X has:
1113e5dd7070Spatrick     //    -- a non-static data member of reference type
1114ec727ea7Spatrick     if (T->isReferenceType()) {
1115ec727ea7Spatrick       data().DefaultedCopyAssignmentIsDeleted = true;
1116e5dd7070Spatrick       data().DefaultedMoveAssignmentIsDeleted = true;
1117ec727ea7Spatrick     }
1118e5dd7070Spatrick 
1119e5dd7070Spatrick     // Bitfields of length 0 are also zero-sized, but we already bailed out for
1120e5dd7070Spatrick     // those because they are always unnamed.
1121e5dd7070Spatrick     bool IsZeroSize = Field->isZeroSize(Context);
1122e5dd7070Spatrick 
1123e5dd7070Spatrick     if (const auto *RecordTy = T->getAs<RecordType>()) {
1124e5dd7070Spatrick       auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
1125e5dd7070Spatrick       if (FieldRec->getDefinition()) {
1126e5dd7070Spatrick         addedClassSubobject(FieldRec);
1127e5dd7070Spatrick 
1128e5dd7070Spatrick         // We may need to perform overload resolution to determine whether a
1129e5dd7070Spatrick         // field can be moved if it's const or volatile qualified.
1130e5dd7070Spatrick         if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
1131e5dd7070Spatrick           // We need to care about 'const' for the copy constructor because an
1132e5dd7070Spatrick           // implicit copy constructor might be declared with a non-const
1133e5dd7070Spatrick           // parameter.
1134e5dd7070Spatrick           data().NeedOverloadResolutionForCopyConstructor = true;
1135e5dd7070Spatrick           data().NeedOverloadResolutionForMoveConstructor = true;
1136ec727ea7Spatrick           data().NeedOverloadResolutionForCopyAssignment = true;
1137e5dd7070Spatrick           data().NeedOverloadResolutionForMoveAssignment = true;
1138e5dd7070Spatrick         }
1139e5dd7070Spatrick 
1140e5dd7070Spatrick         // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1141e5dd7070Spatrick         //   A defaulted [special member] for a class X is defined as
1142e5dd7070Spatrick         //   deleted if:
1143e5dd7070Spatrick         //    -- X is a union-like class that has a variant member with a
1144e5dd7070Spatrick         //       non-trivial [corresponding special member]
1145e5dd7070Spatrick         if (isUnion()) {
1146e5dd7070Spatrick           if (FieldRec->hasNonTrivialCopyConstructor())
1147e5dd7070Spatrick             data().DefaultedCopyConstructorIsDeleted = true;
1148e5dd7070Spatrick           if (FieldRec->hasNonTrivialMoveConstructor())
1149e5dd7070Spatrick             data().DefaultedMoveConstructorIsDeleted = true;
1150ec727ea7Spatrick           if (FieldRec->hasNonTrivialCopyAssignment())
1151ec727ea7Spatrick             data().DefaultedCopyAssignmentIsDeleted = true;
1152e5dd7070Spatrick           if (FieldRec->hasNonTrivialMoveAssignment())
1153e5dd7070Spatrick             data().DefaultedMoveAssignmentIsDeleted = true;
1154e5dd7070Spatrick           if (FieldRec->hasNonTrivialDestructor())
1155e5dd7070Spatrick             data().DefaultedDestructorIsDeleted = true;
1156e5dd7070Spatrick         }
1157e5dd7070Spatrick 
1158e5dd7070Spatrick         // For an anonymous union member, our overload resolution will perform
1159e5dd7070Spatrick         // overload resolution for its members.
1160e5dd7070Spatrick         if (Field->isAnonymousStructOrUnion()) {
1161e5dd7070Spatrick           data().NeedOverloadResolutionForCopyConstructor |=
1162e5dd7070Spatrick               FieldRec->data().NeedOverloadResolutionForCopyConstructor;
1163e5dd7070Spatrick           data().NeedOverloadResolutionForMoveConstructor |=
1164e5dd7070Spatrick               FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1165ec727ea7Spatrick           data().NeedOverloadResolutionForCopyAssignment |=
1166ec727ea7Spatrick               FieldRec->data().NeedOverloadResolutionForCopyAssignment;
1167e5dd7070Spatrick           data().NeedOverloadResolutionForMoveAssignment |=
1168e5dd7070Spatrick               FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1169e5dd7070Spatrick           data().NeedOverloadResolutionForDestructor |=
1170e5dd7070Spatrick               FieldRec->data().NeedOverloadResolutionForDestructor;
1171e5dd7070Spatrick         }
1172e5dd7070Spatrick 
1173e5dd7070Spatrick         // C++0x [class.ctor]p5:
1174e5dd7070Spatrick         //   A default constructor is trivial [...] if:
1175e5dd7070Spatrick         //    -- for all the non-static data members of its class that are of
1176e5dd7070Spatrick         //       class type (or array thereof), each such class has a trivial
1177e5dd7070Spatrick         //       default constructor.
1178e5dd7070Spatrick         if (!FieldRec->hasTrivialDefaultConstructor())
1179e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
1180e5dd7070Spatrick 
1181e5dd7070Spatrick         // C++0x [class.copy]p13:
1182e5dd7070Spatrick         //   A copy/move constructor for class X is trivial if [...]
1183e5dd7070Spatrick         //    [...]
1184e5dd7070Spatrick         //    -- for each non-static data member of X that is of class type (or
1185e5dd7070Spatrick         //       an array thereof), the constructor selected to copy/move that
1186e5dd7070Spatrick         //       member is trivial;
1187e5dd7070Spatrick         if (!FieldRec->hasTrivialCopyConstructor())
1188e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
1189e5dd7070Spatrick 
1190e5dd7070Spatrick         if (!FieldRec->hasTrivialCopyConstructorForCall())
1191e5dd7070Spatrick           data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1192e5dd7070Spatrick 
1193e5dd7070Spatrick         // If the field doesn't have a simple move constructor, we'll eagerly
1194e5dd7070Spatrick         // declare the move constructor for this class and we'll decide whether
1195e5dd7070Spatrick         // it's trivial then.
1196e5dd7070Spatrick         if (!FieldRec->hasTrivialMoveConstructor())
1197e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
1198e5dd7070Spatrick 
1199e5dd7070Spatrick         if (!FieldRec->hasTrivialMoveConstructorForCall())
1200e5dd7070Spatrick           data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1201e5dd7070Spatrick 
1202e5dd7070Spatrick         // C++0x [class.copy]p27:
1203e5dd7070Spatrick         //   A copy/move assignment operator for class X is trivial if [...]
1204e5dd7070Spatrick         //    [...]
1205e5dd7070Spatrick         //    -- for each non-static data member of X that is of class type (or
1206e5dd7070Spatrick         //       an array thereof), the assignment operator selected to
1207e5dd7070Spatrick         //       copy/move that member is trivial;
1208e5dd7070Spatrick         if (!FieldRec->hasTrivialCopyAssignment())
1209e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
1210e5dd7070Spatrick         // If the field doesn't have a simple move assignment, we'll eagerly
1211e5dd7070Spatrick         // declare the move assignment for this class and we'll decide whether
1212e5dd7070Spatrick         // it's trivial then.
1213e5dd7070Spatrick         if (!FieldRec->hasTrivialMoveAssignment())
1214e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
1215e5dd7070Spatrick 
1216e5dd7070Spatrick         if (!FieldRec->hasTrivialDestructor())
1217e5dd7070Spatrick           data().HasTrivialSpecialMembers &= ~SMF_Destructor;
1218e5dd7070Spatrick         if (!FieldRec->hasTrivialDestructorForCall())
1219e5dd7070Spatrick           data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
1220e5dd7070Spatrick         if (!FieldRec->hasIrrelevantDestructor())
1221e5dd7070Spatrick           data().HasIrrelevantDestructor = false;
1222a9ac8606Spatrick         if (FieldRec->isAnyDestructorNoReturn())
1223a9ac8606Spatrick           data().IsAnyDestructorNoReturn = true;
1224e5dd7070Spatrick         if (FieldRec->hasObjectMember())
1225e5dd7070Spatrick           setHasObjectMember(true);
1226e5dd7070Spatrick         if (FieldRec->hasVolatileMember())
1227e5dd7070Spatrick           setHasVolatileMember(true);
1228e5dd7070Spatrick         if (FieldRec->getArgPassingRestrictions() ==
1229e5dd7070Spatrick             RecordDecl::APK_CanNeverPassInRegs)
1230e5dd7070Spatrick           setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
1231e5dd7070Spatrick 
1232e5dd7070Spatrick         // C++0x [class]p7:
1233e5dd7070Spatrick         //   A standard-layout class is a class that:
1234e5dd7070Spatrick         //    -- has no non-static data members of type non-standard-layout
1235e5dd7070Spatrick         //       class (or array of such types) [...]
1236e5dd7070Spatrick         if (!FieldRec->isStandardLayout())
1237e5dd7070Spatrick           data().IsStandardLayout = false;
1238e5dd7070Spatrick         if (!FieldRec->isCXX11StandardLayout())
1239e5dd7070Spatrick           data().IsCXX11StandardLayout = false;
1240e5dd7070Spatrick 
1241e5dd7070Spatrick         // C++2a [class]p7:
1242e5dd7070Spatrick         //   A standard-layout class is a class that:
1243e5dd7070Spatrick         //    [...]
1244e5dd7070Spatrick         //    -- has no element of the set M(S) of types as a base class.
1245e5dd7070Spatrick         if (data().IsStandardLayout &&
1246e5dd7070Spatrick             (isUnion() || IsFirstField || IsZeroSize) &&
1247e5dd7070Spatrick             hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1248e5dd7070Spatrick           data().IsStandardLayout = false;
1249e5dd7070Spatrick 
1250e5dd7070Spatrick         // C++11 [class]p7:
1251e5dd7070Spatrick         //   A standard-layout class is a class that:
1252e5dd7070Spatrick         //    -- has no base classes of the same type as the first non-static
1253e5dd7070Spatrick         //       data member
1254e5dd7070Spatrick         if (data().IsCXX11StandardLayout && IsFirstField) {
1255e5dd7070Spatrick           // FIXME: We should check all base classes here, not just direct
1256e5dd7070Spatrick           // base classes.
1257e5dd7070Spatrick           for (const auto &BI : bases()) {
1258e5dd7070Spatrick             if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
1259e5dd7070Spatrick               data().IsCXX11StandardLayout = false;
1260e5dd7070Spatrick               break;
1261e5dd7070Spatrick             }
1262e5dd7070Spatrick           }
1263e5dd7070Spatrick         }
1264e5dd7070Spatrick 
1265e5dd7070Spatrick         // Keep track of the presence of mutable fields.
1266ec727ea7Spatrick         if (FieldRec->hasMutableFields())
1267e5dd7070Spatrick           data().HasMutableFields = true;
1268ec727ea7Spatrick 
1269ec727ea7Spatrick         if (Field->isMutable()) {
1270ec727ea7Spatrick           // Our copy constructor/assignment might call something other than
1271ec727ea7Spatrick           // the subobject's copy constructor/assignment if it's mutable and of
1272ec727ea7Spatrick           // class type.
1273e5dd7070Spatrick           data().NeedOverloadResolutionForCopyConstructor = true;
1274ec727ea7Spatrick           data().NeedOverloadResolutionForCopyAssignment = true;
1275e5dd7070Spatrick         }
1276e5dd7070Spatrick 
1277e5dd7070Spatrick         // C++11 [class.copy]p13:
1278e5dd7070Spatrick         //   If the implicitly-defined constructor would satisfy the
1279e5dd7070Spatrick         //   requirements of a constexpr constructor, the implicitly-defined
1280e5dd7070Spatrick         //   constructor is constexpr.
1281e5dd7070Spatrick         // C++11 [dcl.constexpr]p4:
1282e5dd7070Spatrick         //    -- every constructor involved in initializing non-static data
1283e5dd7070Spatrick         //       members [...] shall be a constexpr constructor
1284e5dd7070Spatrick         if (!Field->hasInClassInitializer() &&
1285e5dd7070Spatrick             !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
1286e5dd7070Spatrick           // The standard requires any in-class initializer to be a constant
1287e5dd7070Spatrick           // expression. We consider this to be a defect.
1288e5dd7070Spatrick           data().DefaultedDefaultConstructorIsConstexpr = false;
1289e5dd7070Spatrick 
1290e5dd7070Spatrick         // C++11 [class.copy]p8:
1291e5dd7070Spatrick         //   The implicitly-declared copy constructor for a class X will have
1292e5dd7070Spatrick         //   the form 'X::X(const X&)' if each potentially constructed subobject
1293e5dd7070Spatrick         //   of a class type M (or array thereof) has a copy constructor whose
1294e5dd7070Spatrick         //   first parameter is of type 'const M&' or 'const volatile M&'.
1295e5dd7070Spatrick         if (!FieldRec->hasCopyConstructorWithConstParam())
1296e5dd7070Spatrick           data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
1297e5dd7070Spatrick 
1298e5dd7070Spatrick         // C++11 [class.copy]p18:
1299e5dd7070Spatrick         //   The implicitly-declared copy assignment oeprator for a class X will
1300e5dd7070Spatrick         //   have the form 'X& X::operator=(const X&)' if [...] for all the
1301e5dd7070Spatrick         //   non-static data members of X that are of a class type M (or array
1302e5dd7070Spatrick         //   thereof), each such class type has a copy assignment operator whose
1303e5dd7070Spatrick         //   parameter is of type 'const M&', 'const volatile M&' or 'M'.
1304e5dd7070Spatrick         if (!FieldRec->hasCopyAssignmentWithConstParam())
1305e5dd7070Spatrick           data().ImplicitCopyAssignmentHasConstParam = false;
1306e5dd7070Spatrick 
1307e5dd7070Spatrick         if (FieldRec->hasUninitializedReferenceMember() &&
1308e5dd7070Spatrick             !Field->hasInClassInitializer())
1309e5dd7070Spatrick           data().HasUninitializedReferenceMember = true;
1310e5dd7070Spatrick 
1311e5dd7070Spatrick         // C++11 [class.union]p8, DR1460:
1312e5dd7070Spatrick         //   a non-static data member of an anonymous union that is a member of
1313e5dd7070Spatrick         //   X is also a variant member of X.
1314e5dd7070Spatrick         if (FieldRec->hasVariantMembers() &&
1315e5dd7070Spatrick             Field->isAnonymousStructOrUnion())
1316e5dd7070Spatrick           data().HasVariantMembers = true;
1317e5dd7070Spatrick       }
1318e5dd7070Spatrick     } else {
1319e5dd7070Spatrick       // Base element type of field is a non-class type.
1320e5dd7070Spatrick       if (!T->isLiteralType(Context) ||
1321e5dd7070Spatrick           (!Field->hasInClassInitializer() && !isUnion() &&
1322ec727ea7Spatrick            !Context.getLangOpts().CPlusPlus20))
1323e5dd7070Spatrick         data().DefaultedDefaultConstructorIsConstexpr = false;
1324e5dd7070Spatrick 
1325e5dd7070Spatrick       // C++11 [class.copy]p23:
1326e5dd7070Spatrick       //   A defaulted copy/move assignment operator for a class X is defined
1327e5dd7070Spatrick       //   as deleted if X has:
1328e5dd7070Spatrick       //    -- a non-static data member of const non-class type (or array
1329e5dd7070Spatrick       //       thereof)
1330ec727ea7Spatrick       if (T.isConstQualified()) {
1331ec727ea7Spatrick         data().DefaultedCopyAssignmentIsDeleted = true;
1332e5dd7070Spatrick         data().DefaultedMoveAssignmentIsDeleted = true;
1333e5dd7070Spatrick       }
1334a9ac8606Spatrick 
1335a9ac8606Spatrick       // C++20 [temp.param]p7:
1336a9ac8606Spatrick       //   A structural type is [...] a literal class type [for which] the
1337a9ac8606Spatrick       //   types of all non-static data members are structural types or
1338a9ac8606Spatrick       //   (possibly multidimensional) array thereof
1339a9ac8606Spatrick       // We deal with class types elsewhere.
1340a9ac8606Spatrick       if (!T->isStructuralType())
1341a9ac8606Spatrick         data().StructuralIfLiteral = false;
1342ec727ea7Spatrick     }
1343e5dd7070Spatrick 
1344e5dd7070Spatrick     // C++14 [meta.unary.prop]p4:
1345e5dd7070Spatrick     //   T is a class type [...] with [...] no non-static data members other
1346e5dd7070Spatrick     //   than subobjects of zero size
1347e5dd7070Spatrick     if (data().Empty && !IsZeroSize)
1348e5dd7070Spatrick       data().Empty = false;
1349e5dd7070Spatrick   }
1350e5dd7070Spatrick 
1351e5dd7070Spatrick   // Handle using declarations of conversion functions.
1352e5dd7070Spatrick   if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
1353e5dd7070Spatrick     if (Shadow->getDeclName().getNameKind()
1354e5dd7070Spatrick           == DeclarationName::CXXConversionFunctionName) {
1355e5dd7070Spatrick       ASTContext &Ctx = getASTContext();
1356e5dd7070Spatrick       data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1357e5dd7070Spatrick     }
1358e5dd7070Spatrick   }
1359e5dd7070Spatrick 
1360e5dd7070Spatrick   if (const auto *Using = dyn_cast<UsingDecl>(D)) {
1361e5dd7070Spatrick     if (Using->getDeclName().getNameKind() ==
1362e5dd7070Spatrick         DeclarationName::CXXConstructorName) {
1363e5dd7070Spatrick       data().HasInheritedConstructor = true;
1364e5dd7070Spatrick       // C++1z [dcl.init.aggr]p1:
1365e5dd7070Spatrick       //  An aggregate is [...] a class [...] with no inherited constructors
1366e5dd7070Spatrick       data().Aggregate = false;
1367e5dd7070Spatrick     }
1368e5dd7070Spatrick 
1369e5dd7070Spatrick     if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1370e5dd7070Spatrick       data().HasInheritedAssignment = true;
1371e5dd7070Spatrick   }
1372e5dd7070Spatrick }
1373e5dd7070Spatrick 
addedSelectedDestructor(CXXDestructorDecl * DD)1374*12c85518Srobert void CXXRecordDecl::addedSelectedDestructor(CXXDestructorDecl *DD) {
1375*12c85518Srobert   DD->setIneligibleOrNotSelected(false);
1376*12c85518Srobert   addedEligibleSpecialMemberFunction(DD, SMF_Destructor);
1377*12c85518Srobert }
1378*12c85518Srobert 
addedEligibleSpecialMemberFunction(const CXXMethodDecl * MD,unsigned SMKind)1379*12c85518Srobert void CXXRecordDecl::addedEligibleSpecialMemberFunction(const CXXMethodDecl *MD,
1380*12c85518Srobert                                                        unsigned SMKind) {
1381*12c85518Srobert   // FIXME: We shouldn't change DeclaredNonTrivialSpecialMembers if `MD` is
1382*12c85518Srobert   // a function template, but this needs CWG attention before we break ABI.
1383*12c85518Srobert   // See https://github.com/llvm/llvm-project/issues/59206
1384*12c85518Srobert 
1385*12c85518Srobert   if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1386*12c85518Srobert     if (DD->isUserProvided())
1387*12c85518Srobert       data().HasIrrelevantDestructor = false;
1388*12c85518Srobert     // If the destructor is explicitly defaulted and not trivial or not public
1389*12c85518Srobert     // or if the destructor is deleted, we clear HasIrrelevantDestructor in
1390*12c85518Srobert     // finishedDefaultedOrDeletedMember.
1391*12c85518Srobert 
1392*12c85518Srobert     // C++11 [class.dtor]p5:
1393*12c85518Srobert     //   A destructor is trivial if [...] the destructor is not virtual.
1394*12c85518Srobert     if (DD->isVirtual()) {
1395*12c85518Srobert       data().HasTrivialSpecialMembers &= ~SMF_Destructor;
1396*12c85518Srobert       data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
1397*12c85518Srobert     }
1398*12c85518Srobert 
1399*12c85518Srobert     if (DD->isNoReturn())
1400*12c85518Srobert       data().IsAnyDestructorNoReturn = true;
1401*12c85518Srobert   }
1402*12c85518Srobert 
1403*12c85518Srobert   if (!MD->isImplicit() && !MD->isUserProvided()) {
1404*12c85518Srobert     // This method is user-declared but not user-provided. We can't work
1405*12c85518Srobert     // out whether it's trivial yet (not until we get to the end of the
1406*12c85518Srobert     // class). We'll handle this method in
1407*12c85518Srobert     // finishedDefaultedOrDeletedMember.
1408*12c85518Srobert   } else if (MD->isTrivial()) {
1409*12c85518Srobert     data().HasTrivialSpecialMembers |= SMKind;
1410*12c85518Srobert     data().HasTrivialSpecialMembersForCall |= SMKind;
1411*12c85518Srobert   } else if (MD->isTrivialForCall()) {
1412*12c85518Srobert     data().HasTrivialSpecialMembersForCall |= SMKind;
1413*12c85518Srobert     data().DeclaredNonTrivialSpecialMembers |= SMKind;
1414*12c85518Srobert   } else {
1415*12c85518Srobert     data().DeclaredNonTrivialSpecialMembers |= SMKind;
1416*12c85518Srobert     // If this is a user-provided function, do not set
1417*12c85518Srobert     // DeclaredNonTrivialSpecialMembersForCall here since we don't know
1418*12c85518Srobert     // yet whether the method would be considered non-trivial for the
1419*12c85518Srobert     // purpose of calls (attribute "trivial_abi" can be dropped from the
1420*12c85518Srobert     // class later, which can change the special method's triviality).
1421*12c85518Srobert     if (!MD->isUserProvided())
1422*12c85518Srobert       data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1423*12c85518Srobert   }
1424*12c85518Srobert }
1425*12c85518Srobert 
finishedDefaultedOrDeletedMember(CXXMethodDecl * D)1426e5dd7070Spatrick void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1427e5dd7070Spatrick   assert(!D->isImplicit() && !D->isUserProvided());
1428e5dd7070Spatrick 
1429e5dd7070Spatrick   // The kind of special member this declaration is, if any.
1430e5dd7070Spatrick   unsigned SMKind = 0;
1431e5dd7070Spatrick 
1432e5dd7070Spatrick   if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1433e5dd7070Spatrick     if (Constructor->isDefaultConstructor()) {
1434e5dd7070Spatrick       SMKind |= SMF_DefaultConstructor;
1435e5dd7070Spatrick       if (Constructor->isConstexpr())
1436e5dd7070Spatrick         data().HasConstexprDefaultConstructor = true;
1437e5dd7070Spatrick     }
1438e5dd7070Spatrick     if (Constructor->isCopyConstructor())
1439e5dd7070Spatrick       SMKind |= SMF_CopyConstructor;
1440e5dd7070Spatrick     else if (Constructor->isMoveConstructor())
1441e5dd7070Spatrick       SMKind |= SMF_MoveConstructor;
1442e5dd7070Spatrick     else if (Constructor->isConstexpr())
1443e5dd7070Spatrick       // We may now know that the constructor is constexpr.
1444e5dd7070Spatrick       data().HasConstexprNonCopyMoveConstructor = true;
1445e5dd7070Spatrick   } else if (isa<CXXDestructorDecl>(D)) {
1446e5dd7070Spatrick     SMKind |= SMF_Destructor;
1447e5dd7070Spatrick     if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1448e5dd7070Spatrick       data().HasIrrelevantDestructor = false;
1449e5dd7070Spatrick   } else if (D->isCopyAssignmentOperator())
1450e5dd7070Spatrick     SMKind |= SMF_CopyAssignment;
1451e5dd7070Spatrick   else if (D->isMoveAssignmentOperator())
1452e5dd7070Spatrick     SMKind |= SMF_MoveAssignment;
1453e5dd7070Spatrick 
1454e5dd7070Spatrick   // Update which trivial / non-trivial special members we have.
1455e5dd7070Spatrick   // addedMember will have skipped this step for this member.
1456*12c85518Srobert   if (!D->isIneligibleOrNotSelected()) {
1457e5dd7070Spatrick     if (D->isTrivial())
1458e5dd7070Spatrick       data().HasTrivialSpecialMembers |= SMKind;
1459e5dd7070Spatrick     else
1460e5dd7070Spatrick       data().DeclaredNonTrivialSpecialMembers |= SMKind;
1461e5dd7070Spatrick   }
1462*12c85518Srobert }
1463*12c85518Srobert 
AddCaptureList(ASTContext & Ctx,Capture * CaptureList)1464*12c85518Srobert void CXXRecordDecl::LambdaDefinitionData::AddCaptureList(ASTContext &Ctx,
1465*12c85518Srobert                                                          Capture *CaptureList) {
1466*12c85518Srobert   Captures.push_back(CaptureList);
1467*12c85518Srobert   if (Captures.size() == 2) {
1468*12c85518Srobert     // The TinyPtrVector member now needs destruction.
1469*12c85518Srobert     Ctx.addDestruction(&Captures);
1470*12c85518Srobert   }
1471*12c85518Srobert }
1472e5dd7070Spatrick 
setCaptures(ASTContext & Context,ArrayRef<LambdaCapture> Captures)1473a9ac8606Spatrick void CXXRecordDecl::setCaptures(ASTContext &Context,
1474a9ac8606Spatrick                                 ArrayRef<LambdaCapture> Captures) {
1475ec727ea7Spatrick   CXXRecordDecl::LambdaDefinitionData &Data = getLambdaData();
1476ec727ea7Spatrick 
1477ec727ea7Spatrick   // Copy captures.
1478ec727ea7Spatrick   Data.NumCaptures = Captures.size();
1479ec727ea7Spatrick   Data.NumExplicitCaptures = 0;
1480*12c85518Srobert   auto *ToCapture = (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) *
1481ec727ea7Spatrick                                                       Captures.size());
1482*12c85518Srobert   Data.AddCaptureList(Context, ToCapture);
1483ec727ea7Spatrick   for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
1484ec727ea7Spatrick     if (Captures[I].isExplicit())
1485ec727ea7Spatrick       ++Data.NumExplicitCaptures;
1486ec727ea7Spatrick 
1487ec727ea7Spatrick     *ToCapture++ = Captures[I];
1488ec727ea7Spatrick   }
1489ec727ea7Spatrick 
1490ec727ea7Spatrick   if (!lambdaIsDefaultConstructibleAndAssignable())
1491ec727ea7Spatrick     Data.DefaultedCopyAssignmentIsDeleted = true;
1492ec727ea7Spatrick }
1493ec727ea7Spatrick 
setTrivialForCallFlags(CXXMethodDecl * D)1494e5dd7070Spatrick void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1495e5dd7070Spatrick   unsigned SMKind = 0;
1496e5dd7070Spatrick 
1497e5dd7070Spatrick   if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1498e5dd7070Spatrick     if (Constructor->isCopyConstructor())
1499e5dd7070Spatrick       SMKind = SMF_CopyConstructor;
1500e5dd7070Spatrick     else if (Constructor->isMoveConstructor())
1501e5dd7070Spatrick       SMKind = SMF_MoveConstructor;
1502e5dd7070Spatrick   } else if (isa<CXXDestructorDecl>(D))
1503e5dd7070Spatrick     SMKind = SMF_Destructor;
1504e5dd7070Spatrick 
1505e5dd7070Spatrick   if (D->isTrivialForCall())
1506e5dd7070Spatrick     data().HasTrivialSpecialMembersForCall |= SMKind;
1507e5dd7070Spatrick   else
1508e5dd7070Spatrick     data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1509e5dd7070Spatrick }
1510e5dd7070Spatrick 
isCLike() const1511e5dd7070Spatrick bool CXXRecordDecl::isCLike() const {
1512e5dd7070Spatrick   if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1513e5dd7070Spatrick       !TemplateOrInstantiation.isNull())
1514e5dd7070Spatrick     return false;
1515e5dd7070Spatrick   if (!hasDefinition())
1516e5dd7070Spatrick     return true;
1517e5dd7070Spatrick 
1518e5dd7070Spatrick   return isPOD() && data().HasOnlyCMembers;
1519e5dd7070Spatrick }
1520e5dd7070Spatrick 
isGenericLambda() const1521e5dd7070Spatrick bool CXXRecordDecl::isGenericLambda() const {
1522e5dd7070Spatrick   if (!isLambda()) return false;
1523e5dd7070Spatrick   return getLambdaData().IsGenericLambda;
1524e5dd7070Spatrick }
1525e5dd7070Spatrick 
1526e5dd7070Spatrick #ifndef NDEBUG
allLookupResultsAreTheSame(const DeclContext::lookup_result & R)1527e5dd7070Spatrick static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1528e5dd7070Spatrick   for (auto *D : R)
1529e5dd7070Spatrick     if (!declaresSameEntity(D, R.front()))
1530e5dd7070Spatrick       return false;
1531e5dd7070Spatrick   return true;
1532e5dd7070Spatrick }
1533e5dd7070Spatrick #endif
1534e5dd7070Spatrick 
getLambdaCallOperatorHelper(const CXXRecordDecl & RD)1535e5dd7070Spatrick static NamedDecl* getLambdaCallOperatorHelper(const CXXRecordDecl &RD) {
1536e5dd7070Spatrick   if (!RD.isLambda()) return nullptr;
1537e5dd7070Spatrick   DeclarationName Name =
1538e5dd7070Spatrick     RD.getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1539e5dd7070Spatrick   DeclContext::lookup_result Calls = RD.lookup(Name);
1540e5dd7070Spatrick 
1541e5dd7070Spatrick   assert(!Calls.empty() && "Missing lambda call operator!");
1542e5dd7070Spatrick   assert(allLookupResultsAreTheSame(Calls) &&
1543e5dd7070Spatrick          "More than one lambda call operator!");
1544e5dd7070Spatrick   return Calls.front();
1545e5dd7070Spatrick }
1546e5dd7070Spatrick 
getDependentLambdaCallOperator() const1547e5dd7070Spatrick FunctionTemplateDecl* CXXRecordDecl::getDependentLambdaCallOperator() const {
1548e5dd7070Spatrick   NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
1549e5dd7070Spatrick   return  dyn_cast_or_null<FunctionTemplateDecl>(CallOp);
1550e5dd7070Spatrick }
1551e5dd7070Spatrick 
getLambdaCallOperator() const1552e5dd7070Spatrick CXXMethodDecl *CXXRecordDecl::getLambdaCallOperator() const {
1553e5dd7070Spatrick   NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
1554e5dd7070Spatrick 
1555e5dd7070Spatrick   if (CallOp == nullptr)
1556e5dd7070Spatrick     return nullptr;
1557e5dd7070Spatrick 
1558e5dd7070Spatrick   if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
1559e5dd7070Spatrick     return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
1560e5dd7070Spatrick 
1561e5dd7070Spatrick   return cast<CXXMethodDecl>(CallOp);
1562e5dd7070Spatrick }
1563e5dd7070Spatrick 
getLambdaStaticInvoker() const1564e5dd7070Spatrick CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
1565a9ac8606Spatrick   CXXMethodDecl *CallOp = getLambdaCallOperator();
1566a9ac8606Spatrick   CallingConv CC = CallOp->getType()->castAs<FunctionType>()->getCallConv();
1567a9ac8606Spatrick   return getLambdaStaticInvoker(CC);
1568a9ac8606Spatrick }
1569e5dd7070Spatrick 
1570a9ac8606Spatrick static DeclContext::lookup_result
getLambdaStaticInvokers(const CXXRecordDecl & RD)1571a9ac8606Spatrick getLambdaStaticInvokers(const CXXRecordDecl &RD) {
1572a9ac8606Spatrick   assert(RD.isLambda() && "Must be a lambda");
1573a9ac8606Spatrick   DeclarationName Name =
1574a9ac8606Spatrick       &RD.getASTContext().Idents.get(getLambdaStaticInvokerName());
1575a9ac8606Spatrick   return RD.lookup(Name);
1576a9ac8606Spatrick }
1577a9ac8606Spatrick 
getInvokerAsMethod(NamedDecl * ND)1578a9ac8606Spatrick static CXXMethodDecl *getInvokerAsMethod(NamedDecl *ND) {
1579a9ac8606Spatrick   if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(ND))
1580a9ac8606Spatrick     return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1581a9ac8606Spatrick   return cast<CXXMethodDecl>(ND);
1582a9ac8606Spatrick }
1583a9ac8606Spatrick 
getLambdaStaticInvoker(CallingConv CC) const1584a9ac8606Spatrick CXXMethodDecl *CXXRecordDecl::getLambdaStaticInvoker(CallingConv CC) const {
1585a9ac8606Spatrick   if (!isLambda())
1586a9ac8606Spatrick     return nullptr;
1587a9ac8606Spatrick   DeclContext::lookup_result Invoker = getLambdaStaticInvokers(*this);
1588a9ac8606Spatrick 
1589a9ac8606Spatrick   for (NamedDecl *ND : Invoker) {
1590a9ac8606Spatrick     const auto *FTy =
1591a9ac8606Spatrick         cast<ValueDecl>(ND->getAsFunction())->getType()->castAs<FunctionType>();
1592a9ac8606Spatrick     if (FTy->getCallConv() == CC)
1593a9ac8606Spatrick       return getInvokerAsMethod(ND);
1594a9ac8606Spatrick   }
1595a9ac8606Spatrick 
1596a9ac8606Spatrick   return nullptr;
1597e5dd7070Spatrick }
1598e5dd7070Spatrick 
getCaptureFields(llvm::DenseMap<const ValueDecl *,FieldDecl * > & Captures,FieldDecl * & ThisCapture) const1599e5dd7070Spatrick void CXXRecordDecl::getCaptureFields(
1600*12c85518Srobert     llvm::DenseMap<const ValueDecl *, FieldDecl *> &Captures,
1601e5dd7070Spatrick     FieldDecl *&ThisCapture) const {
1602e5dd7070Spatrick   Captures.clear();
1603e5dd7070Spatrick   ThisCapture = nullptr;
1604e5dd7070Spatrick 
1605e5dd7070Spatrick   LambdaDefinitionData &Lambda = getLambdaData();
1606*12c85518Srobert   for (const LambdaCapture *List : Lambda.Captures) {
1607e5dd7070Spatrick     RecordDecl::field_iterator Field = field_begin();
1608*12c85518Srobert     for (const LambdaCapture *C = List, *CEnd = C + Lambda.NumCaptures;
1609e5dd7070Spatrick          C != CEnd; ++C, ++Field) {
1610e5dd7070Spatrick       if (C->capturesThis())
1611e5dd7070Spatrick         ThisCapture = *Field;
1612e5dd7070Spatrick       else if (C->capturesVariable())
1613e5dd7070Spatrick         Captures[C->getCapturedVar()] = *Field;
1614e5dd7070Spatrick     }
1615e5dd7070Spatrick     assert(Field == field_end());
1616e5dd7070Spatrick   }
1617*12c85518Srobert }
1618e5dd7070Spatrick 
1619e5dd7070Spatrick TemplateParameterList *
getGenericLambdaTemplateParameterList() const1620e5dd7070Spatrick CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1621e5dd7070Spatrick   if (!isGenericLambda()) return nullptr;
1622e5dd7070Spatrick   CXXMethodDecl *CallOp = getLambdaCallOperator();
1623e5dd7070Spatrick   if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1624e5dd7070Spatrick     return Tmpl->getTemplateParameters();
1625e5dd7070Spatrick   return nullptr;
1626e5dd7070Spatrick }
1627e5dd7070Spatrick 
1628e5dd7070Spatrick ArrayRef<NamedDecl *>
getLambdaExplicitTemplateParameters() const1629e5dd7070Spatrick CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1630e5dd7070Spatrick   TemplateParameterList *List = getGenericLambdaTemplateParameterList();
1631e5dd7070Spatrick   if (!List)
1632e5dd7070Spatrick     return {};
1633e5dd7070Spatrick 
1634e5dd7070Spatrick   assert(std::is_partitioned(List->begin(), List->end(),
1635e5dd7070Spatrick                              [](const NamedDecl *D) { return !D->isImplicit(); })
1636e5dd7070Spatrick          && "Explicit template params should be ordered before implicit ones");
1637e5dd7070Spatrick 
1638e5dd7070Spatrick   const auto ExplicitEnd = llvm::partition_point(
1639e5dd7070Spatrick       *List, [](const NamedDecl *D) { return !D->isImplicit(); });
1640*12c85518Srobert   return llvm::ArrayRef(List->begin(), ExplicitEnd);
1641e5dd7070Spatrick }
1642e5dd7070Spatrick 
getLambdaContextDecl() const1643e5dd7070Spatrick Decl *CXXRecordDecl::getLambdaContextDecl() const {
1644e5dd7070Spatrick   assert(isLambda() && "Not a lambda closure type!");
1645e5dd7070Spatrick   ExternalASTSource *Source = getParentASTContext().getExternalSource();
1646e5dd7070Spatrick   return getLambdaData().ContextDecl.get(Source);
1647e5dd7070Spatrick }
1648e5dd7070Spatrick 
setDeviceLambdaManglingNumber(unsigned Num) const1649a9ac8606Spatrick void CXXRecordDecl::setDeviceLambdaManglingNumber(unsigned Num) const {
1650a9ac8606Spatrick   assert(isLambda() && "Not a lambda closure type!");
1651a9ac8606Spatrick   if (Num)
1652a9ac8606Spatrick     getASTContext().DeviceLambdaManglingNumbers[this] = Num;
1653a9ac8606Spatrick }
1654a9ac8606Spatrick 
getDeviceLambdaManglingNumber() const1655a9ac8606Spatrick unsigned CXXRecordDecl::getDeviceLambdaManglingNumber() const {
1656a9ac8606Spatrick   assert(isLambda() && "Not a lambda closure type!");
1657a9ac8606Spatrick   auto I = getASTContext().DeviceLambdaManglingNumbers.find(this);
1658a9ac8606Spatrick   if (I != getASTContext().DeviceLambdaManglingNumbers.end())
1659a9ac8606Spatrick     return I->second;
1660a9ac8606Spatrick   return 0;
1661a9ac8606Spatrick }
1662a9ac8606Spatrick 
GetConversionType(ASTContext & Context,NamedDecl * Conv)1663e5dd7070Spatrick static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1664e5dd7070Spatrick   QualType T =
1665e5dd7070Spatrick       cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1666e5dd7070Spatrick           ->getConversionType();
1667e5dd7070Spatrick   return Context.getCanonicalType(T);
1668e5dd7070Spatrick }
1669e5dd7070Spatrick 
1670e5dd7070Spatrick /// Collect the visible conversions of a base class.
1671e5dd7070Spatrick ///
1672e5dd7070Spatrick /// \param Record a base class of the class we're considering
1673e5dd7070Spatrick /// \param InVirtual whether this base class is a virtual base (or a base
1674e5dd7070Spatrick ///   of a virtual base)
1675e5dd7070Spatrick /// \param Access the access along the inheritance path to this base
1676e5dd7070Spatrick /// \param ParentHiddenTypes the conversions provided by the inheritors
1677e5dd7070Spatrick ///   of this base
1678e5dd7070Spatrick /// \param Output the set to which to add conversions from non-virtual bases
1679e5dd7070Spatrick /// \param VOutput the set to which to add conversions from virtual bases
1680e5dd7070Spatrick /// \param HiddenVBaseCs the set of conversions which were hidden in a
1681e5dd7070Spatrick ///   virtual base along some inheritance path
CollectVisibleConversions(ASTContext & Context,const CXXRecordDecl * Record,bool InVirtual,AccessSpecifier Access,const llvm::SmallPtrSet<CanQualType,8> & ParentHiddenTypes,ASTUnresolvedSet & Output,UnresolvedSetImpl & VOutput,llvm::SmallPtrSet<NamedDecl *,8> & HiddenVBaseCs)1682e5dd7070Spatrick static void CollectVisibleConversions(
1683e5dd7070Spatrick     ASTContext &Context, const CXXRecordDecl *Record, bool InVirtual,
1684e5dd7070Spatrick     AccessSpecifier Access,
1685e5dd7070Spatrick     const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1686e5dd7070Spatrick     ASTUnresolvedSet &Output, UnresolvedSetImpl &VOutput,
1687e5dd7070Spatrick     llvm::SmallPtrSet<NamedDecl *, 8> &HiddenVBaseCs) {
1688e5dd7070Spatrick   // The set of types which have conversions in this class or its
1689e5dd7070Spatrick   // subclasses.  As an optimization, we don't copy the derived set
1690e5dd7070Spatrick   // unless it might change.
1691e5dd7070Spatrick   const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1692e5dd7070Spatrick   llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1693e5dd7070Spatrick 
1694e5dd7070Spatrick   // Collect the direct conversions and figure out which conversions
1695e5dd7070Spatrick   // will be hidden in the subclasses.
1696e5dd7070Spatrick   CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1697e5dd7070Spatrick   CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1698e5dd7070Spatrick   if (ConvI != ConvE) {
1699e5dd7070Spatrick     HiddenTypesBuffer = ParentHiddenTypes;
1700e5dd7070Spatrick     HiddenTypes = &HiddenTypesBuffer;
1701e5dd7070Spatrick 
1702e5dd7070Spatrick     for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
1703e5dd7070Spatrick       CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1704e5dd7070Spatrick       bool Hidden = ParentHiddenTypes.count(ConvType);
1705e5dd7070Spatrick       if (!Hidden)
1706e5dd7070Spatrick         HiddenTypesBuffer.insert(ConvType);
1707e5dd7070Spatrick 
1708e5dd7070Spatrick       // If this conversion is hidden and we're in a virtual base,
1709e5dd7070Spatrick       // remember that it's hidden along some inheritance path.
1710e5dd7070Spatrick       if (Hidden && InVirtual)
1711e5dd7070Spatrick         HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1712e5dd7070Spatrick 
1713e5dd7070Spatrick       // If this conversion isn't hidden, add it to the appropriate output.
1714e5dd7070Spatrick       else if (!Hidden) {
1715e5dd7070Spatrick         AccessSpecifier IAccess
1716e5dd7070Spatrick           = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1717e5dd7070Spatrick 
1718e5dd7070Spatrick         if (InVirtual)
1719e5dd7070Spatrick           VOutput.addDecl(I.getDecl(), IAccess);
1720e5dd7070Spatrick         else
1721e5dd7070Spatrick           Output.addDecl(Context, I.getDecl(), IAccess);
1722e5dd7070Spatrick       }
1723e5dd7070Spatrick     }
1724e5dd7070Spatrick   }
1725e5dd7070Spatrick 
1726e5dd7070Spatrick   // Collect information recursively from any base classes.
1727e5dd7070Spatrick   for (const auto &I : Record->bases()) {
1728e5dd7070Spatrick     const auto *RT = I.getType()->getAs<RecordType>();
1729e5dd7070Spatrick     if (!RT) continue;
1730e5dd7070Spatrick 
1731e5dd7070Spatrick     AccessSpecifier BaseAccess
1732e5dd7070Spatrick       = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1733e5dd7070Spatrick     bool BaseInVirtual = InVirtual || I.isVirtual();
1734e5dd7070Spatrick 
1735e5dd7070Spatrick     auto *Base = cast<CXXRecordDecl>(RT->getDecl());
1736e5dd7070Spatrick     CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1737e5dd7070Spatrick                               *HiddenTypes, Output, VOutput, HiddenVBaseCs);
1738e5dd7070Spatrick   }
1739e5dd7070Spatrick }
1740e5dd7070Spatrick 
1741e5dd7070Spatrick /// Collect the visible conversions of a class.
1742e5dd7070Spatrick ///
1743e5dd7070Spatrick /// This would be extremely straightforward if it weren't for virtual
1744e5dd7070Spatrick /// bases.  It might be worth special-casing that, really.
CollectVisibleConversions(ASTContext & Context,const CXXRecordDecl * Record,ASTUnresolvedSet & Output)1745e5dd7070Spatrick static void CollectVisibleConversions(ASTContext &Context,
1746e5dd7070Spatrick                                       const CXXRecordDecl *Record,
1747e5dd7070Spatrick                                       ASTUnresolvedSet &Output) {
1748e5dd7070Spatrick   // The collection of all conversions in virtual bases that we've
1749e5dd7070Spatrick   // found.  These will be added to the output as long as they don't
1750e5dd7070Spatrick   // appear in the hidden-conversions set.
1751e5dd7070Spatrick   UnresolvedSet<8> VBaseCs;
1752e5dd7070Spatrick 
1753e5dd7070Spatrick   // The set of conversions in virtual bases that we've determined to
1754e5dd7070Spatrick   // be hidden.
1755e5dd7070Spatrick   llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1756e5dd7070Spatrick 
1757e5dd7070Spatrick   // The set of types hidden by classes derived from this one.
1758e5dd7070Spatrick   llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1759e5dd7070Spatrick 
1760e5dd7070Spatrick   // Go ahead and collect the direct conversions and add them to the
1761e5dd7070Spatrick   // hidden-types set.
1762e5dd7070Spatrick   CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1763e5dd7070Spatrick   CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1764e5dd7070Spatrick   Output.append(Context, ConvI, ConvE);
1765e5dd7070Spatrick   for (; ConvI != ConvE; ++ConvI)
1766e5dd7070Spatrick     HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
1767e5dd7070Spatrick 
1768e5dd7070Spatrick   // Recursively collect conversions from base classes.
1769e5dd7070Spatrick   for (const auto &I : Record->bases()) {
1770e5dd7070Spatrick     const auto *RT = I.getType()->getAs<RecordType>();
1771e5dd7070Spatrick     if (!RT) continue;
1772e5dd7070Spatrick 
1773e5dd7070Spatrick     CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1774e5dd7070Spatrick                               I.isVirtual(), I.getAccessSpecifier(),
1775e5dd7070Spatrick                               HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1776e5dd7070Spatrick   }
1777e5dd7070Spatrick 
1778e5dd7070Spatrick   // Add any unhidden conversions provided by virtual bases.
1779e5dd7070Spatrick   for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1780e5dd7070Spatrick          I != E; ++I) {
1781e5dd7070Spatrick     if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1782e5dd7070Spatrick       Output.addDecl(Context, I.getDecl(), I.getAccess());
1783e5dd7070Spatrick   }
1784e5dd7070Spatrick }
1785e5dd7070Spatrick 
1786e5dd7070Spatrick /// getVisibleConversionFunctions - get all conversion functions visible
1787e5dd7070Spatrick /// in current class; including conversion function templates.
1788e5dd7070Spatrick llvm::iterator_range<CXXRecordDecl::conversion_iterator>
getVisibleConversionFunctions() const1789e5dd7070Spatrick CXXRecordDecl::getVisibleConversionFunctions() const {
1790e5dd7070Spatrick   ASTContext &Ctx = getASTContext();
1791e5dd7070Spatrick 
1792e5dd7070Spatrick   ASTUnresolvedSet *Set;
1793e5dd7070Spatrick   if (bases_begin() == bases_end()) {
1794e5dd7070Spatrick     // If root class, all conversions are visible.
1795e5dd7070Spatrick     Set = &data().Conversions.get(Ctx);
1796e5dd7070Spatrick   } else {
1797e5dd7070Spatrick     Set = &data().VisibleConversions.get(Ctx);
1798e5dd7070Spatrick     // If visible conversion list is not evaluated, evaluate it.
1799e5dd7070Spatrick     if (!data().ComputedVisibleConversions) {
1800e5dd7070Spatrick       CollectVisibleConversions(Ctx, this, *Set);
1801e5dd7070Spatrick       data().ComputedVisibleConversions = true;
1802e5dd7070Spatrick     }
1803e5dd7070Spatrick   }
1804e5dd7070Spatrick   return llvm::make_range(Set->begin(), Set->end());
1805e5dd7070Spatrick }
1806e5dd7070Spatrick 
removeConversion(const NamedDecl * ConvDecl)1807e5dd7070Spatrick void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1808e5dd7070Spatrick   // This operation is O(N) but extremely rare.  Sema only uses it to
1809e5dd7070Spatrick   // remove UsingShadowDecls in a class that were followed by a direct
1810e5dd7070Spatrick   // declaration, e.g.:
1811e5dd7070Spatrick   //   class A : B {
1812e5dd7070Spatrick   //     using B::operator int;
1813e5dd7070Spatrick   //     operator int();
1814e5dd7070Spatrick   //   };
1815e5dd7070Spatrick   // This is uncommon by itself and even more uncommon in conjunction
1816e5dd7070Spatrick   // with sufficiently large numbers of directly-declared conversions
1817e5dd7070Spatrick   // that asymptotic behavior matters.
1818e5dd7070Spatrick 
1819e5dd7070Spatrick   ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
1820e5dd7070Spatrick   for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1821e5dd7070Spatrick     if (Convs[I].getDecl() == ConvDecl) {
1822e5dd7070Spatrick       Convs.erase(I);
1823*12c85518Srobert       assert(!llvm::is_contained(Convs, ConvDecl) &&
1824e5dd7070Spatrick              "conversion was found multiple times in unresolved set");
1825e5dd7070Spatrick       return;
1826e5dd7070Spatrick     }
1827e5dd7070Spatrick   }
1828e5dd7070Spatrick 
1829e5dd7070Spatrick   llvm_unreachable("conversion not found in set!");
1830e5dd7070Spatrick }
1831e5dd7070Spatrick 
getInstantiatedFromMemberClass() const1832e5dd7070Spatrick CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
1833e5dd7070Spatrick   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1834e5dd7070Spatrick     return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1835e5dd7070Spatrick 
1836e5dd7070Spatrick   return nullptr;
1837e5dd7070Spatrick }
1838e5dd7070Spatrick 
getMemberSpecializationInfo() const1839e5dd7070Spatrick MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1840e5dd7070Spatrick   return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1841e5dd7070Spatrick }
1842e5dd7070Spatrick 
1843e5dd7070Spatrick void
setInstantiationOfMemberClass(CXXRecordDecl * RD,TemplateSpecializationKind TSK)1844e5dd7070Spatrick CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1845e5dd7070Spatrick                                              TemplateSpecializationKind TSK) {
1846e5dd7070Spatrick   assert(TemplateOrInstantiation.isNull() &&
1847e5dd7070Spatrick          "Previous template or instantiation?");
1848e5dd7070Spatrick   assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
1849e5dd7070Spatrick   TemplateOrInstantiation
1850e5dd7070Spatrick     = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1851e5dd7070Spatrick }
1852e5dd7070Spatrick 
getDescribedClassTemplate() const1853e5dd7070Spatrick ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1854e5dd7070Spatrick   return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1855e5dd7070Spatrick }
1856e5dd7070Spatrick 
setDescribedClassTemplate(ClassTemplateDecl * Template)1857e5dd7070Spatrick void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1858e5dd7070Spatrick   TemplateOrInstantiation = Template;
1859e5dd7070Spatrick }
1860e5dd7070Spatrick 
getTemplateSpecializationKind() const1861e5dd7070Spatrick TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1862e5dd7070Spatrick   if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
1863e5dd7070Spatrick     return Spec->getSpecializationKind();
1864e5dd7070Spatrick 
1865e5dd7070Spatrick   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1866e5dd7070Spatrick     return MSInfo->getTemplateSpecializationKind();
1867e5dd7070Spatrick 
1868e5dd7070Spatrick   return TSK_Undeclared;
1869e5dd7070Spatrick }
1870e5dd7070Spatrick 
1871e5dd7070Spatrick void
setTemplateSpecializationKind(TemplateSpecializationKind TSK)1872e5dd7070Spatrick CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1873e5dd7070Spatrick   if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1874e5dd7070Spatrick     Spec->setSpecializationKind(TSK);
1875e5dd7070Spatrick     return;
1876e5dd7070Spatrick   }
1877e5dd7070Spatrick 
1878e5dd7070Spatrick   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1879e5dd7070Spatrick     MSInfo->setTemplateSpecializationKind(TSK);
1880e5dd7070Spatrick     return;
1881e5dd7070Spatrick   }
1882e5dd7070Spatrick 
1883e5dd7070Spatrick   llvm_unreachable("Not a class template or member class specialization");
1884e5dd7070Spatrick }
1885e5dd7070Spatrick 
getTemplateInstantiationPattern() const1886e5dd7070Spatrick const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1887e5dd7070Spatrick   auto GetDefinitionOrSelf =
1888e5dd7070Spatrick       [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1889e5dd7070Spatrick     if (auto *Def = D->getDefinition())
1890e5dd7070Spatrick       return Def;
1891e5dd7070Spatrick     return D;
1892e5dd7070Spatrick   };
1893e5dd7070Spatrick 
1894e5dd7070Spatrick   // If it's a class template specialization, find the template or partial
1895e5dd7070Spatrick   // specialization from which it was instantiated.
1896e5dd7070Spatrick   if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1897e5dd7070Spatrick     auto From = TD->getInstantiatedFrom();
1898e5dd7070Spatrick     if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1899e5dd7070Spatrick       while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1900e5dd7070Spatrick         if (NewCTD->isMemberSpecialization())
1901e5dd7070Spatrick           break;
1902e5dd7070Spatrick         CTD = NewCTD;
1903e5dd7070Spatrick       }
1904e5dd7070Spatrick       return GetDefinitionOrSelf(CTD->getTemplatedDecl());
1905e5dd7070Spatrick     }
1906e5dd7070Spatrick     if (auto *CTPSD =
1907e5dd7070Spatrick             From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1908e5dd7070Spatrick       while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1909e5dd7070Spatrick         if (NewCTPSD->isMemberSpecialization())
1910e5dd7070Spatrick           break;
1911e5dd7070Spatrick         CTPSD = NewCTPSD;
1912e5dd7070Spatrick       }
1913e5dd7070Spatrick       return GetDefinitionOrSelf(CTPSD);
1914e5dd7070Spatrick     }
1915e5dd7070Spatrick   }
1916e5dd7070Spatrick 
1917e5dd7070Spatrick   if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1918e5dd7070Spatrick     if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1919e5dd7070Spatrick       const CXXRecordDecl *RD = this;
1920e5dd7070Spatrick       while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1921e5dd7070Spatrick         RD = NewRD;
1922e5dd7070Spatrick       return GetDefinitionOrSelf(RD);
1923e5dd7070Spatrick     }
1924e5dd7070Spatrick   }
1925e5dd7070Spatrick 
1926e5dd7070Spatrick   assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1927e5dd7070Spatrick          "couldn't find pattern for class template instantiation");
1928e5dd7070Spatrick   return nullptr;
1929e5dd7070Spatrick }
1930e5dd7070Spatrick 
getDestructor() const1931e5dd7070Spatrick CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1932e5dd7070Spatrick   ASTContext &Context = getASTContext();
1933e5dd7070Spatrick   QualType ClassType = Context.getTypeDeclType(this);
1934e5dd7070Spatrick 
1935e5dd7070Spatrick   DeclarationName Name
1936e5dd7070Spatrick     = Context.DeclarationNames.getCXXDestructorName(
1937e5dd7070Spatrick                                           Context.getCanonicalType(ClassType));
1938e5dd7070Spatrick 
1939e5dd7070Spatrick   DeclContext::lookup_result R = lookup(Name);
1940e5dd7070Spatrick 
1941*12c85518Srobert   // If a destructor was marked as not selected, we skip it. We don't always
1942*12c85518Srobert   // have a selected destructor: dependent types, unnamed structs.
1943*12c85518Srobert   for (auto *Decl : R) {
1944*12c85518Srobert     auto* DD = dyn_cast<CXXDestructorDecl>(Decl);
1945*12c85518Srobert     if (DD && !DD->isIneligibleOrNotSelected())
1946*12c85518Srobert       return DD;
1947*12c85518Srobert   }
1948*12c85518Srobert   return nullptr;
1949e5dd7070Spatrick }
1950e5dd7070Spatrick 
isDeclContextInNamespace(const DeclContext * DC)1951e5dd7070Spatrick static bool isDeclContextInNamespace(const DeclContext *DC) {
1952e5dd7070Spatrick   while (!DC->isTranslationUnit()) {
1953e5dd7070Spatrick     if (DC->isNamespace())
1954e5dd7070Spatrick       return true;
1955e5dd7070Spatrick     DC = DC->getParent();
1956e5dd7070Spatrick   }
1957e5dd7070Spatrick   return false;
1958e5dd7070Spatrick }
1959e5dd7070Spatrick 
isInterfaceLike() const1960e5dd7070Spatrick bool CXXRecordDecl::isInterfaceLike() const {
1961e5dd7070Spatrick   assert(hasDefinition() && "checking for interface-like without a definition");
1962e5dd7070Spatrick   // All __interfaces are inheritently interface-like.
1963e5dd7070Spatrick   if (isInterface())
1964e5dd7070Spatrick     return true;
1965e5dd7070Spatrick 
1966e5dd7070Spatrick   // Interface-like types cannot have a user declared constructor, destructor,
1967e5dd7070Spatrick   // friends, VBases, conversion functions, or fields.  Additionally, lambdas
1968e5dd7070Spatrick   // cannot be interface types.
1969e5dd7070Spatrick   if (isLambda() || hasUserDeclaredConstructor() ||
1970e5dd7070Spatrick       hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1971e5dd7070Spatrick       getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1972e5dd7070Spatrick     return false;
1973e5dd7070Spatrick 
1974e5dd7070Spatrick   // No interface-like type can have a method with a definition.
1975e5dd7070Spatrick   for (const auto *const Method : methods())
1976e5dd7070Spatrick     if (Method->isDefined() && !Method->isImplicit())
1977e5dd7070Spatrick       return false;
1978e5dd7070Spatrick 
1979e5dd7070Spatrick   // Check "Special" types.
1980e5dd7070Spatrick   const auto *Uuid = getAttr<UuidAttr>();
1981e5dd7070Spatrick   // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1982e5dd7070Spatrick   // extern C++ block directly in the TU.  These are only valid if in one
1983e5dd7070Spatrick   // of these two situations.
1984e5dd7070Spatrick   if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1985e5dd7070Spatrick       !isDeclContextInNamespace(getDeclContext()) &&
1986e5dd7070Spatrick       ((getName() == "IUnknown" &&
1987e5dd7070Spatrick         Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1988e5dd7070Spatrick        (getName() == "IDispatch" &&
1989e5dd7070Spatrick         Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1990e5dd7070Spatrick     if (getNumBases() > 0)
1991e5dd7070Spatrick       return false;
1992e5dd7070Spatrick     return true;
1993e5dd7070Spatrick   }
1994e5dd7070Spatrick 
1995e5dd7070Spatrick   // FIXME: Any access specifiers is supposed to make this no longer interface
1996e5dd7070Spatrick   // like.
1997e5dd7070Spatrick 
1998e5dd7070Spatrick   // If this isn't a 'special' type, it must have a single interface-like base.
1999e5dd7070Spatrick   if (getNumBases() != 1)
2000e5dd7070Spatrick     return false;
2001e5dd7070Spatrick 
2002e5dd7070Spatrick   const auto BaseSpec = *bases_begin();
2003e5dd7070Spatrick   if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
2004e5dd7070Spatrick     return false;
2005e5dd7070Spatrick   const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
2006e5dd7070Spatrick   if (Base->isInterface() || !Base->isInterfaceLike())
2007e5dd7070Spatrick     return false;
2008e5dd7070Spatrick   return true;
2009e5dd7070Spatrick }
2010e5dd7070Spatrick 
completeDefinition()2011e5dd7070Spatrick void CXXRecordDecl::completeDefinition() {
2012e5dd7070Spatrick   completeDefinition(nullptr);
2013e5dd7070Spatrick }
2014e5dd7070Spatrick 
completeDefinition(CXXFinalOverriderMap * FinalOverriders)2015e5dd7070Spatrick void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
2016e5dd7070Spatrick   RecordDecl::completeDefinition();
2017e5dd7070Spatrick 
2018e5dd7070Spatrick   // If the class may be abstract (but hasn't been marked as such), check for
2019e5dd7070Spatrick   // any pure final overriders.
2020e5dd7070Spatrick   if (mayBeAbstract()) {
2021e5dd7070Spatrick     CXXFinalOverriderMap MyFinalOverriders;
2022e5dd7070Spatrick     if (!FinalOverriders) {
2023e5dd7070Spatrick       getFinalOverriders(MyFinalOverriders);
2024e5dd7070Spatrick       FinalOverriders = &MyFinalOverriders;
2025e5dd7070Spatrick     }
2026e5dd7070Spatrick 
2027e5dd7070Spatrick     bool Done = false;
2028e5dd7070Spatrick     for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
2029e5dd7070Spatrick                                      MEnd = FinalOverriders->end();
2030e5dd7070Spatrick          M != MEnd && !Done; ++M) {
2031e5dd7070Spatrick       for (OverridingMethods::iterator SO = M->second.begin(),
2032e5dd7070Spatrick                                     SOEnd = M->second.end();
2033e5dd7070Spatrick            SO != SOEnd && !Done; ++SO) {
2034e5dd7070Spatrick         assert(SO->second.size() > 0 &&
2035e5dd7070Spatrick                "All virtual functions have overriding virtual functions");
2036e5dd7070Spatrick 
2037e5dd7070Spatrick         // C++ [class.abstract]p4:
2038e5dd7070Spatrick         //   A class is abstract if it contains or inherits at least one
2039e5dd7070Spatrick         //   pure virtual function for which the final overrider is pure
2040e5dd7070Spatrick         //   virtual.
2041e5dd7070Spatrick         if (SO->second.front().Method->isPure()) {
2042e5dd7070Spatrick           data().Abstract = true;
2043e5dd7070Spatrick           Done = true;
2044e5dd7070Spatrick           break;
2045e5dd7070Spatrick         }
2046e5dd7070Spatrick       }
2047e5dd7070Spatrick     }
2048e5dd7070Spatrick   }
2049e5dd7070Spatrick 
2050e5dd7070Spatrick   // Set access bits correctly on the directly-declared conversions.
2051e5dd7070Spatrick   for (conversion_iterator I = conversion_begin(), E = conversion_end();
2052e5dd7070Spatrick        I != E; ++I)
2053e5dd7070Spatrick     I.setAccess((*I)->getAccess());
2054e5dd7070Spatrick }
2055e5dd7070Spatrick 
mayBeAbstract() const2056e5dd7070Spatrick bool CXXRecordDecl::mayBeAbstract() const {
2057e5dd7070Spatrick   if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
2058e5dd7070Spatrick       isDependentContext())
2059e5dd7070Spatrick     return false;
2060e5dd7070Spatrick 
2061e5dd7070Spatrick   for (const auto &B : bases()) {
2062e5dd7070Spatrick     const auto *BaseDecl =
2063e5dd7070Spatrick         cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
2064e5dd7070Spatrick     if (BaseDecl->isAbstract())
2065e5dd7070Spatrick       return true;
2066e5dd7070Spatrick   }
2067e5dd7070Spatrick 
2068e5dd7070Spatrick   return false;
2069e5dd7070Spatrick }
2070e5dd7070Spatrick 
isEffectivelyFinal() const2071ec727ea7Spatrick bool CXXRecordDecl::isEffectivelyFinal() const {
2072ec727ea7Spatrick   auto *Def = getDefinition();
2073ec727ea7Spatrick   if (!Def)
2074ec727ea7Spatrick     return false;
2075ec727ea7Spatrick   if (Def->hasAttr<FinalAttr>())
2076ec727ea7Spatrick     return true;
2077ec727ea7Spatrick   if (const auto *Dtor = Def->getDestructor())
2078ec727ea7Spatrick     if (Dtor->hasAttr<FinalAttr>())
2079ec727ea7Spatrick       return true;
2080ec727ea7Spatrick   return false;
2081ec727ea7Spatrick }
2082ec727ea7Spatrick 
anchor()2083e5dd7070Spatrick void CXXDeductionGuideDecl::anchor() {}
2084e5dd7070Spatrick 
isEquivalent(const ExplicitSpecifier Other) const2085e5dd7070Spatrick bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other) const {
2086e5dd7070Spatrick   if ((getKind() != Other.getKind() ||
2087e5dd7070Spatrick        getKind() == ExplicitSpecKind::Unresolved)) {
2088e5dd7070Spatrick     if (getKind() == ExplicitSpecKind::Unresolved &&
2089e5dd7070Spatrick         Other.getKind() == ExplicitSpecKind::Unresolved) {
2090e5dd7070Spatrick       ODRHash SelfHash, OtherHash;
2091e5dd7070Spatrick       SelfHash.AddStmt(getExpr());
2092e5dd7070Spatrick       OtherHash.AddStmt(Other.getExpr());
2093e5dd7070Spatrick       return SelfHash.CalculateHash() == OtherHash.CalculateHash();
2094e5dd7070Spatrick     } else
2095e5dd7070Spatrick       return false;
2096e5dd7070Spatrick   }
2097e5dd7070Spatrick   return true;
2098e5dd7070Spatrick }
2099e5dd7070Spatrick 
getFromDecl(FunctionDecl * Function)2100e5dd7070Spatrick ExplicitSpecifier ExplicitSpecifier::getFromDecl(FunctionDecl *Function) {
2101e5dd7070Spatrick   switch (Function->getDeclKind()) {
2102e5dd7070Spatrick   case Decl::Kind::CXXConstructor:
2103e5dd7070Spatrick     return cast<CXXConstructorDecl>(Function)->getExplicitSpecifier();
2104e5dd7070Spatrick   case Decl::Kind::CXXConversion:
2105e5dd7070Spatrick     return cast<CXXConversionDecl>(Function)->getExplicitSpecifier();
2106e5dd7070Spatrick   case Decl::Kind::CXXDeductionGuide:
2107e5dd7070Spatrick     return cast<CXXDeductionGuideDecl>(Function)->getExplicitSpecifier();
2108e5dd7070Spatrick   default:
2109e5dd7070Spatrick     return {};
2110e5dd7070Spatrick   }
2111e5dd7070Spatrick }
2112e5dd7070Spatrick 
2113a9ac8606Spatrick CXXDeductionGuideDecl *
Create(ASTContext & C,DeclContext * DC,SourceLocation StartLoc,ExplicitSpecifier ES,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,SourceLocation EndLocation,CXXConstructorDecl * Ctor)2114a9ac8606Spatrick CXXDeductionGuideDecl::Create(ASTContext &C, DeclContext *DC,
2115a9ac8606Spatrick                               SourceLocation StartLoc, ExplicitSpecifier ES,
2116a9ac8606Spatrick                               const DeclarationNameInfo &NameInfo, QualType T,
2117a9ac8606Spatrick                               TypeSourceInfo *TInfo, SourceLocation EndLocation,
2118a9ac8606Spatrick                               CXXConstructorDecl *Ctor) {
2119e5dd7070Spatrick   return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, ES, NameInfo, T,
2120a9ac8606Spatrick                                            TInfo, EndLocation, Ctor);
2121e5dd7070Spatrick }
2122e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2123e5dd7070Spatrick CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
2124e5dd7070Spatrick                                                                  unsigned ID) {
2125e5dd7070Spatrick   return new (C, ID) CXXDeductionGuideDecl(
2126e5dd7070Spatrick       C, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
2127a9ac8606Spatrick       QualType(), nullptr, SourceLocation(), nullptr);
2128e5dd7070Spatrick }
2129e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation StartLoc)2130e5dd7070Spatrick RequiresExprBodyDecl *RequiresExprBodyDecl::Create(
2131e5dd7070Spatrick     ASTContext &C, DeclContext *DC, SourceLocation StartLoc) {
2132e5dd7070Spatrick   return new (C, DC) RequiresExprBodyDecl(C, DC, StartLoc);
2133e5dd7070Spatrick }
2134e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2135e5dd7070Spatrick RequiresExprBodyDecl *RequiresExprBodyDecl::CreateDeserialized(ASTContext &C,
2136e5dd7070Spatrick                                                                unsigned ID) {
2137e5dd7070Spatrick   return new (C, ID) RequiresExprBodyDecl(C, nullptr, SourceLocation());
2138e5dd7070Spatrick }
2139e5dd7070Spatrick 
anchor()2140e5dd7070Spatrick void CXXMethodDecl::anchor() {}
2141e5dd7070Spatrick 
isStatic() const2142e5dd7070Spatrick bool CXXMethodDecl::isStatic() const {
2143e5dd7070Spatrick   const CXXMethodDecl *MD = getCanonicalDecl();
2144e5dd7070Spatrick 
2145e5dd7070Spatrick   if (MD->getStorageClass() == SC_Static)
2146e5dd7070Spatrick     return true;
2147e5dd7070Spatrick 
2148e5dd7070Spatrick   OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
2149e5dd7070Spatrick   return isStaticOverloadedOperator(OOK);
2150e5dd7070Spatrick }
2151e5dd7070Spatrick 
recursivelyOverrides(const CXXMethodDecl * DerivedMD,const CXXMethodDecl * BaseMD)2152e5dd7070Spatrick static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
2153e5dd7070Spatrick                                  const CXXMethodDecl *BaseMD) {
2154e5dd7070Spatrick   for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
2155e5dd7070Spatrick     if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
2156e5dd7070Spatrick       return true;
2157e5dd7070Spatrick     if (recursivelyOverrides(MD, BaseMD))
2158e5dd7070Spatrick       return true;
2159e5dd7070Spatrick   }
2160e5dd7070Spatrick   return false;
2161e5dd7070Spatrick }
2162e5dd7070Spatrick 
2163e5dd7070Spatrick CXXMethodDecl *
getCorrespondingMethodDeclaredInClass(const CXXRecordDecl * RD,bool MayBeBase)2164e5dd7070Spatrick CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
2165e5dd7070Spatrick                                                      bool MayBeBase) {
2166e5dd7070Spatrick   if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
2167e5dd7070Spatrick     return this;
2168e5dd7070Spatrick 
2169e5dd7070Spatrick   // Lookup doesn't work for destructors, so handle them separately.
2170e5dd7070Spatrick   if (isa<CXXDestructorDecl>(this)) {
2171e5dd7070Spatrick     CXXMethodDecl *MD = RD->getDestructor();
2172e5dd7070Spatrick     if (MD) {
2173e5dd7070Spatrick       if (recursivelyOverrides(MD, this))
2174e5dd7070Spatrick         return MD;
2175e5dd7070Spatrick       if (MayBeBase && recursivelyOverrides(this, MD))
2176e5dd7070Spatrick         return MD;
2177e5dd7070Spatrick     }
2178e5dd7070Spatrick     return nullptr;
2179e5dd7070Spatrick   }
2180e5dd7070Spatrick 
2181e5dd7070Spatrick   for (auto *ND : RD->lookup(getDeclName())) {
2182e5dd7070Spatrick     auto *MD = dyn_cast<CXXMethodDecl>(ND);
2183e5dd7070Spatrick     if (!MD)
2184e5dd7070Spatrick       continue;
2185e5dd7070Spatrick     if (recursivelyOverrides(MD, this))
2186e5dd7070Spatrick       return MD;
2187e5dd7070Spatrick     if (MayBeBase && recursivelyOverrides(this, MD))
2188e5dd7070Spatrick       return MD;
2189e5dd7070Spatrick   }
2190e5dd7070Spatrick 
2191e5dd7070Spatrick   return nullptr;
2192e5dd7070Spatrick }
2193e5dd7070Spatrick 
2194e5dd7070Spatrick CXXMethodDecl *
getCorrespondingMethodInClass(const CXXRecordDecl * RD,bool MayBeBase)2195e5dd7070Spatrick CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2196e5dd7070Spatrick                                              bool MayBeBase) {
2197e5dd7070Spatrick   if (auto *MD = getCorrespondingMethodDeclaredInClass(RD, MayBeBase))
2198e5dd7070Spatrick     return MD;
2199e5dd7070Spatrick 
2200e5dd7070Spatrick   llvm::SmallVector<CXXMethodDecl*, 4> FinalOverriders;
2201e5dd7070Spatrick   auto AddFinalOverrider = [&](CXXMethodDecl *D) {
2202e5dd7070Spatrick     // If this function is overridden by a candidate final overrider, it is not
2203e5dd7070Spatrick     // a final overrider.
2204e5dd7070Spatrick     for (CXXMethodDecl *OtherD : FinalOverriders) {
2205e5dd7070Spatrick       if (declaresSameEntity(D, OtherD) || recursivelyOverrides(OtherD, D))
2206e5dd7070Spatrick         return;
2207e5dd7070Spatrick     }
2208e5dd7070Spatrick 
2209e5dd7070Spatrick     // Other candidate final overriders might be overridden by this function.
2210*12c85518Srobert     llvm::erase_if(FinalOverriders, [&](CXXMethodDecl *OtherD) {
2211e5dd7070Spatrick       return recursivelyOverrides(D, OtherD);
2212*12c85518Srobert     });
2213e5dd7070Spatrick 
2214e5dd7070Spatrick     FinalOverriders.push_back(D);
2215e5dd7070Spatrick   };
2216e5dd7070Spatrick 
2217e5dd7070Spatrick   for (const auto &I : RD->bases()) {
2218e5dd7070Spatrick     const RecordType *RT = I.getType()->getAs<RecordType>();
2219e5dd7070Spatrick     if (!RT)
2220e5dd7070Spatrick       continue;
2221e5dd7070Spatrick     const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
2222e5dd7070Spatrick     if (CXXMethodDecl *D = this->getCorrespondingMethodInClass(Base))
2223e5dd7070Spatrick       AddFinalOverrider(D);
2224e5dd7070Spatrick   }
2225e5dd7070Spatrick 
2226e5dd7070Spatrick   return FinalOverriders.size() == 1 ? FinalOverriders.front() : nullptr;
2227e5dd7070Spatrick }
2228e5dd7070Spatrick 
2229*12c85518Srobert CXXMethodDecl *
Create(ASTContext & C,CXXRecordDecl * RD,SourceLocation StartLoc,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,StorageClass SC,bool UsesFPIntrin,bool isInline,ConstexprSpecKind ConstexprKind,SourceLocation EndLocation,Expr * TrailingRequiresClause)2230*12c85518Srobert CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2231*12c85518Srobert                       const DeclarationNameInfo &NameInfo, QualType T,
2232*12c85518Srobert                       TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin,
2233*12c85518Srobert                       bool isInline, ConstexprSpecKind ConstexprKind,
2234e5dd7070Spatrick                       SourceLocation EndLocation,
2235e5dd7070Spatrick                       Expr *TrailingRequiresClause) {
2236*12c85518Srobert   return new (C, RD) CXXMethodDecl(
2237*12c85518Srobert       CXXMethod, C, RD, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin,
2238*12c85518Srobert       isInline, ConstexprKind, EndLocation, TrailingRequiresClause);
2239e5dd7070Spatrick }
2240e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2241e5dd7070Spatrick CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2242*12c85518Srobert   return new (C, ID) CXXMethodDecl(
2243*12c85518Srobert       CXXMethod, C, nullptr, SourceLocation(), DeclarationNameInfo(),
2244*12c85518Srobert       QualType(), nullptr, SC_None, false, false,
2245a9ac8606Spatrick       ConstexprSpecKind::Unspecified, SourceLocation(), nullptr);
2246e5dd7070Spatrick }
2247e5dd7070Spatrick 
getDevirtualizedMethod(const Expr * Base,bool IsAppleKext)2248e5dd7070Spatrick CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
2249e5dd7070Spatrick                                                      bool IsAppleKext) {
2250e5dd7070Spatrick   assert(isVirtual() && "this method is expected to be virtual");
2251e5dd7070Spatrick 
2252e5dd7070Spatrick   // When building with -fapple-kext, all calls must go through the vtable since
2253e5dd7070Spatrick   // the kernel linker can do runtime patching of vtables.
2254e5dd7070Spatrick   if (IsAppleKext)
2255e5dd7070Spatrick     return nullptr;
2256e5dd7070Spatrick 
2257e5dd7070Spatrick   // If the member function is marked 'final', we know that it can't be
2258e5dd7070Spatrick   // overridden and can therefore devirtualize it unless it's pure virtual.
2259e5dd7070Spatrick   if (hasAttr<FinalAttr>())
2260e5dd7070Spatrick     return isPure() ? nullptr : this;
2261e5dd7070Spatrick 
2262e5dd7070Spatrick   // If Base is unknown, we cannot devirtualize.
2263e5dd7070Spatrick   if (!Base)
2264e5dd7070Spatrick     return nullptr;
2265e5dd7070Spatrick 
2266e5dd7070Spatrick   // If the base expression (after skipping derived-to-base conversions) is a
2267e5dd7070Spatrick   // class prvalue, then we can devirtualize.
2268e5dd7070Spatrick   Base = Base->getBestDynamicClassTypeExpr();
2269a9ac8606Spatrick   if (Base->isPRValue() && Base->getType()->isRecordType())
2270e5dd7070Spatrick     return this;
2271e5dd7070Spatrick 
2272e5dd7070Spatrick   // If we don't even know what we would call, we can't devirtualize.
2273e5dd7070Spatrick   const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
2274e5dd7070Spatrick   if (!BestDynamicDecl)
2275e5dd7070Spatrick     return nullptr;
2276e5dd7070Spatrick 
2277e5dd7070Spatrick   // There may be a method corresponding to MD in a derived class.
2278e5dd7070Spatrick   CXXMethodDecl *DevirtualizedMethod =
2279e5dd7070Spatrick       getCorrespondingMethodInClass(BestDynamicDecl);
2280e5dd7070Spatrick 
2281e5dd7070Spatrick   // If there final overrider in the dynamic type is ambiguous, we can't
2282e5dd7070Spatrick   // devirtualize this call.
2283e5dd7070Spatrick   if (!DevirtualizedMethod)
2284e5dd7070Spatrick     return nullptr;
2285e5dd7070Spatrick 
2286e5dd7070Spatrick   // If that method is pure virtual, we can't devirtualize. If this code is
2287e5dd7070Spatrick   // reached, the result would be UB, not a direct call to the derived class
2288e5dd7070Spatrick   // function, and we can't assume the derived class function is defined.
2289e5dd7070Spatrick   if (DevirtualizedMethod->isPure())
2290e5dd7070Spatrick     return nullptr;
2291e5dd7070Spatrick 
2292e5dd7070Spatrick   // If that method is marked final, we can devirtualize it.
2293e5dd7070Spatrick   if (DevirtualizedMethod->hasAttr<FinalAttr>())
2294e5dd7070Spatrick     return DevirtualizedMethod;
2295e5dd7070Spatrick 
2296e5dd7070Spatrick   // Similarly, if the class itself or its destructor is marked 'final',
2297e5dd7070Spatrick   // the class can't be derived from and we can therefore devirtualize the
2298e5dd7070Spatrick   // member function call.
2299ec727ea7Spatrick   if (BestDynamicDecl->isEffectivelyFinal())
2300e5dd7070Spatrick     return DevirtualizedMethod;
2301e5dd7070Spatrick 
2302e5dd7070Spatrick   if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2303e5dd7070Spatrick     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
2304e5dd7070Spatrick       if (VD->getType()->isRecordType())
2305e5dd7070Spatrick         // This is a record decl. We know the type and can devirtualize it.
2306e5dd7070Spatrick         return DevirtualizedMethod;
2307e5dd7070Spatrick 
2308e5dd7070Spatrick     return nullptr;
2309e5dd7070Spatrick   }
2310e5dd7070Spatrick 
2311e5dd7070Spatrick   // We can devirtualize calls on an object accessed by a class member access
2312e5dd7070Spatrick   // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2313e5dd7070Spatrick   // a derived class object constructed in the same location.
2314e5dd7070Spatrick   if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
2315e5dd7070Spatrick     const ValueDecl *VD = ME->getMemberDecl();
2316e5dd7070Spatrick     return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2317e5dd7070Spatrick   }
2318e5dd7070Spatrick 
2319e5dd7070Spatrick   // Likewise for calls on an object accessed by a (non-reference) pointer to
2320e5dd7070Spatrick   // member access.
2321e5dd7070Spatrick   if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2322e5dd7070Spatrick     if (BO->isPtrMemOp()) {
2323e5dd7070Spatrick       auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2324e5dd7070Spatrick       if (MPT->getPointeeType()->isRecordType())
2325e5dd7070Spatrick         return DevirtualizedMethod;
2326e5dd7070Spatrick     }
2327e5dd7070Spatrick   }
2328e5dd7070Spatrick 
2329e5dd7070Spatrick   // We can't devirtualize the call.
2330e5dd7070Spatrick   return nullptr;
2331e5dd7070Spatrick }
2332e5dd7070Spatrick 
isUsualDeallocationFunction(SmallVectorImpl<const FunctionDecl * > & PreventedBy) const2333e5dd7070Spatrick bool CXXMethodDecl::isUsualDeallocationFunction(
2334e5dd7070Spatrick     SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2335e5dd7070Spatrick   assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
2336e5dd7070Spatrick   if (getOverloadedOperator() != OO_Delete &&
2337e5dd7070Spatrick       getOverloadedOperator() != OO_Array_Delete)
2338e5dd7070Spatrick     return false;
2339e5dd7070Spatrick 
2340e5dd7070Spatrick   // C++ [basic.stc.dynamic.deallocation]p2:
2341e5dd7070Spatrick   //   A template instance is never a usual deallocation function,
2342e5dd7070Spatrick   //   regardless of its signature.
2343e5dd7070Spatrick   if (getPrimaryTemplate())
2344e5dd7070Spatrick     return false;
2345e5dd7070Spatrick 
2346e5dd7070Spatrick   // C++ [basic.stc.dynamic.deallocation]p2:
2347e5dd7070Spatrick   //   If a class T has a member deallocation function named operator delete
2348e5dd7070Spatrick   //   with exactly one parameter, then that function is a usual (non-placement)
2349e5dd7070Spatrick   //   deallocation function. [...]
2350e5dd7070Spatrick   if (getNumParams() == 1)
2351e5dd7070Spatrick     return true;
2352e5dd7070Spatrick   unsigned UsualParams = 1;
2353e5dd7070Spatrick 
2354e5dd7070Spatrick   // C++ P0722:
2355e5dd7070Spatrick   //   A destroying operator delete is a usual deallocation function if
2356e5dd7070Spatrick   //   removing the std::destroying_delete_t parameter and changing the
2357e5dd7070Spatrick   //   first parameter type from T* to void* results in the signature of
2358e5dd7070Spatrick   //   a usual deallocation function.
2359e5dd7070Spatrick   if (isDestroyingOperatorDelete())
2360e5dd7070Spatrick     ++UsualParams;
2361e5dd7070Spatrick 
2362e5dd7070Spatrick   // C++ <=14 [basic.stc.dynamic.deallocation]p2:
2363e5dd7070Spatrick   //   [...] If class T does not declare such an operator delete but does
2364e5dd7070Spatrick   //   declare a member deallocation function named operator delete with
2365e5dd7070Spatrick   //   exactly two parameters, the second of which has type std::size_t (18.1),
2366e5dd7070Spatrick   //   then this function is a usual deallocation function.
2367e5dd7070Spatrick   //
2368e5dd7070Spatrick   // C++17 says a usual deallocation function is one with the signature
2369e5dd7070Spatrick   //   (void* [, size_t] [, std::align_val_t] [, ...])
2370e5dd7070Spatrick   // and all such functions are usual deallocation functions. It's not clear
2371e5dd7070Spatrick   // that allowing varargs functions was intentional.
2372e5dd7070Spatrick   ASTContext &Context = getASTContext();
2373e5dd7070Spatrick   if (UsualParams < getNumParams() &&
2374e5dd7070Spatrick       Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2375e5dd7070Spatrick                                      Context.getSizeType()))
2376e5dd7070Spatrick     ++UsualParams;
2377e5dd7070Spatrick 
2378e5dd7070Spatrick   if (UsualParams < getNumParams() &&
2379e5dd7070Spatrick       getParamDecl(UsualParams)->getType()->isAlignValT())
2380e5dd7070Spatrick     ++UsualParams;
2381e5dd7070Spatrick 
2382e5dd7070Spatrick   if (UsualParams != getNumParams())
2383e5dd7070Spatrick     return false;
2384e5dd7070Spatrick 
2385e5dd7070Spatrick   // In C++17 onwards, all potential usual deallocation functions are actual
2386e5dd7070Spatrick   // usual deallocation functions. Honor this behavior when post-C++14
2387e5dd7070Spatrick   // deallocation functions are offered as extensions too.
2388*12c85518Srobert   // FIXME(EricWF): Destroying Delete should be a language option. How do we
2389e5dd7070Spatrick   // handle when destroying delete is used prior to C++17?
2390e5dd7070Spatrick   if (Context.getLangOpts().CPlusPlus17 ||
2391e5dd7070Spatrick       Context.getLangOpts().AlignedAllocation ||
2392e5dd7070Spatrick       isDestroyingOperatorDelete())
2393e5dd7070Spatrick     return true;
2394e5dd7070Spatrick 
2395e5dd7070Spatrick   // This function is a usual deallocation function if there are no
2396e5dd7070Spatrick   // single-parameter deallocation functions of the same kind.
2397e5dd7070Spatrick   DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
2398e5dd7070Spatrick   bool Result = true;
2399e5dd7070Spatrick   for (const auto *D : R) {
2400e5dd7070Spatrick     if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2401e5dd7070Spatrick       if (FD->getNumParams() == 1) {
2402e5dd7070Spatrick         PreventedBy.push_back(FD);
2403e5dd7070Spatrick         Result = false;
2404e5dd7070Spatrick       }
2405e5dd7070Spatrick     }
2406e5dd7070Spatrick   }
2407e5dd7070Spatrick   return Result;
2408e5dd7070Spatrick }
2409e5dd7070Spatrick 
isCopyAssignmentOperator() const2410e5dd7070Spatrick bool CXXMethodDecl::isCopyAssignmentOperator() const {
2411e5dd7070Spatrick   // C++0x [class.copy]p17:
2412e5dd7070Spatrick   //  A user-declared copy assignment operator X::operator= is a non-static
2413e5dd7070Spatrick   //  non-template member function of class X with exactly one parameter of
2414e5dd7070Spatrick   //  type X, X&, const X&, volatile X& or const volatile X&.
2415e5dd7070Spatrick   if (/*operator=*/getOverloadedOperator() != OO_Equal ||
2416e5dd7070Spatrick       /*non-static*/ isStatic() ||
2417e5dd7070Spatrick       /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2418e5dd7070Spatrick       getNumParams() != 1)
2419e5dd7070Spatrick     return false;
2420e5dd7070Spatrick 
2421e5dd7070Spatrick   QualType ParamType = getParamDecl(0)->getType();
2422e5dd7070Spatrick   if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
2423e5dd7070Spatrick     ParamType = Ref->getPointeeType();
2424e5dd7070Spatrick 
2425e5dd7070Spatrick   ASTContext &Context = getASTContext();
2426e5dd7070Spatrick   QualType ClassType
2427e5dd7070Spatrick     = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2428e5dd7070Spatrick   return Context.hasSameUnqualifiedType(ClassType, ParamType);
2429e5dd7070Spatrick }
2430e5dd7070Spatrick 
isMoveAssignmentOperator() const2431e5dd7070Spatrick bool CXXMethodDecl::isMoveAssignmentOperator() const {
2432e5dd7070Spatrick   // C++0x [class.copy]p19:
2433e5dd7070Spatrick   //  A user-declared move assignment operator X::operator= is a non-static
2434e5dd7070Spatrick   //  non-template member function of class X with exactly one parameter of type
2435e5dd7070Spatrick   //  X&&, const X&&, volatile X&&, or const volatile X&&.
2436e5dd7070Spatrick   if (getOverloadedOperator() != OO_Equal || isStatic() ||
2437e5dd7070Spatrick       getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2438e5dd7070Spatrick       getNumParams() != 1)
2439e5dd7070Spatrick     return false;
2440e5dd7070Spatrick 
2441e5dd7070Spatrick   QualType ParamType = getParamDecl(0)->getType();
2442*12c85518Srobert   if (!ParamType->isRValueReferenceType())
2443e5dd7070Spatrick     return false;
2444e5dd7070Spatrick   ParamType = ParamType->getPointeeType();
2445e5dd7070Spatrick 
2446e5dd7070Spatrick   ASTContext &Context = getASTContext();
2447e5dd7070Spatrick   QualType ClassType
2448e5dd7070Spatrick     = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2449e5dd7070Spatrick   return Context.hasSameUnqualifiedType(ClassType, ParamType);
2450e5dd7070Spatrick }
2451e5dd7070Spatrick 
addOverriddenMethod(const CXXMethodDecl * MD)2452e5dd7070Spatrick void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
2453e5dd7070Spatrick   assert(MD->isCanonicalDecl() && "Method is not canonical!");
2454e5dd7070Spatrick   assert(!MD->getParent()->isDependentContext() &&
2455e5dd7070Spatrick          "Can't add an overridden method to a class template!");
2456e5dd7070Spatrick   assert(MD->isVirtual() && "Method is not virtual!");
2457e5dd7070Spatrick 
2458e5dd7070Spatrick   getASTContext().addOverriddenMethod(this, MD);
2459e5dd7070Spatrick }
2460e5dd7070Spatrick 
begin_overridden_methods() const2461e5dd7070Spatrick CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
2462e5dd7070Spatrick   if (isa<CXXConstructorDecl>(this)) return nullptr;
2463e5dd7070Spatrick   return getASTContext().overridden_methods_begin(this);
2464e5dd7070Spatrick }
2465e5dd7070Spatrick 
end_overridden_methods() const2466e5dd7070Spatrick CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
2467e5dd7070Spatrick   if (isa<CXXConstructorDecl>(this)) return nullptr;
2468e5dd7070Spatrick   return getASTContext().overridden_methods_end(this);
2469e5dd7070Spatrick }
2470e5dd7070Spatrick 
size_overridden_methods() const2471e5dd7070Spatrick unsigned CXXMethodDecl::size_overridden_methods() const {
2472e5dd7070Spatrick   if (isa<CXXConstructorDecl>(this)) return 0;
2473e5dd7070Spatrick   return getASTContext().overridden_methods_size(this);
2474e5dd7070Spatrick }
2475e5dd7070Spatrick 
2476e5dd7070Spatrick CXXMethodDecl::overridden_method_range
overridden_methods() const2477e5dd7070Spatrick CXXMethodDecl::overridden_methods() const {
2478e5dd7070Spatrick   if (isa<CXXConstructorDecl>(this))
2479e5dd7070Spatrick     return overridden_method_range(nullptr, nullptr);
2480e5dd7070Spatrick   return getASTContext().overridden_methods(this);
2481e5dd7070Spatrick }
2482e5dd7070Spatrick 
getThisObjectType(ASTContext & C,const FunctionProtoType * FPT,const CXXRecordDecl * Decl)2483e5dd7070Spatrick static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2484e5dd7070Spatrick                                   const CXXRecordDecl *Decl) {
2485e5dd7070Spatrick   QualType ClassTy = C.getTypeDeclType(Decl);
2486e5dd7070Spatrick   return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2487e5dd7070Spatrick }
2488e5dd7070Spatrick 
getThisType(const FunctionProtoType * FPT,const CXXRecordDecl * Decl)2489e5dd7070Spatrick QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2490e5dd7070Spatrick                                     const CXXRecordDecl *Decl) {
2491e5dd7070Spatrick   ASTContext &C = Decl->getASTContext();
2492e5dd7070Spatrick   QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2493e5dd7070Spatrick   return C.getPointerType(ObjectTy);
2494e5dd7070Spatrick }
2495e5dd7070Spatrick 
getThisObjectType(const FunctionProtoType * FPT,const CXXRecordDecl * Decl)2496e5dd7070Spatrick QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2497e5dd7070Spatrick                                           const CXXRecordDecl *Decl) {
2498e5dd7070Spatrick   ASTContext &C = Decl->getASTContext();
2499e5dd7070Spatrick   return ::getThisObjectType(C, FPT, Decl);
2500e5dd7070Spatrick }
2501e5dd7070Spatrick 
getThisType() const2502e5dd7070Spatrick QualType CXXMethodDecl::getThisType() const {
2503e5dd7070Spatrick   // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2504e5dd7070Spatrick   // If the member function is declared const, the type of this is const X*,
2505e5dd7070Spatrick   // if the member function is declared volatile, the type of this is
2506e5dd7070Spatrick   // volatile X*, and if the member function is declared const volatile,
2507e5dd7070Spatrick   // the type of this is const volatile X*.
2508e5dd7070Spatrick   assert(isInstance() && "No 'this' for static methods!");
2509ec727ea7Spatrick   return CXXMethodDecl::getThisType(getType()->castAs<FunctionProtoType>(),
2510e5dd7070Spatrick                                     getParent());
2511e5dd7070Spatrick }
2512e5dd7070Spatrick 
getThisObjectType() const2513e5dd7070Spatrick QualType CXXMethodDecl::getThisObjectType() const {
2514e5dd7070Spatrick   // Ditto getThisType.
2515e5dd7070Spatrick   assert(isInstance() && "No 'this' for static methods!");
2516ec727ea7Spatrick   return CXXMethodDecl::getThisObjectType(
2517ec727ea7Spatrick       getType()->castAs<FunctionProtoType>(), getParent());
2518e5dd7070Spatrick }
2519e5dd7070Spatrick 
hasInlineBody() const2520e5dd7070Spatrick bool CXXMethodDecl::hasInlineBody() const {
2521e5dd7070Spatrick   // If this function is a template instantiation, look at the template from
2522e5dd7070Spatrick   // which it was instantiated.
2523e5dd7070Spatrick   const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2524e5dd7070Spatrick   if (!CheckFn)
2525e5dd7070Spatrick     CheckFn = this;
2526e5dd7070Spatrick 
2527e5dd7070Spatrick   const FunctionDecl *fn;
2528e5dd7070Spatrick   return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2529e5dd7070Spatrick          (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
2530e5dd7070Spatrick }
2531e5dd7070Spatrick 
isLambdaStaticInvoker() const2532e5dd7070Spatrick bool CXXMethodDecl::isLambdaStaticInvoker() const {
2533e5dd7070Spatrick   const CXXRecordDecl *P = getParent();
2534a9ac8606Spatrick   return P->isLambda() && getDeclName().isIdentifier() &&
2535a9ac8606Spatrick          getName() == getLambdaStaticInvokerName();
2536e5dd7070Spatrick }
2537e5dd7070Spatrick 
CXXCtorInitializer(ASTContext & Context,TypeSourceInfo * TInfo,bool IsVirtual,SourceLocation L,Expr * Init,SourceLocation R,SourceLocation EllipsisLoc)2538e5dd7070Spatrick CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2539e5dd7070Spatrick                                        TypeSourceInfo *TInfo, bool IsVirtual,
2540e5dd7070Spatrick                                        SourceLocation L, Expr *Init,
2541e5dd7070Spatrick                                        SourceLocation R,
2542e5dd7070Spatrick                                        SourceLocation EllipsisLoc)
2543a9ac8606Spatrick     : Initializee(TInfo), Init(Init), MemberOrEllipsisLocation(EllipsisLoc),
2544e5dd7070Spatrick       LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2545e5dd7070Spatrick       IsWritten(false), SourceOrder(0) {}
2546e5dd7070Spatrick 
CXXCtorInitializer(ASTContext & Context,FieldDecl * Member,SourceLocation MemberLoc,SourceLocation L,Expr * Init,SourceLocation R)2547a9ac8606Spatrick CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
2548e5dd7070Spatrick                                        SourceLocation MemberLoc,
2549e5dd7070Spatrick                                        SourceLocation L, Expr *Init,
2550e5dd7070Spatrick                                        SourceLocation R)
2551a9ac8606Spatrick     : Initializee(Member), Init(Init), MemberOrEllipsisLocation(MemberLoc),
2552e5dd7070Spatrick       LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2553e5dd7070Spatrick       IsWritten(false), SourceOrder(0) {}
2554e5dd7070Spatrick 
CXXCtorInitializer(ASTContext & Context,IndirectFieldDecl * Member,SourceLocation MemberLoc,SourceLocation L,Expr * Init,SourceLocation R)2555e5dd7070Spatrick CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2556e5dd7070Spatrick                                        IndirectFieldDecl *Member,
2557e5dd7070Spatrick                                        SourceLocation MemberLoc,
2558e5dd7070Spatrick                                        SourceLocation L, Expr *Init,
2559e5dd7070Spatrick                                        SourceLocation R)
2560a9ac8606Spatrick     : Initializee(Member), Init(Init), MemberOrEllipsisLocation(MemberLoc),
2561e5dd7070Spatrick       LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2562e5dd7070Spatrick       IsWritten(false), SourceOrder(0) {}
2563e5dd7070Spatrick 
CXXCtorInitializer(ASTContext & Context,TypeSourceInfo * TInfo,SourceLocation L,Expr * Init,SourceLocation R)2564e5dd7070Spatrick CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2565e5dd7070Spatrick                                        TypeSourceInfo *TInfo,
2566e5dd7070Spatrick                                        SourceLocation L, Expr *Init,
2567e5dd7070Spatrick                                        SourceLocation R)
2568e5dd7070Spatrick     : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2569e5dd7070Spatrick       IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
2570e5dd7070Spatrick 
getID(const ASTContext & Context) const2571e5dd7070Spatrick int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
2572e5dd7070Spatrick   return Context.getAllocator()
2573e5dd7070Spatrick                 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
2574e5dd7070Spatrick }
2575e5dd7070Spatrick 
getBaseClassLoc() const2576e5dd7070Spatrick TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
2577e5dd7070Spatrick   if (isBaseInitializer())
2578e5dd7070Spatrick     return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
2579e5dd7070Spatrick   else
2580e5dd7070Spatrick     return {};
2581e5dd7070Spatrick }
2582e5dd7070Spatrick 
getBaseClass() const2583e5dd7070Spatrick const Type *CXXCtorInitializer::getBaseClass() const {
2584e5dd7070Spatrick   if (isBaseInitializer())
2585e5dd7070Spatrick     return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
2586e5dd7070Spatrick   else
2587e5dd7070Spatrick     return nullptr;
2588e5dd7070Spatrick }
2589e5dd7070Spatrick 
getSourceLocation() const2590e5dd7070Spatrick SourceLocation CXXCtorInitializer::getSourceLocation() const {
2591e5dd7070Spatrick   if (isInClassMemberInitializer())
2592e5dd7070Spatrick     return getAnyMember()->getLocation();
2593e5dd7070Spatrick 
2594e5dd7070Spatrick   if (isAnyMemberInitializer())
2595e5dd7070Spatrick     return getMemberLocation();
2596e5dd7070Spatrick 
2597e5dd7070Spatrick   if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
2598*12c85518Srobert     return TSInfo->getTypeLoc().getBeginLoc();
2599e5dd7070Spatrick 
2600e5dd7070Spatrick   return {};
2601e5dd7070Spatrick }
2602e5dd7070Spatrick 
getSourceRange() const2603e5dd7070Spatrick SourceRange CXXCtorInitializer::getSourceRange() const {
2604e5dd7070Spatrick   if (isInClassMemberInitializer()) {
2605e5dd7070Spatrick     FieldDecl *D = getAnyMember();
2606e5dd7070Spatrick     if (Expr *I = D->getInClassInitializer())
2607e5dd7070Spatrick       return I->getSourceRange();
2608e5dd7070Spatrick     return {};
2609e5dd7070Spatrick   }
2610e5dd7070Spatrick 
2611e5dd7070Spatrick   return SourceRange(getSourceLocation(), getRParenLoc());
2612e5dd7070Spatrick }
2613e5dd7070Spatrick 
CXXConstructorDecl(ASTContext & C,CXXRecordDecl * RD,SourceLocation StartLoc,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,ExplicitSpecifier ES,bool UsesFPIntrin,bool isInline,bool isImplicitlyDeclared,ConstexprSpecKind ConstexprKind,InheritedConstructor Inherited,Expr * TrailingRequiresClause)2614e5dd7070Spatrick CXXConstructorDecl::CXXConstructorDecl(
2615e5dd7070Spatrick     ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2616e5dd7070Spatrick     const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2617*12c85518Srobert     ExplicitSpecifier ES, bool UsesFPIntrin, bool isInline,
2618*12c85518Srobert     bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
2619*12c85518Srobert     InheritedConstructor Inherited, Expr *TrailingRequiresClause)
2620e5dd7070Spatrick     : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2621*12c85518Srobert                     SC_None, UsesFPIntrin, isInline, ConstexprKind,
2622*12c85518Srobert                     SourceLocation(), TrailingRequiresClause) {
2623e5dd7070Spatrick   setNumCtorInitializers(0);
2624e5dd7070Spatrick   setInheritingConstructor(static_cast<bool>(Inherited));
2625e5dd7070Spatrick   setImplicit(isImplicitlyDeclared);
2626e5dd7070Spatrick   CXXConstructorDeclBits.HasTrailingExplicitSpecifier = ES.getExpr() ? 1 : 0;
2627e5dd7070Spatrick   if (Inherited)
2628e5dd7070Spatrick     *getTrailingObjects<InheritedConstructor>() = Inherited;
2629e5dd7070Spatrick   setExplicitSpecifier(ES);
2630e5dd7070Spatrick }
2631e5dd7070Spatrick 
anchor()2632e5dd7070Spatrick void CXXConstructorDecl::anchor() {}
2633e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID,uint64_t AllocKind)2634e5dd7070Spatrick CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2635e5dd7070Spatrick                                                            unsigned ID,
2636e5dd7070Spatrick                                                            uint64_t AllocKind) {
2637a9ac8606Spatrick   bool hasTrailingExplicit = static_cast<bool>(AllocKind & TAKHasTailExplicit);
2638e5dd7070Spatrick   bool isInheritingConstructor =
2639e5dd7070Spatrick       static_cast<bool>(AllocKind & TAKInheritsConstructor);
2640e5dd7070Spatrick   unsigned Extra =
2641e5dd7070Spatrick       additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2642a9ac8606Spatrick           isInheritingConstructor, hasTrailingExplicit);
2643a9ac8606Spatrick   auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2644a9ac8606Spatrick       C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2645*12c85518Srobert       ExplicitSpecifier(), false, false, false, ConstexprSpecKind::Unspecified,
2646a9ac8606Spatrick       InheritedConstructor(), nullptr);
2647e5dd7070Spatrick   Result->setInheritingConstructor(isInheritingConstructor);
2648e5dd7070Spatrick   Result->CXXConstructorDeclBits.HasTrailingExplicitSpecifier =
2649a9ac8606Spatrick       hasTrailingExplicit;
2650e5dd7070Spatrick   Result->setExplicitSpecifier(ExplicitSpecifier());
2651e5dd7070Spatrick   return Result;
2652e5dd7070Spatrick }
2653e5dd7070Spatrick 
Create(ASTContext & C,CXXRecordDecl * RD,SourceLocation StartLoc,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,ExplicitSpecifier ES,bool UsesFPIntrin,bool isInline,bool isImplicitlyDeclared,ConstexprSpecKind ConstexprKind,InheritedConstructor Inherited,Expr * TrailingRequiresClause)2654e5dd7070Spatrick CXXConstructorDecl *CXXConstructorDecl::Create(
2655e5dd7070Spatrick     ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2656e5dd7070Spatrick     const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2657*12c85518Srobert     ExplicitSpecifier ES, bool UsesFPIntrin, bool isInline,
2658*12c85518Srobert     bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
2659*12c85518Srobert     InheritedConstructor Inherited, Expr *TrailingRequiresClause) {
2660e5dd7070Spatrick   assert(NameInfo.getName().getNameKind()
2661e5dd7070Spatrick          == DeclarationName::CXXConstructorName &&
2662e5dd7070Spatrick          "Name must refer to a constructor");
2663e5dd7070Spatrick   unsigned Extra =
2664e5dd7070Spatrick       additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2665e5dd7070Spatrick           Inherited ? 1 : 0, ES.getExpr() ? 1 : 0);
2666*12c85518Srobert   return new (C, RD, Extra) CXXConstructorDecl(
2667*12c85518Srobert       C, RD, StartLoc, NameInfo, T, TInfo, ES, UsesFPIntrin, isInline,
2668*12c85518Srobert       isImplicitlyDeclared, ConstexprKind, Inherited, TrailingRequiresClause);
2669e5dd7070Spatrick }
2670e5dd7070Spatrick 
init_begin() const2671e5dd7070Spatrick CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2672e5dd7070Spatrick   return CtorInitializers.get(getASTContext().getExternalSource());
2673e5dd7070Spatrick }
2674e5dd7070Spatrick 
getTargetConstructor() const2675e5dd7070Spatrick CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2676e5dd7070Spatrick   assert(isDelegatingConstructor() && "Not a delegating constructor!");
2677e5dd7070Spatrick   Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
2678e5dd7070Spatrick   if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
2679e5dd7070Spatrick     return Construct->getConstructor();
2680e5dd7070Spatrick 
2681e5dd7070Spatrick   return nullptr;
2682e5dd7070Spatrick }
2683e5dd7070Spatrick 
isDefaultConstructor() const2684e5dd7070Spatrick bool CXXConstructorDecl::isDefaultConstructor() const {
2685ec727ea7Spatrick   // C++ [class.default.ctor]p1:
2686ec727ea7Spatrick   //   A default constructor for a class X is a constructor of class X for
2687ec727ea7Spatrick   //   which each parameter that is not a function parameter pack has a default
2688ec727ea7Spatrick   //   argument (including the case of a constructor with no parameters)
2689ec727ea7Spatrick   return getMinRequiredArguments() == 0;
2690e5dd7070Spatrick }
2691e5dd7070Spatrick 
2692e5dd7070Spatrick bool
isCopyConstructor(unsigned & TypeQuals) const2693e5dd7070Spatrick CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
2694e5dd7070Spatrick   return isCopyOrMoveConstructor(TypeQuals) &&
2695e5dd7070Spatrick          getParamDecl(0)->getType()->isLValueReferenceType();
2696e5dd7070Spatrick }
2697e5dd7070Spatrick 
isMoveConstructor(unsigned & TypeQuals) const2698e5dd7070Spatrick bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2699e5dd7070Spatrick   return isCopyOrMoveConstructor(TypeQuals) &&
2700e5dd7070Spatrick          getParamDecl(0)->getType()->isRValueReferenceType();
2701e5dd7070Spatrick }
2702e5dd7070Spatrick 
2703e5dd7070Spatrick /// Determine whether this is a copy or move constructor.
isCopyOrMoveConstructor(unsigned & TypeQuals) const2704e5dd7070Spatrick bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
2705e5dd7070Spatrick   // C++ [class.copy]p2:
2706e5dd7070Spatrick   //   A non-template constructor for class X is a copy constructor
2707e5dd7070Spatrick   //   if its first parameter is of type X&, const X&, volatile X& or
2708e5dd7070Spatrick   //   const volatile X&, and either there are no other parameters
2709e5dd7070Spatrick   //   or else all other parameters have default arguments (8.3.6).
2710e5dd7070Spatrick   // C++0x [class.copy]p3:
2711e5dd7070Spatrick   //   A non-template constructor for class X is a move constructor if its
2712e5dd7070Spatrick   //   first parameter is of type X&&, const X&&, volatile X&&, or
2713e5dd7070Spatrick   //   const volatile X&&, and either there are no other parameters or else
2714e5dd7070Spatrick   //   all other parameters have default arguments.
2715ec727ea7Spatrick   if (!hasOneParamOrDefaultArgs() || getPrimaryTemplate() != nullptr ||
2716ec727ea7Spatrick       getDescribedFunctionTemplate() != nullptr)
2717e5dd7070Spatrick     return false;
2718e5dd7070Spatrick 
2719e5dd7070Spatrick   const ParmVarDecl *Param = getParamDecl(0);
2720e5dd7070Spatrick 
2721e5dd7070Spatrick   // Do we have a reference type?
2722e5dd7070Spatrick   const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
2723e5dd7070Spatrick   if (!ParamRefType)
2724e5dd7070Spatrick     return false;
2725e5dd7070Spatrick 
2726e5dd7070Spatrick   // Is it a reference to our class type?
2727e5dd7070Spatrick   ASTContext &Context = getASTContext();
2728e5dd7070Spatrick 
2729e5dd7070Spatrick   CanQualType PointeeType
2730e5dd7070Spatrick     = Context.getCanonicalType(ParamRefType->getPointeeType());
2731e5dd7070Spatrick   CanQualType ClassTy
2732e5dd7070Spatrick     = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2733e5dd7070Spatrick   if (PointeeType.getUnqualifiedType() != ClassTy)
2734e5dd7070Spatrick     return false;
2735e5dd7070Spatrick 
2736e5dd7070Spatrick   // FIXME: other qualifiers?
2737e5dd7070Spatrick 
2738e5dd7070Spatrick   // We have a copy or move constructor.
2739e5dd7070Spatrick   TypeQuals = PointeeType.getCVRQualifiers();
2740e5dd7070Spatrick   return true;
2741e5dd7070Spatrick }
2742e5dd7070Spatrick 
isConvertingConstructor(bool AllowExplicit) const2743e5dd7070Spatrick bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
2744e5dd7070Spatrick   // C++ [class.conv.ctor]p1:
2745e5dd7070Spatrick   //   A constructor declared without the function-specifier explicit
2746e5dd7070Spatrick   //   that can be called with a single parameter specifies a
2747e5dd7070Spatrick   //   conversion from the type of its first parameter to the type of
2748e5dd7070Spatrick   //   its class. Such a constructor is called a converting
2749e5dd7070Spatrick   //   constructor.
2750e5dd7070Spatrick   if (isExplicit() && !AllowExplicit)
2751e5dd7070Spatrick     return false;
2752e5dd7070Spatrick 
2753ec727ea7Spatrick   // FIXME: This has nothing to do with the definition of converting
2754ec727ea7Spatrick   // constructor, but is convenient for how we use this function in overload
2755ec727ea7Spatrick   // resolution.
2756ec727ea7Spatrick   return getNumParams() == 0
2757ec727ea7Spatrick              ? getType()->castAs<FunctionProtoType>()->isVariadic()
2758ec727ea7Spatrick              : getMinRequiredArguments() <= 1;
2759e5dd7070Spatrick }
2760e5dd7070Spatrick 
isSpecializationCopyingObject() const2761e5dd7070Spatrick bool CXXConstructorDecl::isSpecializationCopyingObject() const {
2762ec727ea7Spatrick   if (!hasOneParamOrDefaultArgs() || getDescribedFunctionTemplate() != nullptr)
2763e5dd7070Spatrick     return false;
2764e5dd7070Spatrick 
2765e5dd7070Spatrick   const ParmVarDecl *Param = getParamDecl(0);
2766e5dd7070Spatrick 
2767e5dd7070Spatrick   ASTContext &Context = getASTContext();
2768e5dd7070Spatrick   CanQualType ParamType = Context.getCanonicalType(Param->getType());
2769e5dd7070Spatrick 
2770e5dd7070Spatrick   // Is it the same as our class type?
2771e5dd7070Spatrick   CanQualType ClassTy
2772e5dd7070Spatrick     = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2773e5dd7070Spatrick   if (ParamType.getUnqualifiedType() != ClassTy)
2774e5dd7070Spatrick     return false;
2775e5dd7070Spatrick 
2776e5dd7070Spatrick   return true;
2777e5dd7070Spatrick }
2778e5dd7070Spatrick 
anchor()2779e5dd7070Spatrick void CXXDestructorDecl::anchor() {}
2780e5dd7070Spatrick 
2781e5dd7070Spatrick CXXDestructorDecl *
CreateDeserialized(ASTContext & C,unsigned ID)2782e5dd7070Spatrick CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2783a9ac8606Spatrick   return new (C, ID) CXXDestructorDecl(
2784a9ac8606Spatrick       C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2785*12c85518Srobert       false, false, false, ConstexprSpecKind::Unspecified, nullptr);
2786e5dd7070Spatrick }
2787e5dd7070Spatrick 
Create(ASTContext & C,CXXRecordDecl * RD,SourceLocation StartLoc,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,bool UsesFPIntrin,bool isInline,bool isImplicitlyDeclared,ConstexprSpecKind ConstexprKind,Expr * TrailingRequiresClause)2788e5dd7070Spatrick CXXDestructorDecl *CXXDestructorDecl::Create(
2789e5dd7070Spatrick     ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2790e5dd7070Spatrick     const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2791*12c85518Srobert     bool UsesFPIntrin, bool isInline, bool isImplicitlyDeclared,
2792*12c85518Srobert     ConstexprSpecKind ConstexprKind, Expr *TrailingRequiresClause) {
2793e5dd7070Spatrick   assert(NameInfo.getName().getNameKind()
2794e5dd7070Spatrick          == DeclarationName::CXXDestructorName &&
2795e5dd7070Spatrick          "Name must refer to a destructor");
2796*12c85518Srobert   return new (C, RD) CXXDestructorDecl(
2797*12c85518Srobert       C, RD, StartLoc, NameInfo, T, TInfo, UsesFPIntrin, isInline,
2798*12c85518Srobert       isImplicitlyDeclared, ConstexprKind, TrailingRequiresClause);
2799e5dd7070Spatrick }
2800e5dd7070Spatrick 
setOperatorDelete(FunctionDecl * OD,Expr * ThisArg)2801e5dd7070Spatrick void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
2802e5dd7070Spatrick   auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2803e5dd7070Spatrick   if (OD && !First->OperatorDelete) {
2804e5dd7070Spatrick     First->OperatorDelete = OD;
2805e5dd7070Spatrick     First->OperatorDeleteThisArg = ThisArg;
2806e5dd7070Spatrick     if (auto *L = getASTMutationListener())
2807e5dd7070Spatrick       L->ResolvedOperatorDelete(First, OD, ThisArg);
2808e5dd7070Spatrick   }
2809e5dd7070Spatrick }
2810e5dd7070Spatrick 
anchor()2811e5dd7070Spatrick void CXXConversionDecl::anchor() {}
2812e5dd7070Spatrick 
2813e5dd7070Spatrick CXXConversionDecl *
CreateDeserialized(ASTContext & C,unsigned ID)2814e5dd7070Spatrick CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2815e5dd7070Spatrick   return new (C, ID) CXXConversionDecl(
2816e5dd7070Spatrick       C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2817*12c85518Srobert       false, false, ExplicitSpecifier(), ConstexprSpecKind::Unspecified,
2818a9ac8606Spatrick       SourceLocation(), nullptr);
2819e5dd7070Spatrick }
2820e5dd7070Spatrick 
Create(ASTContext & C,CXXRecordDecl * RD,SourceLocation StartLoc,const DeclarationNameInfo & NameInfo,QualType T,TypeSourceInfo * TInfo,bool UsesFPIntrin,bool isInline,ExplicitSpecifier ES,ConstexprSpecKind ConstexprKind,SourceLocation EndLocation,Expr * TrailingRequiresClause)2821e5dd7070Spatrick CXXConversionDecl *CXXConversionDecl::Create(
2822e5dd7070Spatrick     ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2823e5dd7070Spatrick     const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2824*12c85518Srobert     bool UsesFPIntrin, bool isInline, ExplicitSpecifier ES,
2825*12c85518Srobert     ConstexprSpecKind ConstexprKind, SourceLocation EndLocation,
2826*12c85518Srobert     Expr *TrailingRequiresClause) {
2827e5dd7070Spatrick   assert(NameInfo.getName().getNameKind()
2828e5dd7070Spatrick          == DeclarationName::CXXConversionFunctionName &&
2829e5dd7070Spatrick          "Name must refer to a conversion function");
2830*12c85518Srobert   return new (C, RD) CXXConversionDecl(
2831*12c85518Srobert       C, RD, StartLoc, NameInfo, T, TInfo, UsesFPIntrin, isInline, ES,
2832e5dd7070Spatrick       ConstexprKind, EndLocation, TrailingRequiresClause);
2833e5dd7070Spatrick }
2834e5dd7070Spatrick 
isLambdaToBlockPointerConversion() const2835e5dd7070Spatrick bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2836e5dd7070Spatrick   return isImplicit() && getParent()->isLambda() &&
2837e5dd7070Spatrick          getConversionType()->isBlockPointerType();
2838e5dd7070Spatrick }
2839e5dd7070Spatrick 
LinkageSpecDecl(DeclContext * DC,SourceLocation ExternLoc,SourceLocation LangLoc,LanguageIDs lang,bool HasBraces)2840e5dd7070Spatrick LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2841e5dd7070Spatrick                                  SourceLocation LangLoc, LanguageIDs lang,
2842e5dd7070Spatrick                                  bool HasBraces)
2843e5dd7070Spatrick     : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2844e5dd7070Spatrick       ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2845e5dd7070Spatrick   setLanguage(lang);
2846e5dd7070Spatrick   LinkageSpecDeclBits.HasBraces = HasBraces;
2847e5dd7070Spatrick }
2848e5dd7070Spatrick 
anchor()2849e5dd7070Spatrick void LinkageSpecDecl::anchor() {}
2850e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation ExternLoc,SourceLocation LangLoc,LanguageIDs Lang,bool HasBraces)2851e5dd7070Spatrick LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
2852e5dd7070Spatrick                                          DeclContext *DC,
2853e5dd7070Spatrick                                          SourceLocation ExternLoc,
2854e5dd7070Spatrick                                          SourceLocation LangLoc,
2855e5dd7070Spatrick                                          LanguageIDs Lang,
2856e5dd7070Spatrick                                          bool HasBraces) {
2857e5dd7070Spatrick   return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
2858e5dd7070Spatrick }
2859e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2860e5dd7070Spatrick LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2861e5dd7070Spatrick                                                      unsigned ID) {
2862e5dd7070Spatrick   return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2863e5dd7070Spatrick                                      SourceLocation(), lang_c, false);
2864e5dd7070Spatrick }
2865e5dd7070Spatrick 
anchor()2866e5dd7070Spatrick void UsingDirectiveDecl::anchor() {}
2867e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation L,SourceLocation NamespaceLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation IdentLoc,NamedDecl * Used,DeclContext * CommonAncestor)2868e5dd7070Spatrick UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2869e5dd7070Spatrick                                                SourceLocation L,
2870e5dd7070Spatrick                                                SourceLocation NamespaceLoc,
2871e5dd7070Spatrick                                            NestedNameSpecifierLoc QualifierLoc,
2872e5dd7070Spatrick                                                SourceLocation IdentLoc,
2873e5dd7070Spatrick                                                NamedDecl *Used,
2874e5dd7070Spatrick                                                DeclContext *CommonAncestor) {
2875e5dd7070Spatrick   if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2876e5dd7070Spatrick     Used = NS->getOriginalNamespace();
2877e5dd7070Spatrick   return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2878e5dd7070Spatrick                                         IdentLoc, Used, CommonAncestor);
2879e5dd7070Spatrick }
2880e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2881e5dd7070Spatrick UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2882e5dd7070Spatrick                                                            unsigned ID) {
2883e5dd7070Spatrick   return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2884e5dd7070Spatrick                                         SourceLocation(),
2885e5dd7070Spatrick                                         NestedNameSpecifierLoc(),
2886e5dd7070Spatrick                                         SourceLocation(), nullptr, nullptr);
2887e5dd7070Spatrick }
2888e5dd7070Spatrick 
getNominatedNamespace()2889e5dd7070Spatrick NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2890e5dd7070Spatrick   if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2891e5dd7070Spatrick     return NA->getNamespace();
2892e5dd7070Spatrick   return cast_or_null<NamespaceDecl>(NominatedNamespace);
2893e5dd7070Spatrick }
2894e5dd7070Spatrick 
NamespaceDecl(ASTContext & C,DeclContext * DC,bool Inline,SourceLocation StartLoc,SourceLocation IdLoc,IdentifierInfo * Id,NamespaceDecl * PrevDecl,bool Nested)2895e5dd7070Spatrick NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2896e5dd7070Spatrick                              SourceLocation StartLoc, SourceLocation IdLoc,
2897*12c85518Srobert                              IdentifierInfo *Id, NamespaceDecl *PrevDecl,
2898*12c85518Srobert                              bool Nested)
2899e5dd7070Spatrick     : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2900*12c85518Srobert       redeclarable_base(C), LocStart(StartLoc) {
2901*12c85518Srobert   unsigned Flags = 0;
2902*12c85518Srobert   if (Inline)
2903*12c85518Srobert     Flags |= F_Inline;
2904*12c85518Srobert   if (Nested)
2905*12c85518Srobert     Flags |= F_Nested;
2906*12c85518Srobert   AnonOrFirstNamespaceAndFlags = {nullptr, Flags};
2907e5dd7070Spatrick   setPreviousDecl(PrevDecl);
2908e5dd7070Spatrick 
2909e5dd7070Spatrick   if (PrevDecl)
2910*12c85518Srobert     AnonOrFirstNamespaceAndFlags.setPointer(PrevDecl->getOriginalNamespace());
2911e5dd7070Spatrick }
2912e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,bool Inline,SourceLocation StartLoc,SourceLocation IdLoc,IdentifierInfo * Id,NamespaceDecl * PrevDecl,bool Nested)2913e5dd7070Spatrick NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2914e5dd7070Spatrick                                      bool Inline, SourceLocation StartLoc,
2915e5dd7070Spatrick                                      SourceLocation IdLoc, IdentifierInfo *Id,
2916*12c85518Srobert                                      NamespaceDecl *PrevDecl, bool Nested) {
2917*12c85518Srobert   return new (C, DC)
2918*12c85518Srobert       NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id, PrevDecl, Nested);
2919e5dd7070Spatrick }
2920e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)2921e5dd7070Spatrick NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2922e5dd7070Spatrick   return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
2923*12c85518Srobert                                    SourceLocation(), nullptr, nullptr, false);
2924e5dd7070Spatrick }
2925e5dd7070Spatrick 
getOriginalNamespace()2926e5dd7070Spatrick NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2927e5dd7070Spatrick   if (isFirstDecl())
2928e5dd7070Spatrick     return this;
2929e5dd7070Spatrick 
2930*12c85518Srobert   return AnonOrFirstNamespaceAndFlags.getPointer();
2931e5dd7070Spatrick }
2932e5dd7070Spatrick 
getOriginalNamespace() const2933e5dd7070Spatrick const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2934e5dd7070Spatrick   if (isFirstDecl())
2935e5dd7070Spatrick     return this;
2936e5dd7070Spatrick 
2937*12c85518Srobert   return AnonOrFirstNamespaceAndFlags.getPointer();
2938e5dd7070Spatrick }
2939e5dd7070Spatrick 
isOriginalNamespace() const2940e5dd7070Spatrick bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2941e5dd7070Spatrick 
getNextRedeclarationImpl()2942e5dd7070Spatrick NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2943e5dd7070Spatrick   return getNextRedeclaration();
2944e5dd7070Spatrick }
2945e5dd7070Spatrick 
getPreviousDeclImpl()2946e5dd7070Spatrick NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2947e5dd7070Spatrick   return getPreviousDecl();
2948e5dd7070Spatrick }
2949e5dd7070Spatrick 
getMostRecentDeclImpl()2950e5dd7070Spatrick NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2951e5dd7070Spatrick   return getMostRecentDecl();
2952e5dd7070Spatrick }
2953e5dd7070Spatrick 
anchor()2954e5dd7070Spatrick void NamespaceAliasDecl::anchor() {}
2955e5dd7070Spatrick 
getNextRedeclarationImpl()2956e5dd7070Spatrick NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2957e5dd7070Spatrick   return getNextRedeclaration();
2958e5dd7070Spatrick }
2959e5dd7070Spatrick 
getPreviousDeclImpl()2960e5dd7070Spatrick NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2961e5dd7070Spatrick   return getPreviousDecl();
2962e5dd7070Spatrick }
2963e5dd7070Spatrick 
getMostRecentDeclImpl()2964e5dd7070Spatrick NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2965e5dd7070Spatrick   return getMostRecentDecl();
2966e5dd7070Spatrick }
2967e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation UsingLoc,SourceLocation AliasLoc,IdentifierInfo * Alias,NestedNameSpecifierLoc QualifierLoc,SourceLocation IdentLoc,NamedDecl * Namespace)2968e5dd7070Spatrick NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
2969e5dd7070Spatrick                                                SourceLocation UsingLoc,
2970e5dd7070Spatrick                                                SourceLocation AliasLoc,
2971e5dd7070Spatrick                                                IdentifierInfo *Alias,
2972e5dd7070Spatrick                                            NestedNameSpecifierLoc QualifierLoc,
2973e5dd7070Spatrick                                                SourceLocation IdentLoc,
2974e5dd7070Spatrick                                                NamedDecl *Namespace) {
2975e5dd7070Spatrick   // FIXME: Preserve the aliased namespace as written.
2976e5dd7070Spatrick   if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2977e5dd7070Spatrick     Namespace = NS->getOriginalNamespace();
2978e5dd7070Spatrick   return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
2979e5dd7070Spatrick                                         QualifierLoc, IdentLoc, Namespace);
2980e5dd7070Spatrick }
2981e5dd7070Spatrick 
2982e5dd7070Spatrick NamespaceAliasDecl *
CreateDeserialized(ASTContext & C,unsigned ID)2983e5dd7070Spatrick NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2984e5dd7070Spatrick   return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
2985e5dd7070Spatrick                                         SourceLocation(), nullptr,
2986e5dd7070Spatrick                                         NestedNameSpecifierLoc(),
2987e5dd7070Spatrick                                         SourceLocation(), nullptr);
2988e5dd7070Spatrick }
2989e5dd7070Spatrick 
anchor()2990e5dd7070Spatrick void LifetimeExtendedTemporaryDecl::anchor() {}
2991e5dd7070Spatrick 
2992e5dd7070Spatrick /// Retrieve the storage duration for the materialized temporary.
getStorageDuration() const2993e5dd7070Spatrick StorageDuration LifetimeExtendedTemporaryDecl::getStorageDuration() const {
2994e5dd7070Spatrick   const ValueDecl *ExtendingDecl = getExtendingDecl();
2995e5dd7070Spatrick   if (!ExtendingDecl)
2996e5dd7070Spatrick     return SD_FullExpression;
2997e5dd7070Spatrick   // FIXME: This is not necessarily correct for a temporary materialized
2998e5dd7070Spatrick   // within a default initializer.
2999e5dd7070Spatrick   if (isa<FieldDecl>(ExtendingDecl))
3000e5dd7070Spatrick     return SD_Automatic;
3001e5dd7070Spatrick   // FIXME: This only works because storage class specifiers are not allowed
3002e5dd7070Spatrick   // on decomposition declarations.
3003e5dd7070Spatrick   if (isa<BindingDecl>(ExtendingDecl))
3004e5dd7070Spatrick     return ExtendingDecl->getDeclContext()->isFunctionOrMethod() ? SD_Automatic
3005e5dd7070Spatrick                                                                  : SD_Static;
3006e5dd7070Spatrick   return cast<VarDecl>(ExtendingDecl)->getStorageDuration();
3007e5dd7070Spatrick }
3008e5dd7070Spatrick 
getOrCreateValue(bool MayCreate) const3009e5dd7070Spatrick APValue *LifetimeExtendedTemporaryDecl::getOrCreateValue(bool MayCreate) const {
3010e5dd7070Spatrick   assert(getStorageDuration() == SD_Static &&
3011e5dd7070Spatrick          "don't need to cache the computed value for this temporary");
3012e5dd7070Spatrick   if (MayCreate && !Value) {
3013e5dd7070Spatrick     Value = (new (getASTContext()) APValue);
3014e5dd7070Spatrick     getASTContext().addDestruction(Value);
3015e5dd7070Spatrick   }
3016e5dd7070Spatrick   assert(Value && "may not be null");
3017e5dd7070Spatrick   return Value;
3018e5dd7070Spatrick }
3019e5dd7070Spatrick 
anchor()3020e5dd7070Spatrick void UsingShadowDecl::anchor() {}
3021e5dd7070Spatrick 
UsingShadowDecl(Kind K,ASTContext & C,DeclContext * DC,SourceLocation Loc,DeclarationName Name,BaseUsingDecl * Introducer,NamedDecl * Target)3022e5dd7070Spatrick UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
3023a9ac8606Spatrick                                  SourceLocation Loc, DeclarationName Name,
3024a9ac8606Spatrick                                  BaseUsingDecl *Introducer, NamedDecl *Target)
3025a9ac8606Spatrick     : NamedDecl(K, DC, Loc, Name), redeclarable_base(C),
3026a9ac8606Spatrick       UsingOrNextShadow(Introducer) {
3027*12c85518Srobert   if (Target) {
3028*12c85518Srobert     assert(!isa<UsingShadowDecl>(Target));
3029e5dd7070Spatrick     setTargetDecl(Target);
3030*12c85518Srobert   }
3031e5dd7070Spatrick   setImplicit();
3032e5dd7070Spatrick }
3033e5dd7070Spatrick 
UsingShadowDecl(Kind K,ASTContext & C,EmptyShell Empty)3034e5dd7070Spatrick UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
3035e5dd7070Spatrick     : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
3036e5dd7070Spatrick       redeclarable_base(C) {}
3037e5dd7070Spatrick 
3038e5dd7070Spatrick UsingShadowDecl *
CreateDeserialized(ASTContext & C,unsigned ID)3039e5dd7070Spatrick UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3040e5dd7070Spatrick   return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
3041e5dd7070Spatrick }
3042e5dd7070Spatrick 
getIntroducer() const3043a9ac8606Spatrick BaseUsingDecl *UsingShadowDecl::getIntroducer() const {
3044e5dd7070Spatrick   const UsingShadowDecl *Shadow = this;
3045e5dd7070Spatrick   while (const auto *NextShadow =
3046e5dd7070Spatrick              dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
3047e5dd7070Spatrick     Shadow = NextShadow;
3048a9ac8606Spatrick   return cast<BaseUsingDecl>(Shadow->UsingOrNextShadow);
3049e5dd7070Spatrick }
3050e5dd7070Spatrick 
anchor()3051e5dd7070Spatrick void ConstructorUsingShadowDecl::anchor() {}
3052e5dd7070Spatrick 
3053e5dd7070Spatrick ConstructorUsingShadowDecl *
Create(ASTContext & C,DeclContext * DC,SourceLocation Loc,UsingDecl * Using,NamedDecl * Target,bool IsVirtual)3054e5dd7070Spatrick ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
3055e5dd7070Spatrick                                    SourceLocation Loc, UsingDecl *Using,
3056e5dd7070Spatrick                                    NamedDecl *Target, bool IsVirtual) {
3057e5dd7070Spatrick   return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
3058e5dd7070Spatrick                                                 IsVirtual);
3059e5dd7070Spatrick }
3060e5dd7070Spatrick 
3061e5dd7070Spatrick ConstructorUsingShadowDecl *
CreateDeserialized(ASTContext & C,unsigned ID)3062e5dd7070Spatrick ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3063e5dd7070Spatrick   return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
3064e5dd7070Spatrick }
3065e5dd7070Spatrick 
getNominatedBaseClass() const3066e5dd7070Spatrick CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
3067a9ac8606Spatrick   return getIntroducer()->getQualifier()->getAsRecordDecl();
3068e5dd7070Spatrick }
3069e5dd7070Spatrick 
anchor()3070a9ac8606Spatrick void BaseUsingDecl::anchor() {}
3071e5dd7070Spatrick 
addShadowDecl(UsingShadowDecl * S)3072a9ac8606Spatrick void BaseUsingDecl::addShadowDecl(UsingShadowDecl *S) {
3073*12c85518Srobert   assert(!llvm::is_contained(shadows(), S) && "declaration already in set");
3074a9ac8606Spatrick   assert(S->getIntroducer() == this);
3075e5dd7070Spatrick 
3076e5dd7070Spatrick   if (FirstUsingShadow.getPointer())
3077e5dd7070Spatrick     S->UsingOrNextShadow = FirstUsingShadow.getPointer();
3078e5dd7070Spatrick   FirstUsingShadow.setPointer(S);
3079e5dd7070Spatrick }
3080e5dd7070Spatrick 
removeShadowDecl(UsingShadowDecl * S)3081a9ac8606Spatrick void BaseUsingDecl::removeShadowDecl(UsingShadowDecl *S) {
3082*12c85518Srobert   assert(llvm::is_contained(shadows(), S) && "declaration not in set");
3083a9ac8606Spatrick   assert(S->getIntroducer() == this);
3084e5dd7070Spatrick 
3085e5dd7070Spatrick   // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
3086e5dd7070Spatrick 
3087e5dd7070Spatrick   if (FirstUsingShadow.getPointer() == S) {
3088e5dd7070Spatrick     FirstUsingShadow.setPointer(
3089e5dd7070Spatrick       dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
3090e5dd7070Spatrick     S->UsingOrNextShadow = this;
3091e5dd7070Spatrick     return;
3092e5dd7070Spatrick   }
3093e5dd7070Spatrick 
3094e5dd7070Spatrick   UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
3095e5dd7070Spatrick   while (Prev->UsingOrNextShadow != S)
3096e5dd7070Spatrick     Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
3097e5dd7070Spatrick   Prev->UsingOrNextShadow = S->UsingOrNextShadow;
3098e5dd7070Spatrick   S->UsingOrNextShadow = this;
3099e5dd7070Spatrick }
3100e5dd7070Spatrick 
anchor()3101a9ac8606Spatrick void UsingDecl::anchor() {}
3102a9ac8606Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation UL,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,bool HasTypename)3103e5dd7070Spatrick UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
3104e5dd7070Spatrick                              NestedNameSpecifierLoc QualifierLoc,
3105e5dd7070Spatrick                              const DeclarationNameInfo &NameInfo,
3106e5dd7070Spatrick                              bool HasTypename) {
3107e5dd7070Spatrick   return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
3108e5dd7070Spatrick }
3109e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3110e5dd7070Spatrick UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3111e5dd7070Spatrick   return new (C, ID) UsingDecl(nullptr, SourceLocation(),
3112e5dd7070Spatrick                                NestedNameSpecifierLoc(), DeclarationNameInfo(),
3113e5dd7070Spatrick                                false);
3114e5dd7070Spatrick }
3115e5dd7070Spatrick 
getSourceRange() const3116e5dd7070Spatrick SourceRange UsingDecl::getSourceRange() const {
3117e5dd7070Spatrick   SourceLocation Begin = isAccessDeclaration()
3118e5dd7070Spatrick     ? getQualifierLoc().getBeginLoc() : UsingLocation;
3119e5dd7070Spatrick   return SourceRange(Begin, getNameInfo().getEndLoc());
3120e5dd7070Spatrick }
3121e5dd7070Spatrick 
anchor()3122a9ac8606Spatrick void UsingEnumDecl::anchor() {}
3123a9ac8606Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation UL,SourceLocation EL,SourceLocation NL,TypeSourceInfo * EnumType)3124a9ac8606Spatrick UsingEnumDecl *UsingEnumDecl::Create(ASTContext &C, DeclContext *DC,
3125*12c85518Srobert                                      SourceLocation UL,
3126*12c85518Srobert                                      SourceLocation EL,
3127*12c85518Srobert                                      SourceLocation NL,
3128*12c85518Srobert                                      TypeSourceInfo *EnumType) {
3129*12c85518Srobert   assert(isa<EnumDecl>(EnumType->getType()->getAsTagDecl()));
3130*12c85518Srobert   return new (C, DC)
3131*12c85518Srobert       UsingEnumDecl(DC, EnumType->getType()->getAsTagDecl()->getDeclName(), UL, EL, NL, EnumType);
3132a9ac8606Spatrick }
3133a9ac8606Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3134a9ac8606Spatrick UsingEnumDecl *UsingEnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3135*12c85518Srobert   return new (C, ID)
3136*12c85518Srobert       UsingEnumDecl(nullptr, DeclarationName(), SourceLocation(),
3137a9ac8606Spatrick                     SourceLocation(), SourceLocation(), nullptr);
3138a9ac8606Spatrick }
3139a9ac8606Spatrick 
getSourceRange() const3140a9ac8606Spatrick SourceRange UsingEnumDecl::getSourceRange() const {
3141*12c85518Srobert   return SourceRange(UsingLocation, EnumType->getTypeLoc().getEndLoc());
3142a9ac8606Spatrick }
3143a9ac8606Spatrick 
anchor()3144e5dd7070Spatrick void UsingPackDecl::anchor() {}
3145e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,NamedDecl * InstantiatedFrom,ArrayRef<NamedDecl * > UsingDecls)3146e5dd7070Spatrick UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
3147e5dd7070Spatrick                                      NamedDecl *InstantiatedFrom,
3148e5dd7070Spatrick                                      ArrayRef<NamedDecl *> UsingDecls) {
3149e5dd7070Spatrick   size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
3150e5dd7070Spatrick   return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
3151e5dd7070Spatrick }
3152e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID,unsigned NumExpansions)3153e5dd7070Spatrick UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3154e5dd7070Spatrick                                                  unsigned NumExpansions) {
3155e5dd7070Spatrick   size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
3156*12c85518Srobert   auto *Result =
3157*12c85518Srobert       new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, std::nullopt);
3158e5dd7070Spatrick   Result->NumExpansions = NumExpansions;
3159e5dd7070Spatrick   auto *Trail = Result->getTrailingObjects<NamedDecl *>();
3160e5dd7070Spatrick   for (unsigned I = 0; I != NumExpansions; ++I)
3161e5dd7070Spatrick     new (Trail + I) NamedDecl*(nullptr);
3162e5dd7070Spatrick   return Result;
3163e5dd7070Spatrick }
3164e5dd7070Spatrick 
anchor()3165e5dd7070Spatrick void UnresolvedUsingValueDecl::anchor() {}
3166e5dd7070Spatrick 
3167e5dd7070Spatrick UnresolvedUsingValueDecl *
Create(ASTContext & C,DeclContext * DC,SourceLocation UsingLoc,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,SourceLocation EllipsisLoc)3168e5dd7070Spatrick UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
3169e5dd7070Spatrick                                  SourceLocation UsingLoc,
3170e5dd7070Spatrick                                  NestedNameSpecifierLoc QualifierLoc,
3171e5dd7070Spatrick                                  const DeclarationNameInfo &NameInfo,
3172e5dd7070Spatrick                                  SourceLocation EllipsisLoc) {
3173e5dd7070Spatrick   return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
3174e5dd7070Spatrick                                               QualifierLoc, NameInfo,
3175e5dd7070Spatrick                                               EllipsisLoc);
3176e5dd7070Spatrick }
3177e5dd7070Spatrick 
3178e5dd7070Spatrick UnresolvedUsingValueDecl *
CreateDeserialized(ASTContext & C,unsigned ID)3179e5dd7070Spatrick UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3180e5dd7070Spatrick   return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
3181e5dd7070Spatrick                                               SourceLocation(),
3182e5dd7070Spatrick                                               NestedNameSpecifierLoc(),
3183e5dd7070Spatrick                                               DeclarationNameInfo(),
3184e5dd7070Spatrick                                               SourceLocation());
3185e5dd7070Spatrick }
3186e5dd7070Spatrick 
getSourceRange() const3187e5dd7070Spatrick SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
3188e5dd7070Spatrick   SourceLocation Begin = isAccessDeclaration()
3189e5dd7070Spatrick     ? getQualifierLoc().getBeginLoc() : UsingLocation;
3190e5dd7070Spatrick   return SourceRange(Begin, getNameInfo().getEndLoc());
3191e5dd7070Spatrick }
3192e5dd7070Spatrick 
anchor()3193e5dd7070Spatrick void UnresolvedUsingTypenameDecl::anchor() {}
3194e5dd7070Spatrick 
3195e5dd7070Spatrick UnresolvedUsingTypenameDecl *
Create(ASTContext & C,DeclContext * DC,SourceLocation UsingLoc,SourceLocation TypenameLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TargetNameLoc,DeclarationName TargetName,SourceLocation EllipsisLoc)3196e5dd7070Spatrick UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
3197e5dd7070Spatrick                                     SourceLocation UsingLoc,
3198e5dd7070Spatrick                                     SourceLocation TypenameLoc,
3199e5dd7070Spatrick                                     NestedNameSpecifierLoc QualifierLoc,
3200e5dd7070Spatrick                                     SourceLocation TargetNameLoc,
3201e5dd7070Spatrick                                     DeclarationName TargetName,
3202e5dd7070Spatrick                                     SourceLocation EllipsisLoc) {
3203e5dd7070Spatrick   return new (C, DC) UnresolvedUsingTypenameDecl(
3204e5dd7070Spatrick       DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
3205e5dd7070Spatrick       TargetName.getAsIdentifierInfo(), EllipsisLoc);
3206e5dd7070Spatrick }
3207e5dd7070Spatrick 
3208e5dd7070Spatrick UnresolvedUsingTypenameDecl *
CreateDeserialized(ASTContext & C,unsigned ID)3209e5dd7070Spatrick UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3210e5dd7070Spatrick   return new (C, ID) UnresolvedUsingTypenameDecl(
3211e5dd7070Spatrick       nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
3212e5dd7070Spatrick       SourceLocation(), nullptr, SourceLocation());
3213e5dd7070Spatrick }
3214e5dd7070Spatrick 
3215a9ac8606Spatrick UnresolvedUsingIfExistsDecl *
Create(ASTContext & Ctx,DeclContext * DC,SourceLocation Loc,DeclarationName Name)3216a9ac8606Spatrick UnresolvedUsingIfExistsDecl::Create(ASTContext &Ctx, DeclContext *DC,
3217a9ac8606Spatrick                                     SourceLocation Loc, DeclarationName Name) {
3218a9ac8606Spatrick   return new (Ctx, DC) UnresolvedUsingIfExistsDecl(DC, Loc, Name);
3219a9ac8606Spatrick }
3220a9ac8606Spatrick 
3221a9ac8606Spatrick UnresolvedUsingIfExistsDecl *
CreateDeserialized(ASTContext & Ctx,unsigned ID)3222a9ac8606Spatrick UnresolvedUsingIfExistsDecl::CreateDeserialized(ASTContext &Ctx, unsigned ID) {
3223a9ac8606Spatrick   return new (Ctx, ID)
3224a9ac8606Spatrick       UnresolvedUsingIfExistsDecl(nullptr, SourceLocation(), DeclarationName());
3225a9ac8606Spatrick }
3226a9ac8606Spatrick 
UnresolvedUsingIfExistsDecl(DeclContext * DC,SourceLocation Loc,DeclarationName Name)3227a9ac8606Spatrick UnresolvedUsingIfExistsDecl::UnresolvedUsingIfExistsDecl(DeclContext *DC,
3228a9ac8606Spatrick                                                          SourceLocation Loc,
3229a9ac8606Spatrick                                                          DeclarationName Name)
3230a9ac8606Spatrick     : NamedDecl(Decl::UnresolvedUsingIfExists, DC, Loc, Name) {}
3231a9ac8606Spatrick 
anchor()3232a9ac8606Spatrick void UnresolvedUsingIfExistsDecl::anchor() {}
3233a9ac8606Spatrick 
anchor()3234e5dd7070Spatrick void StaticAssertDecl::anchor() {}
3235e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation StaticAssertLoc,Expr * AssertExpr,StringLiteral * Message,SourceLocation RParenLoc,bool Failed)3236e5dd7070Spatrick StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
3237e5dd7070Spatrick                                            SourceLocation StaticAssertLoc,
3238e5dd7070Spatrick                                            Expr *AssertExpr,
3239e5dd7070Spatrick                                            StringLiteral *Message,
3240e5dd7070Spatrick                                            SourceLocation RParenLoc,
3241e5dd7070Spatrick                                            bool Failed) {
3242e5dd7070Spatrick   return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
3243e5dd7070Spatrick                                       RParenLoc, Failed);
3244e5dd7070Spatrick }
3245e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3246e5dd7070Spatrick StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
3247e5dd7070Spatrick                                                        unsigned ID) {
3248e5dd7070Spatrick   return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
3249e5dd7070Spatrick                                       nullptr, SourceLocation(), false);
3250e5dd7070Spatrick }
3251e5dd7070Spatrick 
getPotentiallyDecomposedVarDecl()3252*12c85518Srobert VarDecl *ValueDecl::getPotentiallyDecomposedVarDecl() {
3253*12c85518Srobert   assert((isa<VarDecl, BindingDecl>(this)) &&
3254*12c85518Srobert          "expected a VarDecl or a BindingDecl");
3255*12c85518Srobert   if (auto *Var = llvm::dyn_cast<VarDecl>(this))
3256*12c85518Srobert     return Var;
3257*12c85518Srobert   if (auto *BD = llvm::dyn_cast<BindingDecl>(this))
3258*12c85518Srobert     return llvm::dyn_cast<VarDecl>(BD->getDecomposedDecl());
3259*12c85518Srobert   return nullptr;
3260*12c85518Srobert }
3261*12c85518Srobert 
anchor()3262e5dd7070Spatrick void BindingDecl::anchor() {}
3263e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation IdLoc,IdentifierInfo * Id)3264e5dd7070Spatrick BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
3265e5dd7070Spatrick                                  SourceLocation IdLoc, IdentifierInfo *Id) {
3266e5dd7070Spatrick   return new (C, DC) BindingDecl(DC, IdLoc, Id);
3267e5dd7070Spatrick }
3268e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3269e5dd7070Spatrick BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3270e5dd7070Spatrick   return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
3271e5dd7070Spatrick }
3272e5dd7070Spatrick 
getHoldingVar() const3273e5dd7070Spatrick VarDecl *BindingDecl::getHoldingVar() const {
3274e5dd7070Spatrick   Expr *B = getBinding();
3275e5dd7070Spatrick   if (!B)
3276e5dd7070Spatrick     return nullptr;
3277e5dd7070Spatrick   auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
3278e5dd7070Spatrick   if (!DRE)
3279e5dd7070Spatrick     return nullptr;
3280e5dd7070Spatrick 
3281ec727ea7Spatrick   auto *VD = cast<VarDecl>(DRE->getDecl());
3282e5dd7070Spatrick   assert(VD->isImplicit() && "holding var for binding decl not implicit");
3283e5dd7070Spatrick   return VD;
3284e5dd7070Spatrick }
3285e5dd7070Spatrick 
anchor()3286e5dd7070Spatrick void DecompositionDecl::anchor() {}
3287e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation StartLoc,SourceLocation LSquareLoc,QualType T,TypeSourceInfo * TInfo,StorageClass SC,ArrayRef<BindingDecl * > Bindings)3288e5dd7070Spatrick DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
3289e5dd7070Spatrick                                              SourceLocation StartLoc,
3290e5dd7070Spatrick                                              SourceLocation LSquareLoc,
3291e5dd7070Spatrick                                              QualType T, TypeSourceInfo *TInfo,
3292e5dd7070Spatrick                                              StorageClass SC,
3293e5dd7070Spatrick                                              ArrayRef<BindingDecl *> Bindings) {
3294e5dd7070Spatrick   size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3295e5dd7070Spatrick   return new (C, DC, Extra)
3296e5dd7070Spatrick       DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
3297e5dd7070Spatrick }
3298e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID,unsigned NumBindings)3299e5dd7070Spatrick DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3300e5dd7070Spatrick                                                          unsigned ID,
3301e5dd7070Spatrick                                                          unsigned NumBindings) {
3302e5dd7070Spatrick   size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
3303e5dd7070Spatrick   auto *Result = new (C, ID, Extra)
3304e5dd7070Spatrick       DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3305*12c85518Srobert                         QualType(), nullptr, StorageClass(), std::nullopt);
3306e5dd7070Spatrick   // Set up and clean out the bindings array.
3307e5dd7070Spatrick   Result->NumBindings = NumBindings;
3308e5dd7070Spatrick   auto *Trail = Result->getTrailingObjects<BindingDecl *>();
3309e5dd7070Spatrick   for (unsigned I = 0; I != NumBindings; ++I)
3310e5dd7070Spatrick     new (Trail + I) BindingDecl*(nullptr);
3311e5dd7070Spatrick   return Result;
3312e5dd7070Spatrick }
3313e5dd7070Spatrick 
printName(llvm::raw_ostream & OS,const PrintingPolicy & Policy) const3314*12c85518Srobert void DecompositionDecl::printName(llvm::raw_ostream &OS,
3315*12c85518Srobert                                   const PrintingPolicy &Policy) const {
3316*12c85518Srobert   OS << '[';
3317e5dd7070Spatrick   bool Comma = false;
3318e5dd7070Spatrick   for (const auto *B : bindings()) {
3319e5dd7070Spatrick     if (Comma)
3320*12c85518Srobert       OS << ", ";
3321*12c85518Srobert     B->printName(OS, Policy);
3322e5dd7070Spatrick     Comma = true;
3323e5dd7070Spatrick   }
3324*12c85518Srobert   OS << ']';
3325e5dd7070Spatrick }
3326e5dd7070Spatrick 
anchor()3327e5dd7070Spatrick void MSPropertyDecl::anchor() {}
3328e5dd7070Spatrick 
Create(ASTContext & C,DeclContext * DC,SourceLocation L,DeclarationName N,QualType T,TypeSourceInfo * TInfo,SourceLocation StartL,IdentifierInfo * Getter,IdentifierInfo * Setter)3329e5dd7070Spatrick MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
3330e5dd7070Spatrick                                        SourceLocation L, DeclarationName N,
3331e5dd7070Spatrick                                        QualType T, TypeSourceInfo *TInfo,
3332e5dd7070Spatrick                                        SourceLocation StartL,
3333e5dd7070Spatrick                                        IdentifierInfo *Getter,
3334e5dd7070Spatrick                                        IdentifierInfo *Setter) {
3335e5dd7070Spatrick   return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
3336e5dd7070Spatrick }
3337e5dd7070Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3338e5dd7070Spatrick MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3339e5dd7070Spatrick                                                    unsigned ID) {
3340e5dd7070Spatrick   return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
3341e5dd7070Spatrick                                     DeclarationName(), QualType(), nullptr,
3342e5dd7070Spatrick                                     SourceLocation(), nullptr, nullptr);
3343e5dd7070Spatrick }
3344e5dd7070Spatrick 
anchor()3345ec727ea7Spatrick void MSGuidDecl::anchor() {}
3346ec727ea7Spatrick 
MSGuidDecl(DeclContext * DC,QualType T,Parts P)3347ec727ea7Spatrick MSGuidDecl::MSGuidDecl(DeclContext *DC, QualType T, Parts P)
3348ec727ea7Spatrick     : ValueDecl(Decl::MSGuid, DC, SourceLocation(), DeclarationName(), T),
3349*12c85518Srobert       PartVal(P) {}
3350ec727ea7Spatrick 
Create(const ASTContext & C,QualType T,Parts P)3351ec727ea7Spatrick MSGuidDecl *MSGuidDecl::Create(const ASTContext &C, QualType T, Parts P) {
3352ec727ea7Spatrick   DeclContext *DC = C.getTranslationUnitDecl();
3353ec727ea7Spatrick   return new (C, DC) MSGuidDecl(DC, T, P);
3354ec727ea7Spatrick }
3355ec727ea7Spatrick 
CreateDeserialized(ASTContext & C,unsigned ID)3356ec727ea7Spatrick MSGuidDecl *MSGuidDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3357ec727ea7Spatrick   return new (C, ID) MSGuidDecl(nullptr, QualType(), Parts());
3358ec727ea7Spatrick }
3359ec727ea7Spatrick 
printName(llvm::raw_ostream & OS,const PrintingPolicy &) const3360*12c85518Srobert void MSGuidDecl::printName(llvm::raw_ostream &OS,
3361*12c85518Srobert                            const PrintingPolicy &) const {
3362ec727ea7Spatrick   OS << llvm::format("GUID{%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 "-",
3363ec727ea7Spatrick                      PartVal.Part1, PartVal.Part2, PartVal.Part3);
3364ec727ea7Spatrick   unsigned I = 0;
3365ec727ea7Spatrick   for (uint8_t Byte : PartVal.Part4And5) {
3366ec727ea7Spatrick     OS << llvm::format("%02" PRIx8, Byte);
3367ec727ea7Spatrick     if (++I == 2)
3368ec727ea7Spatrick       OS << '-';
3369ec727ea7Spatrick   }
3370ec727ea7Spatrick   OS << '}';
3371ec727ea7Spatrick }
3372ec727ea7Spatrick 
3373ec727ea7Spatrick /// Determine if T is a valid 'struct _GUID' of the shape that we expect.
isValidStructGUID(ASTContext & Ctx,QualType T)3374ec727ea7Spatrick static bool isValidStructGUID(ASTContext &Ctx, QualType T) {
3375ec727ea7Spatrick   // FIXME: We only need to check this once, not once each time we compute a
3376ec727ea7Spatrick   // GUID APValue.
3377ec727ea7Spatrick   using MatcherRef = llvm::function_ref<bool(QualType)>;
3378ec727ea7Spatrick 
3379ec727ea7Spatrick   auto IsInt = [&Ctx](unsigned N) {
3380ec727ea7Spatrick     return [&Ctx, N](QualType T) {
3381ec727ea7Spatrick       return T->isUnsignedIntegerOrEnumerationType() &&
3382ec727ea7Spatrick              Ctx.getIntWidth(T) == N;
3383ec727ea7Spatrick     };
3384ec727ea7Spatrick   };
3385ec727ea7Spatrick 
3386ec727ea7Spatrick   auto IsArray = [&Ctx](MatcherRef Elem, unsigned N) {
3387ec727ea7Spatrick     return [&Ctx, Elem, N](QualType T) {
3388ec727ea7Spatrick       const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(T);
3389ec727ea7Spatrick       return CAT && CAT->getSize() == N && Elem(CAT->getElementType());
3390ec727ea7Spatrick     };
3391ec727ea7Spatrick   };
3392ec727ea7Spatrick 
3393ec727ea7Spatrick   auto IsStruct = [](std::initializer_list<MatcherRef> Fields) {
3394ec727ea7Spatrick     return [Fields](QualType T) {
3395ec727ea7Spatrick       const RecordDecl *RD = T->getAsRecordDecl();
3396ec727ea7Spatrick       if (!RD || RD->isUnion())
3397ec727ea7Spatrick         return false;
3398ec727ea7Spatrick       RD = RD->getDefinition();
3399ec727ea7Spatrick       if (!RD)
3400ec727ea7Spatrick         return false;
3401ec727ea7Spatrick       if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
3402ec727ea7Spatrick         if (CXXRD->getNumBases())
3403ec727ea7Spatrick           return false;
3404ec727ea7Spatrick       auto MatcherIt = Fields.begin();
3405ec727ea7Spatrick       for (const FieldDecl *FD : RD->fields()) {
3406ec727ea7Spatrick         if (FD->isUnnamedBitfield()) continue;
3407ec727ea7Spatrick         if (FD->isBitField() || MatcherIt == Fields.end() ||
3408ec727ea7Spatrick             !(*MatcherIt)(FD->getType()))
3409ec727ea7Spatrick           return false;
3410ec727ea7Spatrick         ++MatcherIt;
3411ec727ea7Spatrick       }
3412ec727ea7Spatrick       return MatcherIt == Fields.end();
3413ec727ea7Spatrick     };
3414ec727ea7Spatrick   };
3415ec727ea7Spatrick 
3416ec727ea7Spatrick   // We expect an {i32, i16, i16, [8 x i8]}.
3417ec727ea7Spatrick   return IsStruct({IsInt(32), IsInt(16), IsInt(16), IsArray(IsInt(8), 8)})(T);
3418ec727ea7Spatrick }
3419ec727ea7Spatrick 
getAsAPValue() const3420ec727ea7Spatrick APValue &MSGuidDecl::getAsAPValue() const {
3421ec727ea7Spatrick   if (APVal.isAbsent() && isValidStructGUID(getASTContext(), getType())) {
3422ec727ea7Spatrick     using llvm::APInt;
3423ec727ea7Spatrick     using llvm::APSInt;
3424ec727ea7Spatrick     APVal = APValue(APValue::UninitStruct(), 0, 4);
3425ec727ea7Spatrick     APVal.getStructField(0) = APValue(APSInt(APInt(32, PartVal.Part1), true));
3426ec727ea7Spatrick     APVal.getStructField(1) = APValue(APSInt(APInt(16, PartVal.Part2), true));
3427ec727ea7Spatrick     APVal.getStructField(2) = APValue(APSInt(APInt(16, PartVal.Part3), true));
3428ec727ea7Spatrick     APValue &Arr = APVal.getStructField(3) =
3429ec727ea7Spatrick         APValue(APValue::UninitArray(), 8, 8);
3430ec727ea7Spatrick     for (unsigned I = 0; I != 8; ++I) {
3431ec727ea7Spatrick       Arr.getArrayInitializedElt(I) =
3432ec727ea7Spatrick           APValue(APSInt(APInt(8, PartVal.Part4And5[I]), true));
3433ec727ea7Spatrick     }
3434ec727ea7Spatrick     // Register this APValue to be destroyed if necessary. (Note that the
3435ec727ea7Spatrick     // MSGuidDecl destructor is never run.)
3436ec727ea7Spatrick     getASTContext().addDestruction(&APVal);
3437ec727ea7Spatrick   }
3438ec727ea7Spatrick 
3439ec727ea7Spatrick   return APVal;
3440ec727ea7Spatrick }
3441ec727ea7Spatrick 
anchor()3442*12c85518Srobert void UnnamedGlobalConstantDecl::anchor() {}
3443*12c85518Srobert 
UnnamedGlobalConstantDecl(const ASTContext & C,DeclContext * DC,QualType Ty,const APValue & Val)3444*12c85518Srobert UnnamedGlobalConstantDecl::UnnamedGlobalConstantDecl(const ASTContext &C,
3445*12c85518Srobert                                                      DeclContext *DC,
3446*12c85518Srobert                                                      QualType Ty,
3447*12c85518Srobert                                                      const APValue &Val)
3448*12c85518Srobert     : ValueDecl(Decl::UnnamedGlobalConstant, DC, SourceLocation(),
3449*12c85518Srobert                 DeclarationName(), Ty),
3450*12c85518Srobert       Value(Val) {
3451*12c85518Srobert   // Cleanup the embedded APValue if required (note that our destructor is never
3452*12c85518Srobert   // run)
3453*12c85518Srobert   if (Value.needsCleanup())
3454*12c85518Srobert     C.addDestruction(&Value);
3455*12c85518Srobert }
3456*12c85518Srobert 
3457*12c85518Srobert UnnamedGlobalConstantDecl *
Create(const ASTContext & C,QualType T,const APValue & Value)3458*12c85518Srobert UnnamedGlobalConstantDecl::Create(const ASTContext &C, QualType T,
3459*12c85518Srobert                                   const APValue &Value) {
3460*12c85518Srobert   DeclContext *DC = C.getTranslationUnitDecl();
3461*12c85518Srobert   return new (C, DC) UnnamedGlobalConstantDecl(C, DC, T, Value);
3462*12c85518Srobert }
3463*12c85518Srobert 
3464*12c85518Srobert UnnamedGlobalConstantDecl *
CreateDeserialized(ASTContext & C,unsigned ID)3465*12c85518Srobert UnnamedGlobalConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3466*12c85518Srobert   return new (C, ID)
3467*12c85518Srobert       UnnamedGlobalConstantDecl(C, nullptr, QualType(), APValue());
3468*12c85518Srobert }
3469*12c85518Srobert 
printName(llvm::raw_ostream & OS,const PrintingPolicy &) const3470*12c85518Srobert void UnnamedGlobalConstantDecl::printName(llvm::raw_ostream &OS,
3471*12c85518Srobert                                           const PrintingPolicy &) const {
3472*12c85518Srobert   OS << "unnamed-global-constant";
3473*12c85518Srobert }
3474*12c85518Srobert 
getAccessName(AccessSpecifier AS)3475e5dd7070Spatrick static const char *getAccessName(AccessSpecifier AS) {
3476e5dd7070Spatrick   switch (AS) {
3477e5dd7070Spatrick     case AS_none:
3478e5dd7070Spatrick       llvm_unreachable("Invalid access specifier!");
3479e5dd7070Spatrick     case AS_public:
3480e5dd7070Spatrick       return "public";
3481e5dd7070Spatrick     case AS_private:
3482e5dd7070Spatrick       return "private";
3483e5dd7070Spatrick     case AS_protected:
3484e5dd7070Spatrick       return "protected";
3485e5dd7070Spatrick   }
3486e5dd7070Spatrick   llvm_unreachable("Invalid access specifier!");
3487e5dd7070Spatrick }
3488e5dd7070Spatrick 
operator <<(const StreamingDiagnostic & DB,AccessSpecifier AS)3489a9ac8606Spatrick const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
3490e5dd7070Spatrick                                              AccessSpecifier AS) {
3491e5dd7070Spatrick   return DB << getAccessName(AS);
3492e5dd7070Spatrick }
3493