1 //===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the C++ related Decl classes.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/DeclCXX.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTLambda.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/ASTUnresolvedSet.h"
18 #include "clang/AST/Attr.h"
19 #include "clang/AST/CXXInheritance.h"
20 #include "clang/AST/DeclBase.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/DeclarationName.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/ExprCXX.h"
25 #include "clang/AST/LambdaCapture.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/ODRHash.h"
28 #include "clang/AST/Type.h"
29 #include "clang/AST/TypeLoc.h"
30 #include "clang/AST/UnresolvedSet.h"
31 #include "clang/Basic/Diagnostic.h"
32 #include "clang/Basic/IdentifierTable.h"
33 #include "clang/Basic/LLVM.h"
34 #include "clang/Basic/LangOptions.h"
35 #include "clang/Basic/OperatorKinds.h"
36 #include "clang/Basic/PartialDiagnostic.h"
37 #include "clang/Basic/SourceLocation.h"
38 #include "clang/Basic/Specifiers.h"
39 #include "clang/Basic/TargetInfo.h"
40 #include "llvm/ADT/SmallPtrSet.h"
41 #include "llvm/ADT/SmallVector.h"
42 #include "llvm/ADT/iterator_range.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/Format.h"
46 #include "llvm/Support/raw_ostream.h"
52 using namespace clang
;
54 //===----------------------------------------------------------------------===//
55 // Decl Allocation/Deallocation Method Implementations
56 //===----------------------------------------------------------------------===//
58 void AccessSpecDecl::anchor() {}
60 AccessSpecDecl
*AccessSpecDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
61 return new (C
, ID
) AccessSpecDecl(EmptyShell());
64 void LazyASTUnresolvedSet::getFromExternalSource(ASTContext
&C
) const {
65 ExternalASTSource
*Source
= C
.getExternalSource();
66 assert(Impl
.Decls
.isLazy() && "getFromExternalSource for non-lazy set");
67 assert(Source
&& "getFromExternalSource with no external source");
69 for (ASTUnresolvedSet::iterator I
= Impl
.begin(); I
!= Impl
.end(); ++I
)
70 I
.setDecl(cast
<NamedDecl
>(Source
->GetExternalDecl(
71 reinterpret_cast<uintptr_t>(I
.getDecl()) >> 2)));
72 Impl
.Decls
.setLazy(false);
75 CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl
*D
)
76 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
77 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
78 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
79 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
80 HasPrivateFields(false), HasProtectedFields(false),
81 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
82 HasOnlyCMembers(true), HasInitMethod(false), HasInClassInitializer(false),
83 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
84 HasInheritedConstructor(false), HasInheritedDefaultConstructor(false),
85 HasInheritedAssignment(false),
86 NeedOverloadResolutionForCopyConstructor(false),
87 NeedOverloadResolutionForMoveConstructor(false),
88 NeedOverloadResolutionForCopyAssignment(false),
89 NeedOverloadResolutionForMoveAssignment(false),
90 NeedOverloadResolutionForDestructor(false),
91 DefaultedCopyConstructorIsDeleted(false),
92 DefaultedMoveConstructorIsDeleted(false),
93 DefaultedCopyAssignmentIsDeleted(false),
94 DefaultedMoveAssignmentIsDeleted(false),
95 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All
),
96 HasTrivialSpecialMembersForCall(SMF_All
),
97 DeclaredNonTrivialSpecialMembers(0),
98 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
99 HasConstexprNonCopyMoveConstructor(false),
100 HasDefaultedDefaultConstructor(false),
101 DefaultedDefaultConstructorIsConstexpr(true),
102 HasConstexprDefaultConstructor(false),
103 DefaultedDestructorIsConstexpr(true),
104 HasNonLiteralTypeFieldsOrBases(false), StructuralIfLiteral(true),
105 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
106 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
107 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
108 ImplicitCopyAssignmentHasConstParam(true),
109 HasDeclaredCopyConstructorWithConstParam(false),
110 HasDeclaredCopyAssignmentWithConstParam(false),
111 IsAnyDestructorNoReturn(false), IsLambda(false),
112 IsParsingBaseSpecifiers(false), ComputedVisibleConversions(false),
113 HasODRHash(false), Definition(D
) {}
115 CXXBaseSpecifier
*CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
116 return Bases
.get(Definition
->getASTContext().getExternalSource());
119 CXXBaseSpecifier
*CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
120 return VBases
.get(Definition
->getASTContext().getExternalSource());
123 CXXRecordDecl::CXXRecordDecl(Kind K
, TagKind TK
, const ASTContext
&C
,
124 DeclContext
*DC
, SourceLocation StartLoc
,
125 SourceLocation IdLoc
, IdentifierInfo
*Id
,
126 CXXRecordDecl
*PrevDecl
)
127 : RecordDecl(K
, TK
, C
, DC
, StartLoc
, IdLoc
, Id
, PrevDecl
),
128 DefinitionData(PrevDecl
? PrevDecl
->DefinitionData
131 CXXRecordDecl
*CXXRecordDecl::Create(const ASTContext
&C
, TagKind TK
,
132 DeclContext
*DC
, SourceLocation StartLoc
,
133 SourceLocation IdLoc
, IdentifierInfo
*Id
,
134 CXXRecordDecl
*PrevDecl
,
135 bool DelayTypeCreation
) {
136 auto *R
= new (C
, DC
) CXXRecordDecl(CXXRecord
, TK
, C
, DC
, StartLoc
, IdLoc
, Id
,
138 R
->setMayHaveOutOfDateDef(C
.getLangOpts().Modules
);
140 // FIXME: DelayTypeCreation seems like such a hack
141 if (!DelayTypeCreation
)
142 C
.getTypeDeclType(R
, PrevDecl
);
147 CXXRecordDecl::CreateLambda(const ASTContext
&C
, DeclContext
*DC
,
148 TypeSourceInfo
*Info
, SourceLocation Loc
,
149 unsigned DependencyKind
, bool IsGeneric
,
150 LambdaCaptureDefault CaptureDefault
) {
151 auto *R
= new (C
, DC
) CXXRecordDecl(CXXRecord
, TTK_Class
, C
, DC
, Loc
, Loc
,
153 R
->setBeingDefined(true);
154 R
->DefinitionData
= new (C
) struct LambdaDefinitionData(
155 R
, Info
, DependencyKind
, IsGeneric
, CaptureDefault
);
156 R
->setMayHaveOutOfDateDef(false);
157 R
->setImplicit(true);
159 C
.getTypeDeclType(R
, /*PrevDecl=*/nullptr);
164 CXXRecordDecl::CreateDeserialized(const ASTContext
&C
, unsigned ID
) {
165 auto *R
= new (C
, ID
) CXXRecordDecl(
166 CXXRecord
, TTK_Struct
, C
, nullptr, SourceLocation(), SourceLocation(),
168 R
->setMayHaveOutOfDateDef(false);
172 /// Determine whether a class has a repeated base class. This is intended for
173 /// use when determining if a class is standard-layout, so makes no attempt to
174 /// handle virtual bases.
175 static bool hasRepeatedBaseClass(const CXXRecordDecl
*StartRD
) {
176 llvm::SmallPtrSet
<const CXXRecordDecl
*, 8> SeenBaseTypes
;
177 SmallVector
<const CXXRecordDecl
*, 8> WorkList
= {StartRD
};
178 while (!WorkList
.empty()) {
179 const CXXRecordDecl
*RD
= WorkList
.pop_back_val();
180 if (RD
->getTypeForDecl()->isDependentType())
182 for (const CXXBaseSpecifier
&BaseSpec
: RD
->bases()) {
183 if (const CXXRecordDecl
*B
= BaseSpec
.getType()->getAsCXXRecordDecl()) {
184 if (!SeenBaseTypes
.insert(B
).second
)
186 WorkList
.push_back(B
);
194 CXXRecordDecl::setBases(CXXBaseSpecifier
const * const *Bases
,
196 ASTContext
&C
= getASTContext();
198 if (!data().Bases
.isOffset() && data().NumBases
> 0)
199 C
.Deallocate(data().getBases());
202 if (!C
.getLangOpts().CPlusPlus17
) {
203 // C++ [dcl.init.aggr]p1:
204 // An aggregate is [...] a class with [...] no base classes [...].
205 data().Aggregate
= false;
209 // A POD-struct is an aggregate class...
210 data().PlainOldData
= false;
213 // The set of seen virtual base types.
214 llvm::SmallPtrSet
<CanQualType
, 8> SeenVBaseTypes
;
216 // The virtual bases of this class.
217 SmallVector
<const CXXBaseSpecifier
*, 8> VBases
;
219 data().Bases
= new(C
) CXXBaseSpecifier
[NumBases
];
220 data().NumBases
= NumBases
;
221 for (unsigned i
= 0; i
< NumBases
; ++i
) {
222 data().getBases()[i
] = *Bases
[i
];
223 // Keep track of inherited vbases for this base class.
224 const CXXBaseSpecifier
*Base
= Bases
[i
];
225 QualType BaseType
= Base
->getType();
226 // Skip dependent types; we can't do any checking on them now.
227 if (BaseType
->isDependentType())
229 auto *BaseClassDecl
=
230 cast
<CXXRecordDecl
>(BaseType
->castAs
<RecordType
>()->getDecl());
233 // A standard-layout class is a class that:
235 // -- has all non-static data members and bit-fields in the class and
236 // its base classes first declared in the same class
237 if (BaseClassDecl
->data().HasBasesWithFields
||
238 !BaseClassDecl
->field_empty()) {
239 if (data().HasBasesWithFields
)
240 // Two bases have members or bit-fields: not standard-layout.
241 data().IsStandardLayout
= false;
242 data().HasBasesWithFields
= true;
246 // A standard-layout class is a class that:
247 // -- [...] has [...] at most one base class with non-static data
249 if (BaseClassDecl
->data().HasBasesWithNonStaticDataMembers
||
250 BaseClassDecl
->hasDirectFields()) {
251 if (data().HasBasesWithNonStaticDataMembers
)
252 data().IsCXX11StandardLayout
= false;
253 data().HasBasesWithNonStaticDataMembers
= true;
256 if (!BaseClassDecl
->isEmpty()) {
257 // C++14 [meta.unary.prop]p4:
258 // T is a class type [...] with [...] no base class B for which
259 // is_empty<B>::value is false.
260 data().Empty
= false;
263 // C++1z [dcl.init.agg]p1:
264 // An aggregate is a class with [...] no private or protected base classes
265 if (Base
->getAccessSpecifier() != AS_public
) {
266 data().Aggregate
= false;
268 // C++20 [temp.param]p7:
269 // A structural type is [...] a literal class type with [...] all base
270 // classes [...] public
271 data().StructuralIfLiteral
= false;
274 // C++ [class.virtual]p1:
275 // A class that declares or inherits a virtual function is called a
276 // polymorphic class.
277 if (BaseClassDecl
->isPolymorphic()) {
278 data().Polymorphic
= true;
280 // An aggregate is a class with [...] no virtual functions.
281 data().Aggregate
= false;
285 // A standard-layout class is a class that: [...]
286 // -- has no non-standard-layout base classes
287 if (!BaseClassDecl
->isStandardLayout())
288 data().IsStandardLayout
= false;
289 if (!BaseClassDecl
->isCXX11StandardLayout())
290 data().IsCXX11StandardLayout
= false;
292 // Record if this base is the first non-literal field or base.
293 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType
->isLiteralType(C
))
294 data().HasNonLiteralTypeFieldsOrBases
= true;
296 // Now go through all virtual bases of this base and add them.
297 for (const auto &VBase
: BaseClassDecl
->vbases()) {
298 // Add this base if it's not already in the list.
299 if (SeenVBaseTypes
.insert(C
.getCanonicalType(VBase
.getType())).second
) {
300 VBases
.push_back(&VBase
);
302 // C++11 [class.copy]p8:
303 // The implicitly-declared copy constructor for a class X will have
304 // the form 'X::X(const X&)' if each [...] virtual base class B of X
305 // has a copy constructor whose first parameter is of type
306 // 'const B&' or 'const volatile B&' [...]
307 if (CXXRecordDecl
*VBaseDecl
= VBase
.getType()->getAsCXXRecordDecl())
308 if (!VBaseDecl
->hasCopyConstructorWithConstParam())
309 data().ImplicitCopyConstructorCanHaveConstParamForVBase
= false;
311 // C++1z [dcl.init.agg]p1:
312 // An aggregate is a class with [...] no virtual base classes
313 data().Aggregate
= false;
317 if (Base
->isVirtual()) {
318 // Add this base if it's not already in the list.
319 if (SeenVBaseTypes
.insert(C
.getCanonicalType(BaseType
)).second
)
320 VBases
.push_back(Base
);
322 // C++14 [meta.unary.prop] is_empty:
323 // T is a class type, but not a union type, with ... no virtual base
325 data().Empty
= false;
327 // C++1z [dcl.init.agg]p1:
328 // An aggregate is a class with [...] no virtual base classes
329 data().Aggregate
= false;
331 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
332 // A [default constructor, copy/move constructor, or copy/move assignment
333 // operator for a class X] is trivial [...] if:
334 // -- class X has [...] no virtual base classes
335 data().HasTrivialSpecialMembers
&= SMF_Destructor
;
336 data().HasTrivialSpecialMembersForCall
&= SMF_Destructor
;
339 // A standard-layout class is a class that: [...]
340 // -- has [...] no virtual base classes
341 data().IsStandardLayout
= false;
342 data().IsCXX11StandardLayout
= false;
344 // C++20 [dcl.constexpr]p3:
345 // In the definition of a constexpr function [...]
346 // -- if the function is a constructor or destructor,
347 // its class shall not have any virtual base classes
348 data().DefaultedDefaultConstructorIsConstexpr
= false;
349 data().DefaultedDestructorIsConstexpr
= false;
351 // C++1z [class.copy]p8:
352 // The implicitly-declared copy constructor for a class X will have
353 // the form 'X::X(const X&)' if each potentially constructed subobject
354 // has a copy constructor whose first parameter is of type
355 // 'const B&' or 'const volatile B&' [...]
356 if (!BaseClassDecl
->hasCopyConstructorWithConstParam())
357 data().ImplicitCopyConstructorCanHaveConstParamForVBase
= false;
359 // C++ [class.ctor]p5:
360 // A default constructor is trivial [...] if:
361 // -- all the direct base classes of its class have trivial default
363 if (!BaseClassDecl
->hasTrivialDefaultConstructor())
364 data().HasTrivialSpecialMembers
&= ~SMF_DefaultConstructor
;
366 // C++0x [class.copy]p13:
367 // A copy/move constructor for class X is trivial if [...]
369 // -- the constructor selected to copy/move each direct base class
370 // subobject is trivial, and
371 if (!BaseClassDecl
->hasTrivialCopyConstructor())
372 data().HasTrivialSpecialMembers
&= ~SMF_CopyConstructor
;
374 if (!BaseClassDecl
->hasTrivialCopyConstructorForCall())
375 data().HasTrivialSpecialMembersForCall
&= ~SMF_CopyConstructor
;
377 // If the base class doesn't have a simple move constructor, we'll eagerly
378 // declare it and perform overload resolution to determine which function
379 // it actually calls. If it does have a simple move constructor, this
381 if (!BaseClassDecl
->hasTrivialMoveConstructor())
382 data().HasTrivialSpecialMembers
&= ~SMF_MoveConstructor
;
384 if (!BaseClassDecl
->hasTrivialMoveConstructorForCall())
385 data().HasTrivialSpecialMembersForCall
&= ~SMF_MoveConstructor
;
387 // C++0x [class.copy]p27:
388 // A copy/move assignment operator for class X is trivial if [...]
390 // -- the assignment operator selected to copy/move each direct base
391 // class subobject is trivial, and
392 if (!BaseClassDecl
->hasTrivialCopyAssignment())
393 data().HasTrivialSpecialMembers
&= ~SMF_CopyAssignment
;
394 // If the base class doesn't have a simple move assignment, we'll eagerly
395 // declare it and perform overload resolution to determine which function
396 // it actually calls. If it does have a simple move assignment, this
398 if (!BaseClassDecl
->hasTrivialMoveAssignment())
399 data().HasTrivialSpecialMembers
&= ~SMF_MoveAssignment
;
401 // C++11 [class.ctor]p6:
402 // If that user-written default constructor would satisfy the
403 // requirements of a constexpr constructor, the implicitly-defined
404 // default constructor is constexpr.
405 if (!BaseClassDecl
->hasConstexprDefaultConstructor())
406 data().DefaultedDefaultConstructorIsConstexpr
= false;
408 // C++1z [class.copy]p8:
409 // The implicitly-declared copy constructor for a class X will have
410 // the form 'X::X(const X&)' if each potentially constructed subobject
411 // has a copy constructor whose first parameter is of type
412 // 'const B&' or 'const volatile B&' [...]
413 if (!BaseClassDecl
->hasCopyConstructorWithConstParam())
414 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase
= false;
417 // C++ [class.ctor]p3:
418 // A destructor is trivial if all the direct base classes of its class
419 // have trivial destructors.
420 if (!BaseClassDecl
->hasTrivialDestructor())
421 data().HasTrivialSpecialMembers
&= ~SMF_Destructor
;
423 if (!BaseClassDecl
->hasTrivialDestructorForCall())
424 data().HasTrivialSpecialMembersForCall
&= ~SMF_Destructor
;
426 if (!BaseClassDecl
->hasIrrelevantDestructor())
427 data().HasIrrelevantDestructor
= false;
429 if (BaseClassDecl
->isAnyDestructorNoReturn())
430 data().IsAnyDestructorNoReturn
= true;
432 // C++11 [class.copy]p18:
433 // The implicitly-declared copy assignment operator for a class X will
434 // have the form 'X& X::operator=(const X&)' if each direct base class B
435 // of X has a copy assignment operator whose parameter is of type 'const
436 // B&', 'const volatile B&', or 'B' [...]
437 if (!BaseClassDecl
->hasCopyAssignmentWithConstParam())
438 data().ImplicitCopyAssignmentHasConstParam
= false;
440 // A class has an Objective-C object member if... or any of its bases
441 // has an Objective-C object member.
442 if (BaseClassDecl
->hasObjectMember())
443 setHasObjectMember(true);
445 if (BaseClassDecl
->hasVolatileMember())
446 setHasVolatileMember(true);
448 if (BaseClassDecl
->getArgPassingRestrictions() ==
449 RecordDecl::APK_CanNeverPassInRegs
)
450 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs
);
452 // Keep track of the presence of mutable fields.
453 if (BaseClassDecl
->hasMutableFields())
454 data().HasMutableFields
= true;
456 if (BaseClassDecl
->hasUninitializedReferenceMember())
457 data().HasUninitializedReferenceMember
= true;
459 if (!BaseClassDecl
->allowConstDefaultInit())
460 data().HasUninitializedFields
= true;
462 addedClassSubobject(BaseClassDecl
);
466 // A class S is a standard-layout class if it:
467 // -- has at most one base class subobject of any given type
469 // Note that we only need to check this for classes with more than one base
470 // class. If there's only one base class, and it's standard layout, then
471 // we know there are no repeated base classes.
472 if (data().IsStandardLayout
&& NumBases
> 1 && hasRepeatedBaseClass(this))
473 data().IsStandardLayout
= false;
475 if (VBases
.empty()) {
476 data().IsParsingBaseSpecifiers
= false;
480 // Create base specifier for any direct or indirect virtual bases.
481 data().VBases
= new (C
) CXXBaseSpecifier
[VBases
.size()];
482 data().NumVBases
= VBases
.size();
483 for (int I
= 0, E
= VBases
.size(); I
!= E
; ++I
) {
484 QualType Type
= VBases
[I
]->getType();
485 if (!Type
->isDependentType())
486 addedClassSubobject(Type
->getAsCXXRecordDecl());
487 data().getVBases()[I
] = *VBases
[I
];
490 data().IsParsingBaseSpecifiers
= false;
493 unsigned CXXRecordDecl::getODRHash() const {
494 assert(hasDefinition() && "ODRHash only for records with definitions");
496 // Previously calculated hash is stored in DefinitionData.
497 if (DefinitionData
->HasODRHash
)
498 return DefinitionData
->ODRHash
;
500 // Only calculate hash on first call of getODRHash per record.
502 Hash
.AddCXXRecordDecl(getDefinition());
503 DefinitionData
->HasODRHash
= true;
504 DefinitionData
->ODRHash
= Hash
.CalculateHash();
506 return DefinitionData
->ODRHash
;
509 void CXXRecordDecl::addedClassSubobject(CXXRecordDecl
*Subobj
) {
510 // C++11 [class.copy]p11:
511 // A defaulted copy/move constructor for a class X is defined as
513 // -- a direct or virtual base class B that cannot be copied/moved [...]
514 // -- a non-static data member of class type M (or array thereof)
515 // that cannot be copied or moved [...]
516 if (!Subobj
->hasSimpleCopyConstructor())
517 data().NeedOverloadResolutionForCopyConstructor
= true;
518 if (!Subobj
->hasSimpleMoveConstructor())
519 data().NeedOverloadResolutionForMoveConstructor
= true;
521 // C++11 [class.copy]p23:
522 // A defaulted copy/move assignment operator for a class X is defined as
524 // -- a direct or virtual base class B that cannot be copied/moved [...]
525 // -- a non-static data member of class type M (or array thereof)
526 // that cannot be copied or moved [...]
527 if (!Subobj
->hasSimpleCopyAssignment())
528 data().NeedOverloadResolutionForCopyAssignment
= true;
529 if (!Subobj
->hasSimpleMoveAssignment())
530 data().NeedOverloadResolutionForMoveAssignment
= true;
532 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
533 // A defaulted [ctor or dtor] for a class X is defined as
535 // -- any direct or virtual base class [...] has a type with a destructor
536 // that is deleted or inaccessible from the defaulted [ctor or dtor].
537 // -- any non-static data member has a type with a destructor
538 // that is deleted or inaccessible from the defaulted [ctor or dtor].
539 if (!Subobj
->hasSimpleDestructor()) {
540 data().NeedOverloadResolutionForCopyConstructor
= true;
541 data().NeedOverloadResolutionForMoveConstructor
= true;
542 data().NeedOverloadResolutionForDestructor
= true;
545 // C++2a [dcl.constexpr]p4:
546 // The definition of a constexpr destructor [shall] satisfy the
547 // following requirement:
548 // -- for every subobject of class type or (possibly multi-dimensional)
549 // array thereof, that class type shall have a constexpr destructor
550 if (!Subobj
->hasConstexprDestructor())
551 data().DefaultedDestructorIsConstexpr
= false;
553 // C++20 [temp.param]p7:
554 // A structural type is [...] a literal class type [for which] the types
555 // of all base classes and non-static data members are structural types or
556 // (possibly multi-dimensional) array thereof
557 if (!Subobj
->data().StructuralIfLiteral
)
558 data().StructuralIfLiteral
= false;
561 bool CXXRecordDecl::hasConstexprDestructor() const {
562 auto *Dtor
= getDestructor();
563 return Dtor
? Dtor
->isConstexpr() : defaultedDestructorIsConstexpr();
566 bool CXXRecordDecl::hasAnyDependentBases() const {
567 if (!isDependentContext())
570 return !forallBases([](const CXXRecordDecl
*) { return true; });
573 bool CXXRecordDecl::isTriviallyCopyable() const {
575 // A trivially copyable class is a class that:
576 // -- has no non-trivial copy constructors,
577 if (hasNonTrivialCopyConstructor()) return false;
578 // -- has no non-trivial move constructors,
579 if (hasNonTrivialMoveConstructor()) return false;
580 // -- has no non-trivial copy assignment operators,
581 if (hasNonTrivialCopyAssignment()) return false;
582 // -- has no non-trivial move assignment operators, and
583 if (hasNonTrivialMoveAssignment()) return false;
584 // -- has a trivial destructor.
585 if (!hasTrivialDestructor()) return false;
590 void CXXRecordDecl::markedVirtualFunctionPure() {
591 // C++ [class.abstract]p2:
592 // A class is abstract if it has at least one pure virtual function.
593 data().Abstract
= true;
596 bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
597 ASTContext
&Ctx
, const CXXRecordDecl
*XFirst
) {
601 llvm::SmallPtrSet
<const CXXRecordDecl
*, 8> Bases
;
602 llvm::SmallPtrSet
<const CXXRecordDecl
*, 8> M
;
603 SmallVector
<const CXXRecordDecl
*, 8> WorkList
;
605 // Visit a type that we have determined is an element of M(S).
606 auto Visit
= [&](const CXXRecordDecl
*RD
) -> bool {
607 RD
= RD
->getCanonicalDecl();
610 // A class S is a standard-layout class if it [...] has no element of the
611 // set M(S) of types as a base class.
613 // If we find a subobject of an empty type, it might also be a base class,
614 // so we'll need to walk the base classes to check.
615 if (!RD
->data().HasBasesWithFields
) {
616 // Walk the bases the first time, stopping if we find the type. Build a
617 // set of them so we don't need to walk them again.
619 bool RDIsBase
= !forallBases([&](const CXXRecordDecl
*Base
) -> bool {
620 Base
= Base
->getCanonicalDecl();
634 if (M
.insert(RD
).second
)
635 WorkList
.push_back(RD
);
642 while (!WorkList
.empty()) {
643 const CXXRecordDecl
*X
= WorkList
.pop_back_val();
645 // FIXME: We don't check the bases of X. That matches the standard, but
646 // that sure looks like a wording bug.
648 // -- If X is a non-union class type with a non-static data member
649 // [recurse to each field] that is either of zero size or is the
650 // first non-static data member of X
651 // -- If X is a union type, [recurse to union members]
652 bool IsFirstField
= true;
653 for (auto *FD
: X
->fields()) {
654 // FIXME: Should we really care about the type of the first non-static
655 // data member of a non-union if there are preceding unnamed bit-fields?
656 if (FD
->isUnnamedBitfield())
659 if (!IsFirstField
&& !FD
->isZeroSize(Ctx
))
662 // -- If X is n array type, [visit the element type]
663 QualType T
= Ctx
.getBaseElementType(FD
->getType());
664 if (auto *RD
= T
->getAsCXXRecordDecl())
669 IsFirstField
= false;
676 bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
677 assert(isLambda() && "not a lambda");
679 // C++2a [expr.prim.lambda.capture]p11:
680 // The closure type associated with a lambda-expression has no default
681 // constructor if the lambda-expression has a lambda-capture and a
682 // defaulted default constructor otherwise. It has a deleted copy
683 // assignment operator if the lambda-expression has a lambda-capture and
684 // defaulted copy and move assignment operators otherwise.
686 // C++17 [expr.prim.lambda]p21:
687 // The closure type associated with a lambda-expression has no default
688 // constructor and a deleted copy assignment operator.
689 if (getLambdaCaptureDefault() != LCD_None
|| capture_size() != 0)
691 return getASTContext().getLangOpts().CPlusPlus20
;
694 void CXXRecordDecl::addedMember(Decl
*D
) {
695 if (!D
->isImplicit() &&
696 !isa
<FieldDecl
>(D
) &&
697 !isa
<IndirectFieldDecl
>(D
) &&
698 (!isa
<TagDecl
>(D
) || cast
<TagDecl
>(D
)->getTagKind() == TTK_Class
||
699 cast
<TagDecl
>(D
)->getTagKind() == TTK_Interface
))
700 data().HasOnlyCMembers
= false;
702 // Ignore friends and invalid declarations.
703 if (D
->getFriendObjectKind() || D
->isInvalidDecl())
706 auto *FunTmpl
= dyn_cast
<FunctionTemplateDecl
>(D
);
708 D
= FunTmpl
->getTemplatedDecl();
710 // FIXME: Pass NamedDecl* to addedMember?
711 Decl
*DUnderlying
= D
;
712 if (auto *ND
= dyn_cast
<NamedDecl
>(DUnderlying
)) {
713 DUnderlying
= ND
->getUnderlyingDecl();
714 if (auto *UnderlyingFunTmpl
= dyn_cast
<FunctionTemplateDecl
>(DUnderlying
))
715 DUnderlying
= UnderlyingFunTmpl
->getTemplatedDecl();
718 if (const auto *Method
= dyn_cast
<CXXMethodDecl
>(D
)) {
719 if (Method
->isVirtual()) {
720 // C++ [dcl.init.aggr]p1:
721 // An aggregate is an array or a class with [...] no virtual functions.
722 data().Aggregate
= false;
725 // A POD-struct is an aggregate class...
726 data().PlainOldData
= false;
728 // C++14 [meta.unary.prop]p4:
729 // T is a class type [...] with [...] no virtual member functions...
730 data().Empty
= false;
732 // C++ [class.virtual]p1:
733 // A class that declares or inherits a virtual function is called a
734 // polymorphic class.
735 data().Polymorphic
= true;
737 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
738 // A [default constructor, copy/move constructor, or copy/move
739 // assignment operator for a class X] is trivial [...] if:
740 // -- class X has no virtual functions [...]
741 data().HasTrivialSpecialMembers
&= SMF_Destructor
;
742 data().HasTrivialSpecialMembersForCall
&= SMF_Destructor
;
745 // A standard-layout class is a class that: [...]
746 // -- has no virtual functions
747 data().IsStandardLayout
= false;
748 data().IsCXX11StandardLayout
= false;
752 // Notify the listener if an implicit member was added after the definition
754 if (!isBeingDefined() && D
->isImplicit())
755 if (ASTMutationListener
*L
= getASTMutationListener())
756 L
->AddedCXXImplicitMember(data().Definition
, D
);
758 // The kind of special member this declaration is, if any.
761 // Handle constructors.
762 if (const auto *Constructor
= dyn_cast
<CXXConstructorDecl
>(D
)) {
763 if (Constructor
->isInheritingConstructor()) {
764 // Ignore constructor shadow declarations. They are lazily created and
765 // so shouldn't affect any properties of the class.
767 if (!Constructor
->isImplicit()) {
768 // Note that we have a user-declared constructor.
769 data().UserDeclaredConstructor
= true;
771 const TargetInfo
&TI
= getASTContext().getTargetInfo();
772 if ((!Constructor
->isDeleted() && !Constructor
->isDefaulted()) ||
773 !TI
.areDefaultedSMFStillPOD(getLangOpts())) {
775 // A POD-struct is an aggregate class [...]
776 // Since the POD bit is meant to be C++03 POD-ness, clear it even if
777 // the type is technically an aggregate in C++0x since it wouldn't be
779 data().PlainOldData
= false;
783 if (Constructor
->isDefaultConstructor()) {
784 SMKind
|= SMF_DefaultConstructor
;
786 if (Constructor
->isUserProvided())
787 data().UserProvidedDefaultConstructor
= true;
788 if (Constructor
->isConstexpr())
789 data().HasConstexprDefaultConstructor
= true;
790 if (Constructor
->isDefaulted())
791 data().HasDefaultedDefaultConstructor
= true;
796 if (Constructor
->isCopyConstructor(Quals
)) {
797 SMKind
|= SMF_CopyConstructor
;
799 if (Quals
& Qualifiers::Const
)
800 data().HasDeclaredCopyConstructorWithConstParam
= true;
801 } else if (Constructor
->isMoveConstructor())
802 SMKind
|= SMF_MoveConstructor
;
805 // C++11 [dcl.init.aggr]p1: DR1518
806 // An aggregate is an array or a class with no user-provided [or]
807 // explicit [...] constructors
808 // C++20 [dcl.init.aggr]p1:
809 // An aggregate is an array or a class with no user-declared [...]
811 if (getASTContext().getLangOpts().CPlusPlus20
812 ? !Constructor
->isImplicit()
813 : (Constructor
->isUserProvided() || Constructor
->isExplicit()))
814 data().Aggregate
= false;
818 // Handle constructors, including those inherited from base classes.
819 if (const auto *Constructor
= dyn_cast
<CXXConstructorDecl
>(DUnderlying
)) {
820 // Record if we see any constexpr constructors which are neither copy
821 // nor move constructors.
822 // C++1z [basic.types]p10:
823 // [...] has at least one constexpr constructor or constructor template
824 // (possibly inherited from a base class) that is not a copy or move
826 if (Constructor
->isConstexpr() && !Constructor
->isCopyOrMoveConstructor())
827 data().HasConstexprNonCopyMoveConstructor
= true;
828 if (!isa
<CXXConstructorDecl
>(D
) && Constructor
->isDefaultConstructor())
829 data().HasInheritedDefaultConstructor
= true;
832 // Handle member functions.
833 if (const auto *Method
= dyn_cast
<CXXMethodDecl
>(D
)) {
834 if (isa
<CXXDestructorDecl
>(D
))
835 SMKind
|= SMF_Destructor
;
837 if (Method
->isCopyAssignmentOperator()) {
838 SMKind
|= SMF_CopyAssignment
;
840 const auto *ParamTy
=
841 Method
->getParamDecl(0)->getType()->getAs
<ReferenceType
>();
842 if (!ParamTy
|| ParamTy
->getPointeeType().isConstQualified())
843 data().HasDeclaredCopyAssignmentWithConstParam
= true;
846 if (Method
->isMoveAssignmentOperator())
847 SMKind
|= SMF_MoveAssignment
;
849 // Keep the list of conversion functions up-to-date.
850 if (auto *Conversion
= dyn_cast
<CXXConversionDecl
>(D
)) {
851 // FIXME: We use the 'unsafe' accessor for the access specifier here,
852 // because Sema may not have set it yet. That's really just a misdesign
853 // in Sema. However, LLDB *will* have set the access specifier correctly,
854 // and adds declarations after the class is technically completed,
855 // so completeDefinition()'s overriding of the access specifiers doesn't
857 AccessSpecifier AS
= Conversion
->getAccessUnsafe();
859 if (Conversion
->getPrimaryTemplate()) {
860 // We don't record specializations.
862 ASTContext
&Ctx
= getASTContext();
863 ASTUnresolvedSet
&Conversions
= data().Conversions
.get(Ctx
);
865 FunTmpl
? cast
<NamedDecl
>(FunTmpl
) : cast
<NamedDecl
>(Conversion
);
866 if (Primary
->getPreviousDecl())
867 Conversions
.replace(cast
<NamedDecl
>(Primary
->getPreviousDecl()),
870 Conversions
.addDecl(Ctx
, Primary
, AS
);
875 // If this is the first declaration of a special member, we no longer have
876 // an implicit trivial special member.
877 data().HasTrivialSpecialMembers
&=
878 data().DeclaredSpecialMembers
| ~SMKind
;
879 data().HasTrivialSpecialMembersForCall
&=
880 data().DeclaredSpecialMembers
| ~SMKind
;
882 // Note when we have declared a declared special member, and suppress the
883 // implicit declaration of this special member.
884 data().DeclaredSpecialMembers
|= SMKind
;
885 if (!Method
->isImplicit()) {
886 data().UserDeclaredSpecialMembers
|= SMKind
;
888 const TargetInfo
&TI
= getASTContext().getTargetInfo();
889 if ((!Method
->isDeleted() && !Method
->isDefaulted() &&
890 SMKind
!= SMF_MoveAssignment
) ||
891 !TI
.areDefaultedSMFStillPOD(getLangOpts())) {
893 // A POD-struct is an aggregate class that has [...] no user-defined
894 // copy assignment operator and no user-defined destructor.
896 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
897 // aggregates could not have any constructors, clear it even for an
898 // explicitly defaulted or deleted constructor.
899 // type is technically an aggregate in C++0x since it wouldn't be in
902 // Also, a user-declared move assignment operator makes a class
903 // non-POD. This is an extension in C++03.
904 data().PlainOldData
= false;
907 // When instantiating a class, we delay updating the destructor and
908 // triviality properties of the class until selecting a destructor and
909 // computing the eligibility of its special member functions. This is
910 // because there might be function constraints that we need to evaluate
911 // and compare later in the instantiation.
912 if (!Method
->isIneligibleOrNotSelected()) {
913 addedEligibleSpecialMemberFunction(Method
, SMKind
);
920 // Handle non-static data members.
921 if (const auto *Field
= dyn_cast
<FieldDecl
>(D
)) {
922 ASTContext
&Context
= getASTContext();
925 // A standard-layout class is a class that:
927 // -- has all non-static data members and bit-fields in the class and
928 // its base classes first declared in the same class
929 if (data().HasBasesWithFields
)
930 data().IsStandardLayout
= false;
932 // C++ [class.bit]p2:
933 // A declaration for a bit-field that omits the identifier declares an
934 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
936 if (Field
->isUnnamedBitfield()) {
937 // C++ [meta.unary.prop]p4: [LWG2358]
938 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
940 if (data().Empty
&& !Field
->isZeroLengthBitField(Context
) &&
941 Context
.getLangOpts().getClangABICompat() >
942 LangOptions::ClangABI::Ver6
)
943 data().Empty
= false;
948 // A standard-layout class is a class that:
949 // -- either has no non-static data members in the most derived class
950 // [...] or has no base classes with non-static data members
951 if (data().HasBasesWithNonStaticDataMembers
)
952 data().IsCXX11StandardLayout
= false;
954 // C++ [dcl.init.aggr]p1:
955 // An aggregate is an array or a class (clause 9) with [...] no
956 // private or protected non-static data members (clause 11).
958 // A POD must be an aggregate.
959 if (D
->getAccess() == AS_private
|| D
->getAccess() == AS_protected
) {
960 data().Aggregate
= false;
961 data().PlainOldData
= false;
963 // C++20 [temp.param]p7:
964 // A structural type is [...] a literal class type [for which] all
965 // non-static data members are public
966 data().StructuralIfLiteral
= false;
969 // Track whether this is the first field. We use this when checking
970 // whether the class is standard-layout below.
971 bool IsFirstField
= !data().HasPrivateFields
&&
972 !data().HasProtectedFields
&& !data().HasPublicFields
;
975 // A standard-layout class is a class that:
977 // -- has the same access control for all non-static data members,
978 switch (D
->getAccess()) {
979 case AS_private
: data().HasPrivateFields
= true; break;
980 case AS_protected
: data().HasProtectedFields
= true; break;
981 case AS_public
: data().HasPublicFields
= true; break;
982 case AS_none
: llvm_unreachable("Invalid access specifier");
984 if ((data().HasPrivateFields
+ data().HasProtectedFields
+
985 data().HasPublicFields
) > 1) {
986 data().IsStandardLayout
= false;
987 data().IsCXX11StandardLayout
= false;
990 // Keep track of the presence of mutable fields.
991 if (Field
->isMutable()) {
992 data().HasMutableFields
= true;
994 // C++20 [temp.param]p7:
995 // A structural type is [...] a literal class type [for which] all
996 // non-static data members are public
997 data().StructuralIfLiteral
= false;
1000 // C++11 [class.union]p8, DR1460:
1001 // If X is a union, a non-static data member of X that is not an anonymous
1002 // union is a variant member of X.
1003 if (isUnion() && !Field
->isAnonymousStructOrUnion())
1004 data().HasVariantMembers
= true;
1007 // A POD struct is a class that is both a trivial class and a
1008 // standard-layout class, and has no non-static data members of type
1009 // non-POD struct, non-POD union (or array of such types).
1011 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
1012 // that does not explicitly have no lifetime makes the class a non-POD.
1013 QualType T
= Context
.getBaseElementType(Field
->getType());
1014 if (T
->isObjCRetainableType() || T
.isObjCGCStrong()) {
1015 if (T
.hasNonTrivialObjCLifetime()) {
1016 // Objective-C Automatic Reference Counting:
1017 // If a class has a non-static data member of Objective-C pointer
1018 // type (or array thereof), it is a non-POD type and its
1019 // default constructor (if any), copy constructor, move constructor,
1020 // copy assignment operator, move assignment operator, and destructor are
1022 setHasObjectMember(true);
1023 struct DefinitionData
&Data
= data();
1024 Data
.PlainOldData
= false;
1025 Data
.HasTrivialSpecialMembers
= 0;
1027 // __strong or __weak fields do not make special functions non-trivial
1028 // for the purpose of calls.
1029 Qualifiers::ObjCLifetime LT
= T
.getQualifiers().getObjCLifetime();
1030 if (LT
!= Qualifiers::OCL_Strong
&& LT
!= Qualifiers::OCL_Weak
)
1031 data().HasTrivialSpecialMembersForCall
= 0;
1033 // Structs with __weak fields should never be passed directly.
1034 if (LT
== Qualifiers::OCL_Weak
)
1035 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs
);
1037 Data
.HasIrrelevantDestructor
= false;
1040 data().DefaultedCopyConstructorIsDeleted
= true;
1041 data().DefaultedMoveConstructorIsDeleted
= true;
1042 data().DefaultedCopyAssignmentIsDeleted
= true;
1043 data().DefaultedMoveAssignmentIsDeleted
= true;
1044 data().DefaultedDestructorIsDeleted
= true;
1045 data().NeedOverloadResolutionForCopyConstructor
= true;
1046 data().NeedOverloadResolutionForMoveConstructor
= true;
1047 data().NeedOverloadResolutionForCopyAssignment
= true;
1048 data().NeedOverloadResolutionForMoveAssignment
= true;
1049 data().NeedOverloadResolutionForDestructor
= true;
1051 } else if (!Context
.getLangOpts().ObjCAutoRefCount
) {
1052 setHasObjectMember(true);
1054 } else if (!T
.isCXX98PODType(Context
))
1055 data().PlainOldData
= false;
1057 if (T
->isReferenceType()) {
1058 if (!Field
->hasInClassInitializer())
1059 data().HasUninitializedReferenceMember
= true;
1062 // A standard-layout class is a class that:
1063 // -- has no non-static data members of type [...] reference,
1064 data().IsStandardLayout
= false;
1065 data().IsCXX11StandardLayout
= false;
1067 // C++1z [class.copy.ctor]p10:
1068 // A defaulted copy constructor for a class X is defined as deleted if X has:
1069 // -- a non-static data member of rvalue reference type
1070 if (T
->isRValueReferenceType())
1071 data().DefaultedCopyConstructorIsDeleted
= true;
1074 if (!Field
->hasInClassInitializer() && !Field
->isMutable()) {
1075 if (CXXRecordDecl
*FieldType
= T
->getAsCXXRecordDecl()) {
1076 if (FieldType
->hasDefinition() && !FieldType
->allowConstDefaultInit())
1077 data().HasUninitializedFields
= true;
1079 data().HasUninitializedFields
= true;
1083 // Record if this field is the first non-literal or volatile field or base.
1084 if (!T
->isLiteralType(Context
) || T
.isVolatileQualified())
1085 data().HasNonLiteralTypeFieldsOrBases
= true;
1087 if (Field
->hasInClassInitializer() ||
1088 (Field
->isAnonymousStructOrUnion() &&
1089 Field
->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
1090 data().HasInClassInitializer
= true;
1093 // A default constructor is trivial if [...] no non-static data member
1094 // of its class has a brace-or-equal-initializer.
1095 data().HasTrivialSpecialMembers
&= ~SMF_DefaultConstructor
;
1097 // C++11 [dcl.init.aggr]p1:
1098 // An aggregate is a [...] class with [...] no
1099 // brace-or-equal-initializers for non-static data members.
1101 // This rule was removed in C++14.
1102 if (!getASTContext().getLangOpts().CPlusPlus14
)
1103 data().Aggregate
= false;
1105 // C++11 [class]p10:
1106 // A POD struct is [...] a trivial class.
1107 data().PlainOldData
= false;
1110 // C++11 [class.copy]p23:
1111 // A defaulted copy/move assignment operator for a class X is defined
1112 // as deleted if X has:
1113 // -- a non-static data member of reference type
1114 if (T
->isReferenceType()) {
1115 data().DefaultedCopyAssignmentIsDeleted
= true;
1116 data().DefaultedMoveAssignmentIsDeleted
= true;
1119 // Bitfields of length 0 are also zero-sized, but we already bailed out for
1120 // those because they are always unnamed.
1121 bool IsZeroSize
= Field
->isZeroSize(Context
);
1123 if (const auto *RecordTy
= T
->getAs
<RecordType
>()) {
1124 auto *FieldRec
= cast
<CXXRecordDecl
>(RecordTy
->getDecl());
1125 if (FieldRec
->getDefinition()) {
1126 addedClassSubobject(FieldRec
);
1128 // We may need to perform overload resolution to determine whether a
1129 // field can be moved if it's const or volatile qualified.
1130 if (T
.getCVRQualifiers() & (Qualifiers::Const
| Qualifiers::Volatile
)) {
1131 // We need to care about 'const' for the copy constructor because an
1132 // implicit copy constructor might be declared with a non-const
1134 data().NeedOverloadResolutionForCopyConstructor
= true;
1135 data().NeedOverloadResolutionForMoveConstructor
= true;
1136 data().NeedOverloadResolutionForCopyAssignment
= true;
1137 data().NeedOverloadResolutionForMoveAssignment
= true;
1140 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1141 // A defaulted [special member] for a class X is defined as
1143 // -- X is a union-like class that has a variant member with a
1144 // non-trivial [corresponding special member]
1146 if (FieldRec
->hasNonTrivialCopyConstructor())
1147 data().DefaultedCopyConstructorIsDeleted
= true;
1148 if (FieldRec
->hasNonTrivialMoveConstructor())
1149 data().DefaultedMoveConstructorIsDeleted
= true;
1150 if (FieldRec
->hasNonTrivialCopyAssignment())
1151 data().DefaultedCopyAssignmentIsDeleted
= true;
1152 if (FieldRec
->hasNonTrivialMoveAssignment())
1153 data().DefaultedMoveAssignmentIsDeleted
= true;
1154 if (FieldRec
->hasNonTrivialDestructor())
1155 data().DefaultedDestructorIsDeleted
= true;
1158 // For an anonymous union member, our overload resolution will perform
1159 // overload resolution for its members.
1160 if (Field
->isAnonymousStructOrUnion()) {
1161 data().NeedOverloadResolutionForCopyConstructor
|=
1162 FieldRec
->data().NeedOverloadResolutionForCopyConstructor
;
1163 data().NeedOverloadResolutionForMoveConstructor
|=
1164 FieldRec
->data().NeedOverloadResolutionForMoveConstructor
;
1165 data().NeedOverloadResolutionForCopyAssignment
|=
1166 FieldRec
->data().NeedOverloadResolutionForCopyAssignment
;
1167 data().NeedOverloadResolutionForMoveAssignment
|=
1168 FieldRec
->data().NeedOverloadResolutionForMoveAssignment
;
1169 data().NeedOverloadResolutionForDestructor
|=
1170 FieldRec
->data().NeedOverloadResolutionForDestructor
;
1173 // C++0x [class.ctor]p5:
1174 // A default constructor is trivial [...] if:
1175 // -- for all the non-static data members of its class that are of
1176 // class type (or array thereof), each such class has a trivial
1177 // default constructor.
1178 if (!FieldRec
->hasTrivialDefaultConstructor())
1179 data().HasTrivialSpecialMembers
&= ~SMF_DefaultConstructor
;
1181 // C++0x [class.copy]p13:
1182 // A copy/move constructor for class X is trivial if [...]
1184 // -- for each non-static data member of X that is of class type (or
1185 // an array thereof), the constructor selected to copy/move that
1186 // member is trivial;
1187 if (!FieldRec
->hasTrivialCopyConstructor())
1188 data().HasTrivialSpecialMembers
&= ~SMF_CopyConstructor
;
1190 if (!FieldRec
->hasTrivialCopyConstructorForCall())
1191 data().HasTrivialSpecialMembersForCall
&= ~SMF_CopyConstructor
;
1193 // If the field doesn't have a simple move constructor, we'll eagerly
1194 // declare the move constructor for this class and we'll decide whether
1195 // it's trivial then.
1196 if (!FieldRec
->hasTrivialMoveConstructor())
1197 data().HasTrivialSpecialMembers
&= ~SMF_MoveConstructor
;
1199 if (!FieldRec
->hasTrivialMoveConstructorForCall())
1200 data().HasTrivialSpecialMembersForCall
&= ~SMF_MoveConstructor
;
1202 // C++0x [class.copy]p27:
1203 // A copy/move assignment operator for class X is trivial if [...]
1205 // -- for each non-static data member of X that is of class type (or
1206 // an array thereof), the assignment operator selected to
1207 // copy/move that member is trivial;
1208 if (!FieldRec
->hasTrivialCopyAssignment())
1209 data().HasTrivialSpecialMembers
&= ~SMF_CopyAssignment
;
1210 // If the field doesn't have a simple move assignment, we'll eagerly
1211 // declare the move assignment for this class and we'll decide whether
1212 // it's trivial then.
1213 if (!FieldRec
->hasTrivialMoveAssignment())
1214 data().HasTrivialSpecialMembers
&= ~SMF_MoveAssignment
;
1216 if (!FieldRec
->hasTrivialDestructor())
1217 data().HasTrivialSpecialMembers
&= ~SMF_Destructor
;
1218 if (!FieldRec
->hasTrivialDestructorForCall())
1219 data().HasTrivialSpecialMembersForCall
&= ~SMF_Destructor
;
1220 if (!FieldRec
->hasIrrelevantDestructor())
1221 data().HasIrrelevantDestructor
= false;
1222 if (FieldRec
->isAnyDestructorNoReturn())
1223 data().IsAnyDestructorNoReturn
= true;
1224 if (FieldRec
->hasObjectMember())
1225 setHasObjectMember(true);
1226 if (FieldRec
->hasVolatileMember())
1227 setHasVolatileMember(true);
1228 if (FieldRec
->getArgPassingRestrictions() ==
1229 RecordDecl::APK_CanNeverPassInRegs
)
1230 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs
);
1233 // A standard-layout class is a class that:
1234 // -- has no non-static data members of type non-standard-layout
1235 // class (or array of such types) [...]
1236 if (!FieldRec
->isStandardLayout())
1237 data().IsStandardLayout
= false;
1238 if (!FieldRec
->isCXX11StandardLayout())
1239 data().IsCXX11StandardLayout
= false;
1242 // A standard-layout class is a class that:
1244 // -- has no element of the set M(S) of types as a base class.
1245 if (data().IsStandardLayout
&&
1246 (isUnion() || IsFirstField
|| IsZeroSize
) &&
1247 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context
, FieldRec
))
1248 data().IsStandardLayout
= false;
1251 // A standard-layout class is a class that:
1252 // -- has no base classes of the same type as the first non-static
1254 if (data().IsCXX11StandardLayout
&& IsFirstField
) {
1255 // FIXME: We should check all base classes here, not just direct
1257 for (const auto &BI
: bases()) {
1258 if (Context
.hasSameUnqualifiedType(BI
.getType(), T
)) {
1259 data().IsCXX11StandardLayout
= false;
1265 // Keep track of the presence of mutable fields.
1266 if (FieldRec
->hasMutableFields())
1267 data().HasMutableFields
= true;
1269 if (Field
->isMutable()) {
1270 // Our copy constructor/assignment might call something other than
1271 // the subobject's copy constructor/assignment if it's mutable and of
1273 data().NeedOverloadResolutionForCopyConstructor
= true;
1274 data().NeedOverloadResolutionForCopyAssignment
= true;
1277 // C++11 [class.copy]p13:
1278 // If the implicitly-defined constructor would satisfy the
1279 // requirements of a constexpr constructor, the implicitly-defined
1280 // constructor is constexpr.
1281 // C++11 [dcl.constexpr]p4:
1282 // -- every constructor involved in initializing non-static data
1283 // members [...] shall be a constexpr constructor
1284 if (!Field
->hasInClassInitializer() &&
1285 !FieldRec
->hasConstexprDefaultConstructor() && !isUnion())
1286 // The standard requires any in-class initializer to be a constant
1287 // expression. We consider this to be a defect.
1288 data().DefaultedDefaultConstructorIsConstexpr
= false;
1290 // C++11 [class.copy]p8:
1291 // The implicitly-declared copy constructor for a class X will have
1292 // the form 'X::X(const X&)' if each potentially constructed subobject
1293 // of a class type M (or array thereof) has a copy constructor whose
1294 // first parameter is of type 'const M&' or 'const volatile M&'.
1295 if (!FieldRec
->hasCopyConstructorWithConstParam())
1296 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase
= false;
1298 // C++11 [class.copy]p18:
1299 // The implicitly-declared copy assignment oeprator for a class X will
1300 // have the form 'X& X::operator=(const X&)' if [...] for all the
1301 // non-static data members of X that are of a class type M (or array
1302 // thereof), each such class type has a copy assignment operator whose
1303 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1304 if (!FieldRec
->hasCopyAssignmentWithConstParam())
1305 data().ImplicitCopyAssignmentHasConstParam
= false;
1307 if (FieldRec
->hasUninitializedReferenceMember() &&
1308 !Field
->hasInClassInitializer())
1309 data().HasUninitializedReferenceMember
= true;
1311 // C++11 [class.union]p8, DR1460:
1312 // a non-static data member of an anonymous union that is a member of
1313 // X is also a variant member of X.
1314 if (FieldRec
->hasVariantMembers() &&
1315 Field
->isAnonymousStructOrUnion())
1316 data().HasVariantMembers
= true;
1319 // Base element type of field is a non-class type.
1320 if (!T
->isLiteralType(Context
) ||
1321 (!Field
->hasInClassInitializer() && !isUnion() &&
1322 !Context
.getLangOpts().CPlusPlus20
))
1323 data().DefaultedDefaultConstructorIsConstexpr
= false;
1325 // C++11 [class.copy]p23:
1326 // A defaulted copy/move assignment operator for a class X is defined
1327 // as deleted if X has:
1328 // -- a non-static data member of const non-class type (or array
1330 if (T
.isConstQualified()) {
1331 data().DefaultedCopyAssignmentIsDeleted
= true;
1332 data().DefaultedMoveAssignmentIsDeleted
= true;
1335 // C++20 [temp.param]p7:
1336 // A structural type is [...] a literal class type [for which] the
1337 // types of all non-static data members are structural types or
1338 // (possibly multidimensional) array thereof
1339 // We deal with class types elsewhere.
1340 if (!T
->isStructuralType())
1341 data().StructuralIfLiteral
= false;
1344 // C++14 [meta.unary.prop]p4:
1345 // T is a class type [...] with [...] no non-static data members other
1346 // than subobjects of zero size
1347 if (data().Empty
&& !IsZeroSize
)
1348 data().Empty
= false;
1351 // Handle using declarations of conversion functions.
1352 if (auto *Shadow
= dyn_cast
<UsingShadowDecl
>(D
)) {
1353 if (Shadow
->getDeclName().getNameKind()
1354 == DeclarationName::CXXConversionFunctionName
) {
1355 ASTContext
&Ctx
= getASTContext();
1356 data().Conversions
.get(Ctx
).addDecl(Ctx
, Shadow
, Shadow
->getAccess());
1360 if (const auto *Using
= dyn_cast
<UsingDecl
>(D
)) {
1361 if (Using
->getDeclName().getNameKind() ==
1362 DeclarationName::CXXConstructorName
) {
1363 data().HasInheritedConstructor
= true;
1364 // C++1z [dcl.init.aggr]p1:
1365 // An aggregate is [...] a class [...] with no inherited constructors
1366 data().Aggregate
= false;
1369 if (Using
->getDeclName().getCXXOverloadedOperator() == OO_Equal
)
1370 data().HasInheritedAssignment
= true;
1374 void CXXRecordDecl::addedSelectedDestructor(CXXDestructorDecl
*DD
) {
1375 DD
->setIneligibleOrNotSelected(false);
1376 addedEligibleSpecialMemberFunction(DD
, SMF_Destructor
);
1379 void CXXRecordDecl::addedEligibleSpecialMemberFunction(const CXXMethodDecl
*MD
,
1381 // FIXME: We shouldn't change DeclaredNonTrivialSpecialMembers if `MD` is
1382 // a function template, but this needs CWG attention before we break ABI.
1383 // See https://github.com/llvm/llvm-project/issues/59206
1385 if (const auto *DD
= dyn_cast
<CXXDestructorDecl
>(MD
)) {
1386 if (DD
->isUserProvided())
1387 data().HasIrrelevantDestructor
= false;
1388 // If the destructor is explicitly defaulted and not trivial or not public
1389 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
1390 // finishedDefaultedOrDeletedMember.
1392 // C++11 [class.dtor]p5:
1393 // A destructor is trivial if [...] the destructor is not virtual.
1394 if (DD
->isVirtual()) {
1395 data().HasTrivialSpecialMembers
&= ~SMF_Destructor
;
1396 data().HasTrivialSpecialMembersForCall
&= ~SMF_Destructor
;
1399 if (DD
->isNoReturn())
1400 data().IsAnyDestructorNoReturn
= true;
1403 if (!MD
->isImplicit() && !MD
->isUserProvided()) {
1404 // This method is user-declared but not user-provided. We can't work
1405 // out whether it's trivial yet (not until we get to the end of the
1406 // class). We'll handle this method in
1407 // finishedDefaultedOrDeletedMember.
1408 } else if (MD
->isTrivial()) {
1409 data().HasTrivialSpecialMembers
|= SMKind
;
1410 data().HasTrivialSpecialMembersForCall
|= SMKind
;
1411 } else if (MD
->isTrivialForCall()) {
1412 data().HasTrivialSpecialMembersForCall
|= SMKind
;
1413 data().DeclaredNonTrivialSpecialMembers
|= SMKind
;
1415 data().DeclaredNonTrivialSpecialMembers
|= SMKind
;
1416 // If this is a user-provided function, do not set
1417 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
1418 // yet whether the method would be considered non-trivial for the
1419 // purpose of calls (attribute "trivial_abi" can be dropped from the
1420 // class later, which can change the special method's triviality).
1421 if (!MD
->isUserProvided())
1422 data().DeclaredNonTrivialSpecialMembersForCall
|= SMKind
;
1426 void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl
*D
) {
1427 assert(!D
->isImplicit() && !D
->isUserProvided());
1429 // The kind of special member this declaration is, if any.
1430 unsigned SMKind
= 0;
1432 if (const auto *Constructor
= dyn_cast
<CXXConstructorDecl
>(D
)) {
1433 if (Constructor
->isDefaultConstructor()) {
1434 SMKind
|= SMF_DefaultConstructor
;
1435 if (Constructor
->isConstexpr())
1436 data().HasConstexprDefaultConstructor
= true;
1438 if (Constructor
->isCopyConstructor())
1439 SMKind
|= SMF_CopyConstructor
;
1440 else if (Constructor
->isMoveConstructor())
1441 SMKind
|= SMF_MoveConstructor
;
1442 else if (Constructor
->isConstexpr())
1443 // We may now know that the constructor is constexpr.
1444 data().HasConstexprNonCopyMoveConstructor
= true;
1445 } else if (isa
<CXXDestructorDecl
>(D
)) {
1446 SMKind
|= SMF_Destructor
;
1447 if (!D
->isTrivial() || D
->getAccess() != AS_public
|| D
->isDeleted())
1448 data().HasIrrelevantDestructor
= false;
1449 } else if (D
->isCopyAssignmentOperator())
1450 SMKind
|= SMF_CopyAssignment
;
1451 else if (D
->isMoveAssignmentOperator())
1452 SMKind
|= SMF_MoveAssignment
;
1454 // Update which trivial / non-trivial special members we have.
1455 // addedMember will have skipped this step for this member.
1456 if (!D
->isIneligibleOrNotSelected()) {
1458 data().HasTrivialSpecialMembers
|= SMKind
;
1460 data().DeclaredNonTrivialSpecialMembers
|= SMKind
;
1464 void CXXRecordDecl::LambdaDefinitionData::AddCaptureList(ASTContext
&Ctx
,
1465 Capture
*CaptureList
) {
1466 Captures
.push_back(CaptureList
);
1467 if (Captures
.size() == 2) {
1468 // The TinyPtrVector member now needs destruction.
1469 Ctx
.addDestruction(&Captures
);
1473 void CXXRecordDecl::setCaptures(ASTContext
&Context
,
1474 ArrayRef
<LambdaCapture
> Captures
) {
1475 CXXRecordDecl::LambdaDefinitionData
&Data
= getLambdaData();
1478 Data
.NumCaptures
= Captures
.size();
1479 Data
.NumExplicitCaptures
= 0;
1480 auto *ToCapture
= (LambdaCapture
*)Context
.Allocate(sizeof(LambdaCapture
) *
1482 Data
.AddCaptureList(Context
, ToCapture
);
1483 for (unsigned I
= 0, N
= Captures
.size(); I
!= N
; ++I
) {
1484 if (Captures
[I
].isExplicit())
1485 ++Data
.NumExplicitCaptures
;
1487 *ToCapture
++ = Captures
[I
];
1490 if (!lambdaIsDefaultConstructibleAndAssignable())
1491 Data
.DefaultedCopyAssignmentIsDeleted
= true;
1494 void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl
*D
) {
1495 unsigned SMKind
= 0;
1497 if (const auto *Constructor
= dyn_cast
<CXXConstructorDecl
>(D
)) {
1498 if (Constructor
->isCopyConstructor())
1499 SMKind
= SMF_CopyConstructor
;
1500 else if (Constructor
->isMoveConstructor())
1501 SMKind
= SMF_MoveConstructor
;
1502 } else if (isa
<CXXDestructorDecl
>(D
))
1503 SMKind
= SMF_Destructor
;
1505 if (D
->isTrivialForCall())
1506 data().HasTrivialSpecialMembersForCall
|= SMKind
;
1508 data().DeclaredNonTrivialSpecialMembersForCall
|= SMKind
;
1511 bool CXXRecordDecl::isCLike() const {
1512 if (getTagKind() == TTK_Class
|| getTagKind() == TTK_Interface
||
1513 !TemplateOrInstantiation
.isNull())
1515 if (!hasDefinition())
1518 return isPOD() && data().HasOnlyCMembers
;
1521 bool CXXRecordDecl::isGenericLambda() const {
1522 if (!isLambda()) return false;
1523 return getLambdaData().IsGenericLambda
;
1527 static bool allLookupResultsAreTheSame(const DeclContext::lookup_result
&R
) {
1529 if (!declaresSameEntity(D
, R
.front()))
1535 static NamedDecl
* getLambdaCallOperatorHelper(const CXXRecordDecl
&RD
) {
1536 if (!RD
.isLambda()) return nullptr;
1537 DeclarationName Name
=
1538 RD
.getASTContext().DeclarationNames
.getCXXOperatorName(OO_Call
);
1539 DeclContext::lookup_result Calls
= RD
.lookup(Name
);
1541 assert(!Calls
.empty() && "Missing lambda call operator!");
1542 assert(allLookupResultsAreTheSame(Calls
) &&
1543 "More than one lambda call operator!");
1544 return Calls
.front();
1547 FunctionTemplateDecl
* CXXRecordDecl::getDependentLambdaCallOperator() const {
1548 NamedDecl
*CallOp
= getLambdaCallOperatorHelper(*this);
1549 return dyn_cast_or_null
<FunctionTemplateDecl
>(CallOp
);
1552 CXXMethodDecl
*CXXRecordDecl::getLambdaCallOperator() const {
1553 NamedDecl
*CallOp
= getLambdaCallOperatorHelper(*this);
1555 if (CallOp
== nullptr)
1558 if (const auto *CallOpTmpl
= dyn_cast
<FunctionTemplateDecl
>(CallOp
))
1559 return cast
<CXXMethodDecl
>(CallOpTmpl
->getTemplatedDecl());
1561 return cast
<CXXMethodDecl
>(CallOp
);
1564 CXXMethodDecl
* CXXRecordDecl::getLambdaStaticInvoker() const {
1565 CXXMethodDecl
*CallOp
= getLambdaCallOperator();
1566 CallingConv CC
= CallOp
->getType()->castAs
<FunctionType
>()->getCallConv();
1567 return getLambdaStaticInvoker(CC
);
1570 static DeclContext::lookup_result
1571 getLambdaStaticInvokers(const CXXRecordDecl
&RD
) {
1572 assert(RD
.isLambda() && "Must be a lambda");
1573 DeclarationName Name
=
1574 &RD
.getASTContext().Idents
.get(getLambdaStaticInvokerName());
1575 return RD
.lookup(Name
);
1578 static CXXMethodDecl
*getInvokerAsMethod(NamedDecl
*ND
) {
1579 if (const auto *InvokerTemplate
= dyn_cast
<FunctionTemplateDecl
>(ND
))
1580 return cast
<CXXMethodDecl
>(InvokerTemplate
->getTemplatedDecl());
1581 return cast
<CXXMethodDecl
>(ND
);
1584 CXXMethodDecl
*CXXRecordDecl::getLambdaStaticInvoker(CallingConv CC
) const {
1587 DeclContext::lookup_result Invoker
= getLambdaStaticInvokers(*this);
1589 for (NamedDecl
*ND
: Invoker
) {
1591 cast
<ValueDecl
>(ND
->getAsFunction())->getType()->castAs
<FunctionType
>();
1592 if (FTy
->getCallConv() == CC
)
1593 return getInvokerAsMethod(ND
);
1599 void CXXRecordDecl::getCaptureFields(
1600 llvm::DenseMap
<const ValueDecl
*, FieldDecl
*> &Captures
,
1601 FieldDecl
*&ThisCapture
) const {
1603 ThisCapture
= nullptr;
1605 LambdaDefinitionData
&Lambda
= getLambdaData();
1606 for (const LambdaCapture
*List
: Lambda
.Captures
) {
1607 RecordDecl::field_iterator Field
= field_begin();
1608 for (const LambdaCapture
*C
= List
, *CEnd
= C
+ Lambda
.NumCaptures
;
1609 C
!= CEnd
; ++C
, ++Field
) {
1610 if (C
->capturesThis())
1611 ThisCapture
= *Field
;
1612 else if (C
->capturesVariable())
1613 Captures
[C
->getCapturedVar()] = *Field
;
1615 assert(Field
== field_end());
1619 TemplateParameterList
*
1620 CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1621 if (!isGenericLambda()) return nullptr;
1622 CXXMethodDecl
*CallOp
= getLambdaCallOperator();
1623 if (FunctionTemplateDecl
*Tmpl
= CallOp
->getDescribedFunctionTemplate())
1624 return Tmpl
->getTemplateParameters();
1628 ArrayRef
<NamedDecl
*>
1629 CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1630 TemplateParameterList
*List
= getGenericLambdaTemplateParameterList();
1634 assert(std::is_partitioned(List
->begin(), List
->end(),
1635 [](const NamedDecl
*D
) { return !D
->isImplicit(); })
1636 && "Explicit template params should be ordered before implicit ones");
1638 const auto ExplicitEnd
= llvm::partition_point(
1639 *List
, [](const NamedDecl
*D
) { return !D
->isImplicit(); });
1640 return llvm::ArrayRef(List
->begin(), ExplicitEnd
);
1643 Decl
*CXXRecordDecl::getLambdaContextDecl() const {
1644 assert(isLambda() && "Not a lambda closure type!");
1645 ExternalASTSource
*Source
= getParentASTContext().getExternalSource();
1646 return getLambdaData().ContextDecl
.get(Source
);
1649 void CXXRecordDecl::setDeviceLambdaManglingNumber(unsigned Num
) const {
1650 assert(isLambda() && "Not a lambda closure type!");
1652 getASTContext().DeviceLambdaManglingNumbers
[this] = Num
;
1655 unsigned CXXRecordDecl::getDeviceLambdaManglingNumber() const {
1656 assert(isLambda() && "Not a lambda closure type!");
1657 auto I
= getASTContext().DeviceLambdaManglingNumbers
.find(this);
1658 if (I
!= getASTContext().DeviceLambdaManglingNumbers
.end())
1663 static CanQualType
GetConversionType(ASTContext
&Context
, NamedDecl
*Conv
) {
1665 cast
<CXXConversionDecl
>(Conv
->getUnderlyingDecl()->getAsFunction())
1666 ->getConversionType();
1667 return Context
.getCanonicalType(T
);
1670 /// Collect the visible conversions of a base class.
1672 /// \param Record a base class of the class we're considering
1673 /// \param InVirtual whether this base class is a virtual base (or a base
1674 /// of a virtual base)
1675 /// \param Access the access along the inheritance path to this base
1676 /// \param ParentHiddenTypes the conversions provided by the inheritors
1678 /// \param Output the set to which to add conversions from non-virtual bases
1679 /// \param VOutput the set to which to add conversions from virtual bases
1680 /// \param HiddenVBaseCs the set of conversions which were hidden in a
1681 /// virtual base along some inheritance path
1682 static void CollectVisibleConversions(
1683 ASTContext
&Context
, const CXXRecordDecl
*Record
, bool InVirtual
,
1684 AccessSpecifier Access
,
1685 const llvm::SmallPtrSet
<CanQualType
, 8> &ParentHiddenTypes
,
1686 ASTUnresolvedSet
&Output
, UnresolvedSetImpl
&VOutput
,
1687 llvm::SmallPtrSet
<NamedDecl
*, 8> &HiddenVBaseCs
) {
1688 // The set of types which have conversions in this class or its
1689 // subclasses. As an optimization, we don't copy the derived set
1690 // unless it might change.
1691 const llvm::SmallPtrSet
<CanQualType
, 8> *HiddenTypes
= &ParentHiddenTypes
;
1692 llvm::SmallPtrSet
<CanQualType
, 8> HiddenTypesBuffer
;
1694 // Collect the direct conversions and figure out which conversions
1695 // will be hidden in the subclasses.
1696 CXXRecordDecl::conversion_iterator ConvI
= Record
->conversion_begin();
1697 CXXRecordDecl::conversion_iterator ConvE
= Record
->conversion_end();
1698 if (ConvI
!= ConvE
) {
1699 HiddenTypesBuffer
= ParentHiddenTypes
;
1700 HiddenTypes
= &HiddenTypesBuffer
;
1702 for (CXXRecordDecl::conversion_iterator I
= ConvI
; I
!= ConvE
; ++I
) {
1703 CanQualType
ConvType(GetConversionType(Context
, I
.getDecl()));
1704 bool Hidden
= ParentHiddenTypes
.count(ConvType
);
1706 HiddenTypesBuffer
.insert(ConvType
);
1708 // If this conversion is hidden and we're in a virtual base,
1709 // remember that it's hidden along some inheritance path.
1710 if (Hidden
&& InVirtual
)
1711 HiddenVBaseCs
.insert(cast
<NamedDecl
>(I
.getDecl()->getCanonicalDecl()));
1713 // If this conversion isn't hidden, add it to the appropriate output.
1715 AccessSpecifier IAccess
1716 = CXXRecordDecl::MergeAccess(Access
, I
.getAccess());
1719 VOutput
.addDecl(I
.getDecl(), IAccess
);
1721 Output
.addDecl(Context
, I
.getDecl(), IAccess
);
1726 // Collect information recursively from any base classes.
1727 for (const auto &I
: Record
->bases()) {
1728 const auto *RT
= I
.getType()->getAs
<RecordType
>();
1731 AccessSpecifier BaseAccess
1732 = CXXRecordDecl::MergeAccess(Access
, I
.getAccessSpecifier());
1733 bool BaseInVirtual
= InVirtual
|| I
.isVirtual();
1735 auto *Base
= cast
<CXXRecordDecl
>(RT
->getDecl());
1736 CollectVisibleConversions(Context
, Base
, BaseInVirtual
, BaseAccess
,
1737 *HiddenTypes
, Output
, VOutput
, HiddenVBaseCs
);
1741 /// Collect the visible conversions of a class.
1743 /// This would be extremely straightforward if it weren't for virtual
1744 /// bases. It might be worth special-casing that, really.
1745 static void CollectVisibleConversions(ASTContext
&Context
,
1746 const CXXRecordDecl
*Record
,
1747 ASTUnresolvedSet
&Output
) {
1748 // The collection of all conversions in virtual bases that we've
1749 // found. These will be added to the output as long as they don't
1750 // appear in the hidden-conversions set.
1751 UnresolvedSet
<8> VBaseCs
;
1753 // The set of conversions in virtual bases that we've determined to
1755 llvm::SmallPtrSet
<NamedDecl
*, 8> HiddenVBaseCs
;
1757 // The set of types hidden by classes derived from this one.
1758 llvm::SmallPtrSet
<CanQualType
, 8> HiddenTypes
;
1760 // Go ahead and collect the direct conversions and add them to the
1761 // hidden-types set.
1762 CXXRecordDecl::conversion_iterator ConvI
= Record
->conversion_begin();
1763 CXXRecordDecl::conversion_iterator ConvE
= Record
->conversion_end();
1764 Output
.append(Context
, ConvI
, ConvE
);
1765 for (; ConvI
!= ConvE
; ++ConvI
)
1766 HiddenTypes
.insert(GetConversionType(Context
, ConvI
.getDecl()));
1768 // Recursively collect conversions from base classes.
1769 for (const auto &I
: Record
->bases()) {
1770 const auto *RT
= I
.getType()->getAs
<RecordType
>();
1773 CollectVisibleConversions(Context
, cast
<CXXRecordDecl
>(RT
->getDecl()),
1774 I
.isVirtual(), I
.getAccessSpecifier(),
1775 HiddenTypes
, Output
, VBaseCs
, HiddenVBaseCs
);
1778 // Add any unhidden conversions provided by virtual bases.
1779 for (UnresolvedSetIterator I
= VBaseCs
.begin(), E
= VBaseCs
.end();
1781 if (!HiddenVBaseCs
.count(cast
<NamedDecl
>(I
.getDecl()->getCanonicalDecl())))
1782 Output
.addDecl(Context
, I
.getDecl(), I
.getAccess());
1786 /// getVisibleConversionFunctions - get all conversion functions visible
1787 /// in current class; including conversion function templates.
1788 llvm::iterator_range
<CXXRecordDecl::conversion_iterator
>
1789 CXXRecordDecl::getVisibleConversionFunctions() const {
1790 ASTContext
&Ctx
= getASTContext();
1792 ASTUnresolvedSet
*Set
;
1793 if (bases_begin() == bases_end()) {
1794 // If root class, all conversions are visible.
1795 Set
= &data().Conversions
.get(Ctx
);
1797 Set
= &data().VisibleConversions
.get(Ctx
);
1798 // If visible conversion list is not evaluated, evaluate it.
1799 if (!data().ComputedVisibleConversions
) {
1800 CollectVisibleConversions(Ctx
, this, *Set
);
1801 data().ComputedVisibleConversions
= true;
1804 return llvm::make_range(Set
->begin(), Set
->end());
1807 void CXXRecordDecl::removeConversion(const NamedDecl
*ConvDecl
) {
1808 // This operation is O(N) but extremely rare. Sema only uses it to
1809 // remove UsingShadowDecls in a class that were followed by a direct
1810 // declaration, e.g.:
1812 // using B::operator int;
1815 // This is uncommon by itself and even more uncommon in conjunction
1816 // with sufficiently large numbers of directly-declared conversions
1817 // that asymptotic behavior matters.
1819 ASTUnresolvedSet
&Convs
= data().Conversions
.get(getASTContext());
1820 for (unsigned I
= 0, E
= Convs
.size(); I
!= E
; ++I
) {
1821 if (Convs
[I
].getDecl() == ConvDecl
) {
1823 assert(!llvm::is_contained(Convs
, ConvDecl
) &&
1824 "conversion was found multiple times in unresolved set");
1829 llvm_unreachable("conversion not found in set!");
1832 CXXRecordDecl
*CXXRecordDecl::getInstantiatedFromMemberClass() const {
1833 if (MemberSpecializationInfo
*MSInfo
= getMemberSpecializationInfo())
1834 return cast
<CXXRecordDecl
>(MSInfo
->getInstantiatedFrom());
1839 MemberSpecializationInfo
*CXXRecordDecl::getMemberSpecializationInfo() const {
1840 return TemplateOrInstantiation
.dyn_cast
<MemberSpecializationInfo
*>();
1844 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl
*RD
,
1845 TemplateSpecializationKind TSK
) {
1846 assert(TemplateOrInstantiation
.isNull() &&
1847 "Previous template or instantiation?");
1848 assert(!isa
<ClassTemplatePartialSpecializationDecl
>(this));
1849 TemplateOrInstantiation
1850 = new (getASTContext()) MemberSpecializationInfo(RD
, TSK
);
1853 ClassTemplateDecl
*CXXRecordDecl::getDescribedClassTemplate() const {
1854 return TemplateOrInstantiation
.dyn_cast
<ClassTemplateDecl
*>();
1857 void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl
*Template
) {
1858 TemplateOrInstantiation
= Template
;
1861 TemplateSpecializationKind
CXXRecordDecl::getTemplateSpecializationKind() const{
1862 if (const auto *Spec
= dyn_cast
<ClassTemplateSpecializationDecl
>(this))
1863 return Spec
->getSpecializationKind();
1865 if (MemberSpecializationInfo
*MSInfo
= getMemberSpecializationInfo())
1866 return MSInfo
->getTemplateSpecializationKind();
1868 return TSK_Undeclared
;
1872 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK
) {
1873 if (auto *Spec
= dyn_cast
<ClassTemplateSpecializationDecl
>(this)) {
1874 Spec
->setSpecializationKind(TSK
);
1878 if (MemberSpecializationInfo
*MSInfo
= getMemberSpecializationInfo()) {
1879 MSInfo
->setTemplateSpecializationKind(TSK
);
1883 llvm_unreachable("Not a class template or member class specialization");
1886 const CXXRecordDecl
*CXXRecordDecl::getTemplateInstantiationPattern() const {
1887 auto GetDefinitionOrSelf
=
1888 [](const CXXRecordDecl
*D
) -> const CXXRecordDecl
* {
1889 if (auto *Def
= D
->getDefinition())
1894 // If it's a class template specialization, find the template or partial
1895 // specialization from which it was instantiated.
1896 if (auto *TD
= dyn_cast
<ClassTemplateSpecializationDecl
>(this)) {
1897 auto From
= TD
->getInstantiatedFrom();
1898 if (auto *CTD
= From
.dyn_cast
<ClassTemplateDecl
*>()) {
1899 while (auto *NewCTD
= CTD
->getInstantiatedFromMemberTemplate()) {
1900 if (NewCTD
->isMemberSpecialization())
1904 return GetDefinitionOrSelf(CTD
->getTemplatedDecl());
1907 From
.dyn_cast
<ClassTemplatePartialSpecializationDecl
*>()) {
1908 while (auto *NewCTPSD
= CTPSD
->getInstantiatedFromMember()) {
1909 if (NewCTPSD
->isMemberSpecialization())
1913 return GetDefinitionOrSelf(CTPSD
);
1917 if (MemberSpecializationInfo
*MSInfo
= getMemberSpecializationInfo()) {
1918 if (isTemplateInstantiation(MSInfo
->getTemplateSpecializationKind())) {
1919 const CXXRecordDecl
*RD
= this;
1920 while (auto *NewRD
= RD
->getInstantiatedFromMemberClass())
1922 return GetDefinitionOrSelf(RD
);
1926 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1927 "couldn't find pattern for class template instantiation");
1931 CXXDestructorDecl
*CXXRecordDecl::getDestructor() const {
1932 ASTContext
&Context
= getASTContext();
1933 QualType ClassType
= Context
.getTypeDeclType(this);
1935 DeclarationName Name
1936 = Context
.DeclarationNames
.getCXXDestructorName(
1937 Context
.getCanonicalType(ClassType
));
1939 DeclContext::lookup_result R
= lookup(Name
);
1941 // If a destructor was marked as not selected, we skip it. We don't always
1942 // have a selected destructor: dependent types, unnamed structs.
1943 for (auto *Decl
: R
) {
1944 auto* DD
= dyn_cast
<CXXDestructorDecl
>(Decl
);
1945 if (DD
&& !DD
->isIneligibleOrNotSelected())
1951 static bool isDeclContextInNamespace(const DeclContext
*DC
) {
1952 while (!DC
->isTranslationUnit()) {
1953 if (DC
->isNamespace())
1955 DC
= DC
->getParent();
1960 bool CXXRecordDecl::isInterfaceLike() const {
1961 assert(hasDefinition() && "checking for interface-like without a definition");
1962 // All __interfaces are inheritently interface-like.
1966 // Interface-like types cannot have a user declared constructor, destructor,
1967 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1968 // cannot be interface types.
1969 if (isLambda() || hasUserDeclaredConstructor() ||
1970 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1971 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1974 // No interface-like type can have a method with a definition.
1975 for (const auto *const Method
: methods())
1976 if (Method
->isDefined() && !Method
->isImplicit())
1979 // Check "Special" types.
1980 const auto *Uuid
= getAttr
<UuidAttr
>();
1981 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1982 // extern C++ block directly in the TU. These are only valid if in one
1983 // of these two situations.
1984 if (Uuid
&& isStruct() && !getDeclContext()->isExternCContext() &&
1985 !isDeclContextInNamespace(getDeclContext()) &&
1986 ((getName() == "IUnknown" &&
1987 Uuid
->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1988 (getName() == "IDispatch" &&
1989 Uuid
->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1990 if (getNumBases() > 0)
1995 // FIXME: Any access specifiers is supposed to make this no longer interface
1998 // If this isn't a 'special' type, it must have a single interface-like base.
1999 if (getNumBases() != 1)
2002 const auto BaseSpec
= *bases_begin();
2003 if (BaseSpec
.isVirtual() || BaseSpec
.getAccessSpecifier() != AS_public
)
2005 const auto *Base
= BaseSpec
.getType()->getAsCXXRecordDecl();
2006 if (Base
->isInterface() || !Base
->isInterfaceLike())
2011 void CXXRecordDecl::completeDefinition() {
2012 completeDefinition(nullptr);
2015 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap
*FinalOverriders
) {
2016 RecordDecl::completeDefinition();
2018 // If the class may be abstract (but hasn't been marked as such), check for
2019 // any pure final overriders.
2020 if (mayBeAbstract()) {
2021 CXXFinalOverriderMap MyFinalOverriders
;
2022 if (!FinalOverriders
) {
2023 getFinalOverriders(MyFinalOverriders
);
2024 FinalOverriders
= &MyFinalOverriders
;
2028 for (CXXFinalOverriderMap::iterator M
= FinalOverriders
->begin(),
2029 MEnd
= FinalOverriders
->end();
2030 M
!= MEnd
&& !Done
; ++M
) {
2031 for (OverridingMethods::iterator SO
= M
->second
.begin(),
2032 SOEnd
= M
->second
.end();
2033 SO
!= SOEnd
&& !Done
; ++SO
) {
2034 assert(SO
->second
.size() > 0 &&
2035 "All virtual functions have overriding virtual functions");
2037 // C++ [class.abstract]p4:
2038 // A class is abstract if it contains or inherits at least one
2039 // pure virtual function for which the final overrider is pure
2041 if (SO
->second
.front().Method
->isPure()) {
2042 data().Abstract
= true;
2050 // Set access bits correctly on the directly-declared conversions.
2051 for (conversion_iterator I
= conversion_begin(), E
= conversion_end();
2053 I
.setAccess((*I
)->getAccess());
2056 bool CXXRecordDecl::mayBeAbstract() const {
2057 if (data().Abstract
|| isInvalidDecl() || !data().Polymorphic
||
2058 isDependentContext())
2061 for (const auto &B
: bases()) {
2062 const auto *BaseDecl
=
2063 cast
<CXXRecordDecl
>(B
.getType()->castAs
<RecordType
>()->getDecl());
2064 if (BaseDecl
->isAbstract())
2071 bool CXXRecordDecl::isEffectivelyFinal() const {
2072 auto *Def
= getDefinition();
2075 if (Def
->hasAttr
<FinalAttr
>())
2077 if (const auto *Dtor
= Def
->getDestructor())
2078 if (Dtor
->hasAttr
<FinalAttr
>())
2083 void CXXDeductionGuideDecl::anchor() {}
2085 bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other
) const {
2086 if ((getKind() != Other
.getKind() ||
2087 getKind() == ExplicitSpecKind::Unresolved
)) {
2088 if (getKind() == ExplicitSpecKind::Unresolved
&&
2089 Other
.getKind() == ExplicitSpecKind::Unresolved
) {
2090 ODRHash SelfHash
, OtherHash
;
2091 SelfHash
.AddStmt(getExpr());
2092 OtherHash
.AddStmt(Other
.getExpr());
2093 return SelfHash
.CalculateHash() == OtherHash
.CalculateHash();
2100 ExplicitSpecifier
ExplicitSpecifier::getFromDecl(FunctionDecl
*Function
) {
2101 switch (Function
->getDeclKind()) {
2102 case Decl::Kind::CXXConstructor
:
2103 return cast
<CXXConstructorDecl
>(Function
)->getExplicitSpecifier();
2104 case Decl::Kind::CXXConversion
:
2105 return cast
<CXXConversionDecl
>(Function
)->getExplicitSpecifier();
2106 case Decl::Kind::CXXDeductionGuide
:
2107 return cast
<CXXDeductionGuideDecl
>(Function
)->getExplicitSpecifier();
2113 CXXDeductionGuideDecl
*
2114 CXXDeductionGuideDecl::Create(ASTContext
&C
, DeclContext
*DC
,
2115 SourceLocation StartLoc
, ExplicitSpecifier ES
,
2116 const DeclarationNameInfo
&NameInfo
, QualType T
,
2117 TypeSourceInfo
*TInfo
, SourceLocation EndLocation
,
2118 CXXConstructorDecl
*Ctor
) {
2119 return new (C
, DC
) CXXDeductionGuideDecl(C
, DC
, StartLoc
, ES
, NameInfo
, T
,
2120 TInfo
, EndLocation
, Ctor
);
2123 CXXDeductionGuideDecl
*CXXDeductionGuideDecl::CreateDeserialized(ASTContext
&C
,
2125 return new (C
, ID
) CXXDeductionGuideDecl(
2126 C
, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
2127 QualType(), nullptr, SourceLocation(), nullptr);
2130 RequiresExprBodyDecl
*RequiresExprBodyDecl::Create(
2131 ASTContext
&C
, DeclContext
*DC
, SourceLocation StartLoc
) {
2132 return new (C
, DC
) RequiresExprBodyDecl(C
, DC
, StartLoc
);
2135 RequiresExprBodyDecl
*RequiresExprBodyDecl::CreateDeserialized(ASTContext
&C
,
2137 return new (C
, ID
) RequiresExprBodyDecl(C
, nullptr, SourceLocation());
2140 void CXXMethodDecl::anchor() {}
2142 bool CXXMethodDecl::isStatic() const {
2143 const CXXMethodDecl
*MD
= getCanonicalDecl();
2145 if (MD
->getStorageClass() == SC_Static
)
2148 OverloadedOperatorKind OOK
= getDeclName().getCXXOverloadedOperator();
2149 return isStaticOverloadedOperator(OOK
);
2152 static bool recursivelyOverrides(const CXXMethodDecl
*DerivedMD
,
2153 const CXXMethodDecl
*BaseMD
) {
2154 for (const CXXMethodDecl
*MD
: DerivedMD
->overridden_methods()) {
2155 if (MD
->getCanonicalDecl() == BaseMD
->getCanonicalDecl())
2157 if (recursivelyOverrides(MD
, BaseMD
))
2164 CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl
*RD
,
2166 if (this->getParent()->getCanonicalDecl() == RD
->getCanonicalDecl())
2169 // Lookup doesn't work for destructors, so handle them separately.
2170 if (isa
<CXXDestructorDecl
>(this)) {
2171 CXXMethodDecl
*MD
= RD
->getDestructor();
2173 if (recursivelyOverrides(MD
, this))
2175 if (MayBeBase
&& recursivelyOverrides(this, MD
))
2181 for (auto *ND
: RD
->lookup(getDeclName())) {
2182 auto *MD
= dyn_cast
<CXXMethodDecl
>(ND
);
2185 if (recursivelyOverrides(MD
, this))
2187 if (MayBeBase
&& recursivelyOverrides(this, MD
))
2195 CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl
*RD
,
2197 if (auto *MD
= getCorrespondingMethodDeclaredInClass(RD
, MayBeBase
))
2200 llvm::SmallVector
<CXXMethodDecl
*, 4> FinalOverriders
;
2201 auto AddFinalOverrider
= [&](CXXMethodDecl
*D
) {
2202 // If this function is overridden by a candidate final overrider, it is not
2203 // a final overrider.
2204 for (CXXMethodDecl
*OtherD
: FinalOverriders
) {
2205 if (declaresSameEntity(D
, OtherD
) || recursivelyOverrides(OtherD
, D
))
2209 // Other candidate final overriders might be overridden by this function.
2210 llvm::erase_if(FinalOverriders
, [&](CXXMethodDecl
*OtherD
) {
2211 return recursivelyOverrides(D
, OtherD
);
2214 FinalOverriders
.push_back(D
);
2217 for (const auto &I
: RD
->bases()) {
2218 const RecordType
*RT
= I
.getType()->getAs
<RecordType
>();
2221 const auto *Base
= cast
<CXXRecordDecl
>(RT
->getDecl());
2222 if (CXXMethodDecl
*D
= this->getCorrespondingMethodInClass(Base
))
2223 AddFinalOverrider(D
);
2226 return FinalOverriders
.size() == 1 ? FinalOverriders
.front() : nullptr;
2230 CXXMethodDecl::Create(ASTContext
&C
, CXXRecordDecl
*RD
, SourceLocation StartLoc
,
2231 const DeclarationNameInfo
&NameInfo
, QualType T
,
2232 TypeSourceInfo
*TInfo
, StorageClass SC
, bool UsesFPIntrin
,
2233 bool isInline
, ConstexprSpecKind ConstexprKind
,
2234 SourceLocation EndLocation
,
2235 Expr
*TrailingRequiresClause
) {
2236 return new (C
, RD
) CXXMethodDecl(
2237 CXXMethod
, C
, RD
, StartLoc
, NameInfo
, T
, TInfo
, SC
, UsesFPIntrin
,
2238 isInline
, ConstexprKind
, EndLocation
, TrailingRequiresClause
);
2241 CXXMethodDecl
*CXXMethodDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
2242 return new (C
, ID
) CXXMethodDecl(
2243 CXXMethod
, C
, nullptr, SourceLocation(), DeclarationNameInfo(),
2244 QualType(), nullptr, SC_None
, false, false,
2245 ConstexprSpecKind::Unspecified
, SourceLocation(), nullptr);
2248 CXXMethodDecl
*CXXMethodDecl::getDevirtualizedMethod(const Expr
*Base
,
2250 assert(isVirtual() && "this method is expected to be virtual");
2252 // When building with -fapple-kext, all calls must go through the vtable since
2253 // the kernel linker can do runtime patching of vtables.
2257 // If the member function is marked 'final', we know that it can't be
2258 // overridden and can therefore devirtualize it unless it's pure virtual.
2259 if (hasAttr
<FinalAttr
>())
2260 return isPure() ? nullptr : this;
2262 // If Base is unknown, we cannot devirtualize.
2266 // If the base expression (after skipping derived-to-base conversions) is a
2267 // class prvalue, then we can devirtualize.
2268 Base
= Base
->getBestDynamicClassTypeExpr();
2269 if (Base
->isPRValue() && Base
->getType()->isRecordType())
2272 // If we don't even know what we would call, we can't devirtualize.
2273 const CXXRecordDecl
*BestDynamicDecl
= Base
->getBestDynamicClassType();
2274 if (!BestDynamicDecl
)
2277 // There may be a method corresponding to MD in a derived class.
2278 CXXMethodDecl
*DevirtualizedMethod
=
2279 getCorrespondingMethodInClass(BestDynamicDecl
);
2281 // If there final overrider in the dynamic type is ambiguous, we can't
2282 // devirtualize this call.
2283 if (!DevirtualizedMethod
)
2286 // If that method is pure virtual, we can't devirtualize. If this code is
2287 // reached, the result would be UB, not a direct call to the derived class
2288 // function, and we can't assume the derived class function is defined.
2289 if (DevirtualizedMethod
->isPure())
2292 // If that method is marked final, we can devirtualize it.
2293 if (DevirtualizedMethod
->hasAttr
<FinalAttr
>())
2294 return DevirtualizedMethod
;
2296 // Similarly, if the class itself or its destructor is marked 'final',
2297 // the class can't be derived from and we can therefore devirtualize the
2298 // member function call.
2299 if (BestDynamicDecl
->isEffectivelyFinal())
2300 return DevirtualizedMethod
;
2302 if (const auto *DRE
= dyn_cast
<DeclRefExpr
>(Base
)) {
2303 if (const auto *VD
= dyn_cast
<VarDecl
>(DRE
->getDecl()))
2304 if (VD
->getType()->isRecordType())
2305 // This is a record decl. We know the type and can devirtualize it.
2306 return DevirtualizedMethod
;
2311 // We can devirtualize calls on an object accessed by a class member access
2312 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2313 // a derived class object constructed in the same location.
2314 if (const auto *ME
= dyn_cast
<MemberExpr
>(Base
)) {
2315 const ValueDecl
*VD
= ME
->getMemberDecl();
2316 return VD
->getType()->isRecordType() ? DevirtualizedMethod
: nullptr;
2319 // Likewise for calls on an object accessed by a (non-reference) pointer to
2321 if (auto *BO
= dyn_cast
<BinaryOperator
>(Base
)) {
2322 if (BO
->isPtrMemOp()) {
2323 auto *MPT
= BO
->getRHS()->getType()->castAs
<MemberPointerType
>();
2324 if (MPT
->getPointeeType()->isRecordType())
2325 return DevirtualizedMethod
;
2329 // We can't devirtualize the call.
2333 bool CXXMethodDecl::isUsualDeallocationFunction(
2334 SmallVectorImpl
<const FunctionDecl
*> &PreventedBy
) const {
2335 assert(PreventedBy
.empty() && "PreventedBy is expected to be empty");
2336 if (getOverloadedOperator() != OO_Delete
&&
2337 getOverloadedOperator() != OO_Array_Delete
)
2340 // C++ [basic.stc.dynamic.deallocation]p2:
2341 // A template instance is never a usual deallocation function,
2342 // regardless of its signature.
2343 if (getPrimaryTemplate())
2346 // C++ [basic.stc.dynamic.deallocation]p2:
2347 // If a class T has a member deallocation function named operator delete
2348 // with exactly one parameter, then that function is a usual (non-placement)
2349 // deallocation function. [...]
2350 if (getNumParams() == 1)
2352 unsigned UsualParams
= 1;
2355 // A destroying operator delete is a usual deallocation function if
2356 // removing the std::destroying_delete_t parameter and changing the
2357 // first parameter type from T* to void* results in the signature of
2358 // a usual deallocation function.
2359 if (isDestroyingOperatorDelete())
2362 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
2363 // [...] If class T does not declare such an operator delete but does
2364 // declare a member deallocation function named operator delete with
2365 // exactly two parameters, the second of which has type std::size_t (18.1),
2366 // then this function is a usual deallocation function.
2368 // C++17 says a usual deallocation function is one with the signature
2369 // (void* [, size_t] [, std::align_val_t] [, ...])
2370 // and all such functions are usual deallocation functions. It's not clear
2371 // that allowing varargs functions was intentional.
2372 ASTContext
&Context
= getASTContext();
2373 if (UsualParams
< getNumParams() &&
2374 Context
.hasSameUnqualifiedType(getParamDecl(UsualParams
)->getType(),
2375 Context
.getSizeType()))
2378 if (UsualParams
< getNumParams() &&
2379 getParamDecl(UsualParams
)->getType()->isAlignValT())
2382 if (UsualParams
!= getNumParams())
2385 // In C++17 onwards, all potential usual deallocation functions are actual
2386 // usual deallocation functions. Honor this behavior when post-C++14
2387 // deallocation functions are offered as extensions too.
2388 // FIXME(EricWF): Destroying Delete should be a language option. How do we
2389 // handle when destroying delete is used prior to C++17?
2390 if (Context
.getLangOpts().CPlusPlus17
||
2391 Context
.getLangOpts().AlignedAllocation
||
2392 isDestroyingOperatorDelete())
2395 // This function is a usual deallocation function if there are no
2396 // single-parameter deallocation functions of the same kind.
2397 DeclContext::lookup_result R
= getDeclContext()->lookup(getDeclName());
2399 for (const auto *D
: R
) {
2400 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
2401 if (FD
->getNumParams() == 1) {
2402 PreventedBy
.push_back(FD
);
2410 bool CXXMethodDecl::isCopyAssignmentOperator() const {
2411 // C++0x [class.copy]p17:
2412 // A user-declared copy assignment operator X::operator= is a non-static
2413 // non-template member function of class X with exactly one parameter of
2414 // type X, X&, const X&, volatile X& or const volatile X&.
2415 if (/*operator=*/getOverloadedOperator() != OO_Equal
||
2416 /*non-static*/ isStatic() ||
2417 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2418 getNumParams() != 1)
2421 QualType ParamType
= getParamDecl(0)->getType();
2422 if (const auto *Ref
= ParamType
->getAs
<LValueReferenceType
>())
2423 ParamType
= Ref
->getPointeeType();
2425 ASTContext
&Context
= getASTContext();
2427 = Context
.getCanonicalType(Context
.getTypeDeclType(getParent()));
2428 return Context
.hasSameUnqualifiedType(ClassType
, ParamType
);
2431 bool CXXMethodDecl::isMoveAssignmentOperator() const {
2432 // C++0x [class.copy]p19:
2433 // A user-declared move assignment operator X::operator= is a non-static
2434 // non-template member function of class X with exactly one parameter of type
2435 // X&&, const X&&, volatile X&&, or const volatile X&&.
2436 if (getOverloadedOperator() != OO_Equal
|| isStatic() ||
2437 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2438 getNumParams() != 1)
2441 QualType ParamType
= getParamDecl(0)->getType();
2442 if (!ParamType
->isRValueReferenceType())
2444 ParamType
= ParamType
->getPointeeType();
2446 ASTContext
&Context
= getASTContext();
2448 = Context
.getCanonicalType(Context
.getTypeDeclType(getParent()));
2449 return Context
.hasSameUnqualifiedType(ClassType
, ParamType
);
2452 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl
*MD
) {
2453 assert(MD
->isCanonicalDecl() && "Method is not canonical!");
2454 assert(!MD
->getParent()->isDependentContext() &&
2455 "Can't add an overridden method to a class template!");
2456 assert(MD
->isVirtual() && "Method is not virtual!");
2458 getASTContext().addOverriddenMethod(this, MD
);
2461 CXXMethodDecl::method_iterator
CXXMethodDecl::begin_overridden_methods() const {
2462 if (isa
<CXXConstructorDecl
>(this)) return nullptr;
2463 return getASTContext().overridden_methods_begin(this);
2466 CXXMethodDecl::method_iterator
CXXMethodDecl::end_overridden_methods() const {
2467 if (isa
<CXXConstructorDecl
>(this)) return nullptr;
2468 return getASTContext().overridden_methods_end(this);
2471 unsigned CXXMethodDecl::size_overridden_methods() const {
2472 if (isa
<CXXConstructorDecl
>(this)) return 0;
2473 return getASTContext().overridden_methods_size(this);
2476 CXXMethodDecl::overridden_method_range
2477 CXXMethodDecl::overridden_methods() const {
2478 if (isa
<CXXConstructorDecl
>(this))
2479 return overridden_method_range(nullptr, nullptr);
2480 return getASTContext().overridden_methods(this);
2483 static QualType
getThisObjectType(ASTContext
&C
, const FunctionProtoType
*FPT
,
2484 const CXXRecordDecl
*Decl
) {
2485 QualType ClassTy
= C
.getTypeDeclType(Decl
);
2486 return C
.getQualifiedType(ClassTy
, FPT
->getMethodQuals());
2489 QualType
CXXMethodDecl::getThisType(const FunctionProtoType
*FPT
,
2490 const CXXRecordDecl
*Decl
) {
2491 ASTContext
&C
= Decl
->getASTContext();
2492 QualType ObjectTy
= ::getThisObjectType(C
, FPT
, Decl
);
2493 return C
.getPointerType(ObjectTy
);
2496 QualType
CXXMethodDecl::getThisObjectType(const FunctionProtoType
*FPT
,
2497 const CXXRecordDecl
*Decl
) {
2498 ASTContext
&C
= Decl
->getASTContext();
2499 return ::getThisObjectType(C
, FPT
, Decl
);
2502 QualType
CXXMethodDecl::getThisType() const {
2503 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2504 // If the member function is declared const, the type of this is const X*,
2505 // if the member function is declared volatile, the type of this is
2506 // volatile X*, and if the member function is declared const volatile,
2507 // the type of this is const volatile X*.
2508 assert(isInstance() && "No 'this' for static methods!");
2509 return CXXMethodDecl::getThisType(getType()->castAs
<FunctionProtoType
>(),
2513 QualType
CXXMethodDecl::getThisObjectType() const {
2514 // Ditto getThisType.
2515 assert(isInstance() && "No 'this' for static methods!");
2516 return CXXMethodDecl::getThisObjectType(
2517 getType()->castAs
<FunctionProtoType
>(), getParent());
2520 bool CXXMethodDecl::hasInlineBody() const {
2521 // If this function is a template instantiation, look at the template from
2522 // which it was instantiated.
2523 const FunctionDecl
*CheckFn
= getTemplateInstantiationPattern();
2527 const FunctionDecl
*fn
;
2528 return CheckFn
->isDefined(fn
) && !fn
->isOutOfLine() &&
2529 (fn
->doesThisDeclarationHaveABody() || fn
->willHaveBody());
2532 bool CXXMethodDecl::isLambdaStaticInvoker() const {
2533 const CXXRecordDecl
*P
= getParent();
2534 return P
->isLambda() && getDeclName().isIdentifier() &&
2535 getName() == getLambdaStaticInvokerName();
2538 CXXCtorInitializer::CXXCtorInitializer(ASTContext
&Context
,
2539 TypeSourceInfo
*TInfo
, bool IsVirtual
,
2540 SourceLocation L
, Expr
*Init
,
2542 SourceLocation EllipsisLoc
)
2543 : Initializee(TInfo
), Init(Init
), MemberOrEllipsisLocation(EllipsisLoc
),
2544 LParenLoc(L
), RParenLoc(R
), IsDelegating(false), IsVirtual(IsVirtual
),
2545 IsWritten(false), SourceOrder(0) {}
2547 CXXCtorInitializer::CXXCtorInitializer(ASTContext
&Context
, FieldDecl
*Member
,
2548 SourceLocation MemberLoc
,
2549 SourceLocation L
, Expr
*Init
,
2551 : Initializee(Member
), Init(Init
), MemberOrEllipsisLocation(MemberLoc
),
2552 LParenLoc(L
), RParenLoc(R
), IsDelegating(false), IsVirtual(false),
2553 IsWritten(false), SourceOrder(0) {}
2555 CXXCtorInitializer::CXXCtorInitializer(ASTContext
&Context
,
2556 IndirectFieldDecl
*Member
,
2557 SourceLocation MemberLoc
,
2558 SourceLocation L
, Expr
*Init
,
2560 : Initializee(Member
), Init(Init
), MemberOrEllipsisLocation(MemberLoc
),
2561 LParenLoc(L
), RParenLoc(R
), IsDelegating(false), IsVirtual(false),
2562 IsWritten(false), SourceOrder(0) {}
2564 CXXCtorInitializer::CXXCtorInitializer(ASTContext
&Context
,
2565 TypeSourceInfo
*TInfo
,
2566 SourceLocation L
, Expr
*Init
,
2568 : Initializee(TInfo
), Init(Init
), LParenLoc(L
), RParenLoc(R
),
2569 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
2571 int64_t CXXCtorInitializer::getID(const ASTContext
&Context
) const {
2572 return Context
.getAllocator()
2573 .identifyKnownAlignedObject
<CXXCtorInitializer
>(this);
2576 TypeLoc
CXXCtorInitializer::getBaseClassLoc() const {
2577 if (isBaseInitializer())
2578 return Initializee
.get
<TypeSourceInfo
*>()->getTypeLoc();
2583 const Type
*CXXCtorInitializer::getBaseClass() const {
2584 if (isBaseInitializer())
2585 return Initializee
.get
<TypeSourceInfo
*>()->getType().getTypePtr();
2590 SourceLocation
CXXCtorInitializer::getSourceLocation() const {
2591 if (isInClassMemberInitializer())
2592 return getAnyMember()->getLocation();
2594 if (isAnyMemberInitializer())
2595 return getMemberLocation();
2597 if (const auto *TSInfo
= Initializee
.get
<TypeSourceInfo
*>())
2598 return TSInfo
->getTypeLoc().getBeginLoc();
2603 SourceRange
CXXCtorInitializer::getSourceRange() const {
2604 if (isInClassMemberInitializer()) {
2605 FieldDecl
*D
= getAnyMember();
2606 if (Expr
*I
= D
->getInClassInitializer())
2607 return I
->getSourceRange();
2611 return SourceRange(getSourceLocation(), getRParenLoc());
2614 CXXConstructorDecl::CXXConstructorDecl(
2615 ASTContext
&C
, CXXRecordDecl
*RD
, SourceLocation StartLoc
,
2616 const DeclarationNameInfo
&NameInfo
, QualType T
, TypeSourceInfo
*TInfo
,
2617 ExplicitSpecifier ES
, bool UsesFPIntrin
, bool isInline
,
2618 bool isImplicitlyDeclared
, ConstexprSpecKind ConstexprKind
,
2619 InheritedConstructor Inherited
, Expr
*TrailingRequiresClause
)
2620 : CXXMethodDecl(CXXConstructor
, C
, RD
, StartLoc
, NameInfo
, T
, TInfo
,
2621 SC_None
, UsesFPIntrin
, isInline
, ConstexprKind
,
2622 SourceLocation(), TrailingRequiresClause
) {
2623 setNumCtorInitializers(0);
2624 setInheritingConstructor(static_cast<bool>(Inherited
));
2625 setImplicit(isImplicitlyDeclared
);
2626 CXXConstructorDeclBits
.HasTrailingExplicitSpecifier
= ES
.getExpr() ? 1 : 0;
2628 *getTrailingObjects
<InheritedConstructor
>() = Inherited
;
2629 setExplicitSpecifier(ES
);
2632 void CXXConstructorDecl::anchor() {}
2634 CXXConstructorDecl
*CXXConstructorDecl::CreateDeserialized(ASTContext
&C
,
2636 uint64_t AllocKind
) {
2637 bool hasTrailingExplicit
= static_cast<bool>(AllocKind
& TAKHasTailExplicit
);
2638 bool isInheritingConstructor
=
2639 static_cast<bool>(AllocKind
& TAKInheritsConstructor
);
2641 additionalSizeToAlloc
<InheritedConstructor
, ExplicitSpecifier
>(
2642 isInheritingConstructor
, hasTrailingExplicit
);
2643 auto *Result
= new (C
, ID
, Extra
) CXXConstructorDecl(
2644 C
, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2645 ExplicitSpecifier(), false, false, false, ConstexprSpecKind::Unspecified
,
2646 InheritedConstructor(), nullptr);
2647 Result
->setInheritingConstructor(isInheritingConstructor
);
2648 Result
->CXXConstructorDeclBits
.HasTrailingExplicitSpecifier
=
2649 hasTrailingExplicit
;
2650 Result
->setExplicitSpecifier(ExplicitSpecifier());
2654 CXXConstructorDecl
*CXXConstructorDecl::Create(
2655 ASTContext
&C
, CXXRecordDecl
*RD
, SourceLocation StartLoc
,
2656 const DeclarationNameInfo
&NameInfo
, QualType T
, TypeSourceInfo
*TInfo
,
2657 ExplicitSpecifier ES
, bool UsesFPIntrin
, bool isInline
,
2658 bool isImplicitlyDeclared
, ConstexprSpecKind ConstexprKind
,
2659 InheritedConstructor Inherited
, Expr
*TrailingRequiresClause
) {
2660 assert(NameInfo
.getName().getNameKind()
2661 == DeclarationName::CXXConstructorName
&&
2662 "Name must refer to a constructor");
2664 additionalSizeToAlloc
<InheritedConstructor
, ExplicitSpecifier
>(
2665 Inherited
? 1 : 0, ES
.getExpr() ? 1 : 0);
2666 return new (C
, RD
, Extra
) CXXConstructorDecl(
2667 C
, RD
, StartLoc
, NameInfo
, T
, TInfo
, ES
, UsesFPIntrin
, isInline
,
2668 isImplicitlyDeclared
, ConstexprKind
, Inherited
, TrailingRequiresClause
);
2671 CXXConstructorDecl::init_const_iterator
CXXConstructorDecl::init_begin() const {
2672 return CtorInitializers
.get(getASTContext().getExternalSource());
2675 CXXConstructorDecl
*CXXConstructorDecl::getTargetConstructor() const {
2676 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2677 Expr
*E
= (*init_begin())->getInit()->IgnoreImplicit();
2678 if (const auto *Construct
= dyn_cast
<CXXConstructExpr
>(E
))
2679 return Construct
->getConstructor();
2684 bool CXXConstructorDecl::isDefaultConstructor() const {
2685 // C++ [class.default.ctor]p1:
2686 // A default constructor for a class X is a constructor of class X for
2687 // which each parameter that is not a function parameter pack has a default
2688 // argument (including the case of a constructor with no parameters)
2689 return getMinRequiredArguments() == 0;
2693 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals
) const {
2694 return isCopyOrMoveConstructor(TypeQuals
) &&
2695 getParamDecl(0)->getType()->isLValueReferenceType();
2698 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals
) const {
2699 return isCopyOrMoveConstructor(TypeQuals
) &&
2700 getParamDecl(0)->getType()->isRValueReferenceType();
2703 /// Determine whether this is a copy or move constructor.
2704 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals
) const {
2705 // C++ [class.copy]p2:
2706 // A non-template constructor for class X is a copy constructor
2707 // if its first parameter is of type X&, const X&, volatile X& or
2708 // const volatile X&, and either there are no other parameters
2709 // or else all other parameters have default arguments (8.3.6).
2710 // C++0x [class.copy]p3:
2711 // A non-template constructor for class X is a move constructor if its
2712 // first parameter is of type X&&, const X&&, volatile X&&, or
2713 // const volatile X&&, and either there are no other parameters or else
2714 // all other parameters have default arguments.
2715 if (!hasOneParamOrDefaultArgs() || getPrimaryTemplate() != nullptr ||
2716 getDescribedFunctionTemplate() != nullptr)
2719 const ParmVarDecl
*Param
= getParamDecl(0);
2721 // Do we have a reference type?
2722 const auto *ParamRefType
= Param
->getType()->getAs
<ReferenceType
>();
2726 // Is it a reference to our class type?
2727 ASTContext
&Context
= getASTContext();
2729 CanQualType PointeeType
2730 = Context
.getCanonicalType(ParamRefType
->getPointeeType());
2732 = Context
.getCanonicalType(Context
.getTagDeclType(getParent()));
2733 if (PointeeType
.getUnqualifiedType() != ClassTy
)
2736 // FIXME: other qualifiers?
2738 // We have a copy or move constructor.
2739 TypeQuals
= PointeeType
.getCVRQualifiers();
2743 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit
) const {
2744 // C++ [class.conv.ctor]p1:
2745 // A constructor declared without the function-specifier explicit
2746 // that can be called with a single parameter specifies a
2747 // conversion from the type of its first parameter to the type of
2748 // its class. Such a constructor is called a converting
2750 if (isExplicit() && !AllowExplicit
)
2753 // FIXME: This has nothing to do with the definition of converting
2754 // constructor, but is convenient for how we use this function in overload
2756 return getNumParams() == 0
2757 ? getType()->castAs
<FunctionProtoType
>()->isVariadic()
2758 : getMinRequiredArguments() <= 1;
2761 bool CXXConstructorDecl::isSpecializationCopyingObject() const {
2762 if (!hasOneParamOrDefaultArgs() || getDescribedFunctionTemplate() != nullptr)
2765 const ParmVarDecl
*Param
= getParamDecl(0);
2767 ASTContext
&Context
= getASTContext();
2768 CanQualType ParamType
= Context
.getCanonicalType(Param
->getType());
2770 // Is it the same as our class type?
2772 = Context
.getCanonicalType(Context
.getTagDeclType(getParent()));
2773 if (ParamType
.getUnqualifiedType() != ClassTy
)
2779 void CXXDestructorDecl::anchor() {}
2782 CXXDestructorDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
2783 return new (C
, ID
) CXXDestructorDecl(
2784 C
, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2785 false, false, false, ConstexprSpecKind::Unspecified
, nullptr);
2788 CXXDestructorDecl
*CXXDestructorDecl::Create(
2789 ASTContext
&C
, CXXRecordDecl
*RD
, SourceLocation StartLoc
,
2790 const DeclarationNameInfo
&NameInfo
, QualType T
, TypeSourceInfo
*TInfo
,
2791 bool UsesFPIntrin
, bool isInline
, bool isImplicitlyDeclared
,
2792 ConstexprSpecKind ConstexprKind
, Expr
*TrailingRequiresClause
) {
2793 assert(NameInfo
.getName().getNameKind()
2794 == DeclarationName::CXXDestructorName
&&
2795 "Name must refer to a destructor");
2796 return new (C
, RD
) CXXDestructorDecl(
2797 C
, RD
, StartLoc
, NameInfo
, T
, TInfo
, UsesFPIntrin
, isInline
,
2798 isImplicitlyDeclared
, ConstexprKind
, TrailingRequiresClause
);
2801 void CXXDestructorDecl::setOperatorDelete(FunctionDecl
*OD
, Expr
*ThisArg
) {
2802 auto *First
= cast
<CXXDestructorDecl
>(getFirstDecl());
2803 if (OD
&& !First
->OperatorDelete
) {
2804 First
->OperatorDelete
= OD
;
2805 First
->OperatorDeleteThisArg
= ThisArg
;
2806 if (auto *L
= getASTMutationListener())
2807 L
->ResolvedOperatorDelete(First
, OD
, ThisArg
);
2811 void CXXConversionDecl::anchor() {}
2814 CXXConversionDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
2815 return new (C
, ID
) CXXConversionDecl(
2816 C
, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2817 false, false, ExplicitSpecifier(), ConstexprSpecKind::Unspecified
,
2818 SourceLocation(), nullptr);
2821 CXXConversionDecl
*CXXConversionDecl::Create(
2822 ASTContext
&C
, CXXRecordDecl
*RD
, SourceLocation StartLoc
,
2823 const DeclarationNameInfo
&NameInfo
, QualType T
, TypeSourceInfo
*TInfo
,
2824 bool UsesFPIntrin
, bool isInline
, ExplicitSpecifier ES
,
2825 ConstexprSpecKind ConstexprKind
, SourceLocation EndLocation
,
2826 Expr
*TrailingRequiresClause
) {
2827 assert(NameInfo
.getName().getNameKind()
2828 == DeclarationName::CXXConversionFunctionName
&&
2829 "Name must refer to a conversion function");
2830 return new (C
, RD
) CXXConversionDecl(
2831 C
, RD
, StartLoc
, NameInfo
, T
, TInfo
, UsesFPIntrin
, isInline
, ES
,
2832 ConstexprKind
, EndLocation
, TrailingRequiresClause
);
2835 bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2836 return isImplicit() && getParent()->isLambda() &&
2837 getConversionType()->isBlockPointerType();
2840 LinkageSpecDecl::LinkageSpecDecl(DeclContext
*DC
, SourceLocation ExternLoc
,
2841 SourceLocation LangLoc
, LanguageIDs lang
,
2843 : Decl(LinkageSpec
, DC
, LangLoc
), DeclContext(LinkageSpec
),
2844 ExternLoc(ExternLoc
), RBraceLoc(SourceLocation()) {
2846 LinkageSpecDeclBits
.HasBraces
= HasBraces
;
2849 void LinkageSpecDecl::anchor() {}
2851 LinkageSpecDecl
*LinkageSpecDecl::Create(ASTContext
&C
,
2853 SourceLocation ExternLoc
,
2854 SourceLocation LangLoc
,
2857 return new (C
, DC
) LinkageSpecDecl(DC
, ExternLoc
, LangLoc
, Lang
, HasBraces
);
2860 LinkageSpecDecl
*LinkageSpecDecl::CreateDeserialized(ASTContext
&C
,
2862 return new (C
, ID
) LinkageSpecDecl(nullptr, SourceLocation(),
2863 SourceLocation(), lang_c
, false);
2866 void UsingDirectiveDecl::anchor() {}
2868 UsingDirectiveDecl
*UsingDirectiveDecl::Create(ASTContext
&C
, DeclContext
*DC
,
2870 SourceLocation NamespaceLoc
,
2871 NestedNameSpecifierLoc QualifierLoc
,
2872 SourceLocation IdentLoc
,
2874 DeclContext
*CommonAncestor
) {
2875 if (auto *NS
= dyn_cast_or_null
<NamespaceDecl
>(Used
))
2876 Used
= NS
->getOriginalNamespace();
2877 return new (C
, DC
) UsingDirectiveDecl(DC
, L
, NamespaceLoc
, QualifierLoc
,
2878 IdentLoc
, Used
, CommonAncestor
);
2881 UsingDirectiveDecl
*UsingDirectiveDecl::CreateDeserialized(ASTContext
&C
,
2883 return new (C
, ID
) UsingDirectiveDecl(nullptr, SourceLocation(),
2885 NestedNameSpecifierLoc(),
2886 SourceLocation(), nullptr, nullptr);
2889 NamespaceDecl
*UsingDirectiveDecl::getNominatedNamespace() {
2890 if (auto *NA
= dyn_cast_or_null
<NamespaceAliasDecl
>(NominatedNamespace
))
2891 return NA
->getNamespace();
2892 return cast_or_null
<NamespaceDecl
>(NominatedNamespace
);
2895 NamespaceDecl::NamespaceDecl(ASTContext
&C
, DeclContext
*DC
, bool Inline
,
2896 SourceLocation StartLoc
, SourceLocation IdLoc
,
2897 IdentifierInfo
*Id
, NamespaceDecl
*PrevDecl
,
2899 : NamedDecl(Namespace
, DC
, IdLoc
, Id
), DeclContext(Namespace
),
2900 redeclarable_base(C
), LocStart(StartLoc
) {
2906 AnonOrFirstNamespaceAndFlags
= {nullptr, Flags
};
2907 setPreviousDecl(PrevDecl
);
2910 AnonOrFirstNamespaceAndFlags
.setPointer(PrevDecl
->getOriginalNamespace());
2913 NamespaceDecl
*NamespaceDecl::Create(ASTContext
&C
, DeclContext
*DC
,
2914 bool Inline
, SourceLocation StartLoc
,
2915 SourceLocation IdLoc
, IdentifierInfo
*Id
,
2916 NamespaceDecl
*PrevDecl
, bool Nested
) {
2918 NamespaceDecl(C
, DC
, Inline
, StartLoc
, IdLoc
, Id
, PrevDecl
, Nested
);
2921 NamespaceDecl
*NamespaceDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
2922 return new (C
, ID
) NamespaceDecl(C
, nullptr, false, SourceLocation(),
2923 SourceLocation(), nullptr, nullptr, false);
2926 NamespaceDecl
*NamespaceDecl::getOriginalNamespace() {
2930 return AnonOrFirstNamespaceAndFlags
.getPointer();
2933 const NamespaceDecl
*NamespaceDecl::getOriginalNamespace() const {
2937 return AnonOrFirstNamespaceAndFlags
.getPointer();
2940 bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2942 NamespaceDecl
*NamespaceDecl::getNextRedeclarationImpl() {
2943 return getNextRedeclaration();
2946 NamespaceDecl
*NamespaceDecl::getPreviousDeclImpl() {
2947 return getPreviousDecl();
2950 NamespaceDecl
*NamespaceDecl::getMostRecentDeclImpl() {
2951 return getMostRecentDecl();
2954 void NamespaceAliasDecl::anchor() {}
2956 NamespaceAliasDecl
*NamespaceAliasDecl::getNextRedeclarationImpl() {
2957 return getNextRedeclaration();
2960 NamespaceAliasDecl
*NamespaceAliasDecl::getPreviousDeclImpl() {
2961 return getPreviousDecl();
2964 NamespaceAliasDecl
*NamespaceAliasDecl::getMostRecentDeclImpl() {
2965 return getMostRecentDecl();
2968 NamespaceAliasDecl
*NamespaceAliasDecl::Create(ASTContext
&C
, DeclContext
*DC
,
2969 SourceLocation UsingLoc
,
2970 SourceLocation AliasLoc
,
2971 IdentifierInfo
*Alias
,
2972 NestedNameSpecifierLoc QualifierLoc
,
2973 SourceLocation IdentLoc
,
2974 NamedDecl
*Namespace
) {
2975 // FIXME: Preserve the aliased namespace as written.
2976 if (auto *NS
= dyn_cast_or_null
<NamespaceDecl
>(Namespace
))
2977 Namespace
= NS
->getOriginalNamespace();
2978 return new (C
, DC
) NamespaceAliasDecl(C
, DC
, UsingLoc
, AliasLoc
, Alias
,
2979 QualifierLoc
, IdentLoc
, Namespace
);
2982 NamespaceAliasDecl
*
2983 NamespaceAliasDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
2984 return new (C
, ID
) NamespaceAliasDecl(C
, nullptr, SourceLocation(),
2985 SourceLocation(), nullptr,
2986 NestedNameSpecifierLoc(),
2987 SourceLocation(), nullptr);
2990 void LifetimeExtendedTemporaryDecl::anchor() {}
2992 /// Retrieve the storage duration for the materialized temporary.
2993 StorageDuration
LifetimeExtendedTemporaryDecl::getStorageDuration() const {
2994 const ValueDecl
*ExtendingDecl
= getExtendingDecl();
2996 return SD_FullExpression
;
2997 // FIXME: This is not necessarily correct for a temporary materialized
2998 // within a default initializer.
2999 if (isa
<FieldDecl
>(ExtendingDecl
))
3000 return SD_Automatic
;
3001 // FIXME: This only works because storage class specifiers are not allowed
3002 // on decomposition declarations.
3003 if (isa
<BindingDecl
>(ExtendingDecl
))
3004 return ExtendingDecl
->getDeclContext()->isFunctionOrMethod() ? SD_Automatic
3006 return cast
<VarDecl
>(ExtendingDecl
)->getStorageDuration();
3009 APValue
*LifetimeExtendedTemporaryDecl::getOrCreateValue(bool MayCreate
) const {
3010 assert(getStorageDuration() == SD_Static
&&
3011 "don't need to cache the computed value for this temporary");
3012 if (MayCreate
&& !Value
) {
3013 Value
= (new (getASTContext()) APValue
);
3014 getASTContext().addDestruction(Value
);
3016 assert(Value
&& "may not be null");
3020 void UsingShadowDecl::anchor() {}
3022 UsingShadowDecl::UsingShadowDecl(Kind K
, ASTContext
&C
, DeclContext
*DC
,
3023 SourceLocation Loc
, DeclarationName Name
,
3024 BaseUsingDecl
*Introducer
, NamedDecl
*Target
)
3025 : NamedDecl(K
, DC
, Loc
, Name
), redeclarable_base(C
),
3026 UsingOrNextShadow(Introducer
) {
3028 assert(!isa
<UsingShadowDecl
>(Target
));
3029 setTargetDecl(Target
);
3034 UsingShadowDecl::UsingShadowDecl(Kind K
, ASTContext
&C
, EmptyShell Empty
)
3035 : NamedDecl(K
, nullptr, SourceLocation(), DeclarationName()),
3036 redeclarable_base(C
) {}
3039 UsingShadowDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3040 return new (C
, ID
) UsingShadowDecl(UsingShadow
, C
, EmptyShell());
3043 BaseUsingDecl
*UsingShadowDecl::getIntroducer() const {
3044 const UsingShadowDecl
*Shadow
= this;
3045 while (const auto *NextShadow
=
3046 dyn_cast
<UsingShadowDecl
>(Shadow
->UsingOrNextShadow
))
3047 Shadow
= NextShadow
;
3048 return cast
<BaseUsingDecl
>(Shadow
->UsingOrNextShadow
);
3051 void ConstructorUsingShadowDecl::anchor() {}
3053 ConstructorUsingShadowDecl
*
3054 ConstructorUsingShadowDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3055 SourceLocation Loc
, UsingDecl
*Using
,
3056 NamedDecl
*Target
, bool IsVirtual
) {
3057 return new (C
, DC
) ConstructorUsingShadowDecl(C
, DC
, Loc
, Using
, Target
,
3061 ConstructorUsingShadowDecl
*
3062 ConstructorUsingShadowDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3063 return new (C
, ID
) ConstructorUsingShadowDecl(C
, EmptyShell());
3066 CXXRecordDecl
*ConstructorUsingShadowDecl::getNominatedBaseClass() const {
3067 return getIntroducer()->getQualifier()->getAsRecordDecl();
3070 void BaseUsingDecl::anchor() {}
3072 void BaseUsingDecl::addShadowDecl(UsingShadowDecl
*S
) {
3073 assert(!llvm::is_contained(shadows(), S
) && "declaration already in set");
3074 assert(S
->getIntroducer() == this);
3076 if (FirstUsingShadow
.getPointer())
3077 S
->UsingOrNextShadow
= FirstUsingShadow
.getPointer();
3078 FirstUsingShadow
.setPointer(S
);
3081 void BaseUsingDecl::removeShadowDecl(UsingShadowDecl
*S
) {
3082 assert(llvm::is_contained(shadows(), S
) && "declaration not in set");
3083 assert(S
->getIntroducer() == this);
3085 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
3087 if (FirstUsingShadow
.getPointer() == S
) {
3088 FirstUsingShadow
.setPointer(
3089 dyn_cast
<UsingShadowDecl
>(S
->UsingOrNextShadow
));
3090 S
->UsingOrNextShadow
= this;
3094 UsingShadowDecl
*Prev
= FirstUsingShadow
.getPointer();
3095 while (Prev
->UsingOrNextShadow
!= S
)
3096 Prev
= cast
<UsingShadowDecl
>(Prev
->UsingOrNextShadow
);
3097 Prev
->UsingOrNextShadow
= S
->UsingOrNextShadow
;
3098 S
->UsingOrNextShadow
= this;
3101 void UsingDecl::anchor() {}
3103 UsingDecl
*UsingDecl::Create(ASTContext
&C
, DeclContext
*DC
, SourceLocation UL
,
3104 NestedNameSpecifierLoc QualifierLoc
,
3105 const DeclarationNameInfo
&NameInfo
,
3107 return new (C
, DC
) UsingDecl(DC
, UL
, QualifierLoc
, NameInfo
, HasTypename
);
3110 UsingDecl
*UsingDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3111 return new (C
, ID
) UsingDecl(nullptr, SourceLocation(),
3112 NestedNameSpecifierLoc(), DeclarationNameInfo(),
3116 SourceRange
UsingDecl::getSourceRange() const {
3117 SourceLocation Begin
= isAccessDeclaration()
3118 ? getQualifierLoc().getBeginLoc() : UsingLocation
;
3119 return SourceRange(Begin
, getNameInfo().getEndLoc());
3122 void UsingEnumDecl::anchor() {}
3124 UsingEnumDecl
*UsingEnumDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3128 TypeSourceInfo
*EnumType
) {
3129 assert(isa
<EnumDecl
>(EnumType
->getType()->getAsTagDecl()));
3131 UsingEnumDecl(DC
, EnumType
->getType()->getAsTagDecl()->getDeclName(), UL
, EL
, NL
, EnumType
);
3134 UsingEnumDecl
*UsingEnumDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3136 UsingEnumDecl(nullptr, DeclarationName(), SourceLocation(),
3137 SourceLocation(), SourceLocation(), nullptr);
3140 SourceRange
UsingEnumDecl::getSourceRange() const {
3141 return SourceRange(UsingLocation
, EnumType
->getTypeLoc().getEndLoc());
3144 void UsingPackDecl::anchor() {}
3146 UsingPackDecl
*UsingPackDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3147 NamedDecl
*InstantiatedFrom
,
3148 ArrayRef
<NamedDecl
*> UsingDecls
) {
3149 size_t Extra
= additionalSizeToAlloc
<NamedDecl
*>(UsingDecls
.size());
3150 return new (C
, DC
, Extra
) UsingPackDecl(DC
, InstantiatedFrom
, UsingDecls
);
3153 UsingPackDecl
*UsingPackDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
,
3154 unsigned NumExpansions
) {
3155 size_t Extra
= additionalSizeToAlloc
<NamedDecl
*>(NumExpansions
);
3157 new (C
, ID
, Extra
) UsingPackDecl(nullptr, nullptr, std::nullopt
);
3158 Result
->NumExpansions
= NumExpansions
;
3159 auto *Trail
= Result
->getTrailingObjects
<NamedDecl
*>();
3160 for (unsigned I
= 0; I
!= NumExpansions
; ++I
)
3161 new (Trail
+ I
) NamedDecl
*(nullptr);
3165 void UnresolvedUsingValueDecl::anchor() {}
3167 UnresolvedUsingValueDecl
*
3168 UnresolvedUsingValueDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3169 SourceLocation UsingLoc
,
3170 NestedNameSpecifierLoc QualifierLoc
,
3171 const DeclarationNameInfo
&NameInfo
,
3172 SourceLocation EllipsisLoc
) {
3173 return new (C
, DC
) UnresolvedUsingValueDecl(DC
, C
.DependentTy
, UsingLoc
,
3174 QualifierLoc
, NameInfo
,
3178 UnresolvedUsingValueDecl
*
3179 UnresolvedUsingValueDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3180 return new (C
, ID
) UnresolvedUsingValueDecl(nullptr, QualType(),
3182 NestedNameSpecifierLoc(),
3183 DeclarationNameInfo(),
3187 SourceRange
UnresolvedUsingValueDecl::getSourceRange() const {
3188 SourceLocation Begin
= isAccessDeclaration()
3189 ? getQualifierLoc().getBeginLoc() : UsingLocation
;
3190 return SourceRange(Begin
, getNameInfo().getEndLoc());
3193 void UnresolvedUsingTypenameDecl::anchor() {}
3195 UnresolvedUsingTypenameDecl
*
3196 UnresolvedUsingTypenameDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3197 SourceLocation UsingLoc
,
3198 SourceLocation TypenameLoc
,
3199 NestedNameSpecifierLoc QualifierLoc
,
3200 SourceLocation TargetNameLoc
,
3201 DeclarationName TargetName
,
3202 SourceLocation EllipsisLoc
) {
3203 return new (C
, DC
) UnresolvedUsingTypenameDecl(
3204 DC
, UsingLoc
, TypenameLoc
, QualifierLoc
, TargetNameLoc
,
3205 TargetName
.getAsIdentifierInfo(), EllipsisLoc
);
3208 UnresolvedUsingTypenameDecl
*
3209 UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3210 return new (C
, ID
) UnresolvedUsingTypenameDecl(
3211 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
3212 SourceLocation(), nullptr, SourceLocation());
3215 UnresolvedUsingIfExistsDecl
*
3216 UnresolvedUsingIfExistsDecl::Create(ASTContext
&Ctx
, DeclContext
*DC
,
3217 SourceLocation Loc
, DeclarationName Name
) {
3218 return new (Ctx
, DC
) UnresolvedUsingIfExistsDecl(DC
, Loc
, Name
);
3221 UnresolvedUsingIfExistsDecl
*
3222 UnresolvedUsingIfExistsDecl::CreateDeserialized(ASTContext
&Ctx
, unsigned ID
) {
3223 return new (Ctx
, ID
)
3224 UnresolvedUsingIfExistsDecl(nullptr, SourceLocation(), DeclarationName());
3227 UnresolvedUsingIfExistsDecl::UnresolvedUsingIfExistsDecl(DeclContext
*DC
,
3229 DeclarationName Name
)
3230 : NamedDecl(Decl::UnresolvedUsingIfExists
, DC
, Loc
, Name
) {}
3232 void UnresolvedUsingIfExistsDecl::anchor() {}
3234 void StaticAssertDecl::anchor() {}
3236 StaticAssertDecl
*StaticAssertDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3237 SourceLocation StaticAssertLoc
,
3239 StringLiteral
*Message
,
3240 SourceLocation RParenLoc
,
3242 return new (C
, DC
) StaticAssertDecl(DC
, StaticAssertLoc
, AssertExpr
, Message
,
3246 StaticAssertDecl
*StaticAssertDecl::CreateDeserialized(ASTContext
&C
,
3248 return new (C
, ID
) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
3249 nullptr, SourceLocation(), false);
3252 VarDecl
*ValueDecl::getPotentiallyDecomposedVarDecl() {
3253 assert((isa
<VarDecl
, BindingDecl
>(this)) &&
3254 "expected a VarDecl or a BindingDecl");
3255 if (auto *Var
= llvm::dyn_cast
<VarDecl
>(this))
3257 if (auto *BD
= llvm::dyn_cast
<BindingDecl
>(this))
3258 return llvm::dyn_cast
<VarDecl
>(BD
->getDecomposedDecl());
3262 void BindingDecl::anchor() {}
3264 BindingDecl
*BindingDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3265 SourceLocation IdLoc
, IdentifierInfo
*Id
) {
3266 return new (C
, DC
) BindingDecl(DC
, IdLoc
, Id
);
3269 BindingDecl
*BindingDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3270 return new (C
, ID
) BindingDecl(nullptr, SourceLocation(), nullptr);
3273 VarDecl
*BindingDecl::getHoldingVar() const {
3274 Expr
*B
= getBinding();
3277 auto *DRE
= dyn_cast
<DeclRefExpr
>(B
->IgnoreImplicit());
3281 auto *VD
= cast
<VarDecl
>(DRE
->getDecl());
3282 assert(VD
->isImplicit() && "holding var for binding decl not implicit");
3286 void DecompositionDecl::anchor() {}
3288 DecompositionDecl
*DecompositionDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3289 SourceLocation StartLoc
,
3290 SourceLocation LSquareLoc
,
3291 QualType T
, TypeSourceInfo
*TInfo
,
3293 ArrayRef
<BindingDecl
*> Bindings
) {
3294 size_t Extra
= additionalSizeToAlloc
<BindingDecl
*>(Bindings
.size());
3295 return new (C
, DC
, Extra
)
3296 DecompositionDecl(C
, DC
, StartLoc
, LSquareLoc
, T
, TInfo
, SC
, Bindings
);
3299 DecompositionDecl
*DecompositionDecl::CreateDeserialized(ASTContext
&C
,
3301 unsigned NumBindings
) {
3302 size_t Extra
= additionalSizeToAlloc
<BindingDecl
*>(NumBindings
);
3303 auto *Result
= new (C
, ID
, Extra
)
3304 DecompositionDecl(C
, nullptr, SourceLocation(), SourceLocation(),
3305 QualType(), nullptr, StorageClass(), std::nullopt
);
3306 // Set up and clean out the bindings array.
3307 Result
->NumBindings
= NumBindings
;
3308 auto *Trail
= Result
->getTrailingObjects
<BindingDecl
*>();
3309 for (unsigned I
= 0; I
!= NumBindings
; ++I
)
3310 new (Trail
+ I
) BindingDecl
*(nullptr);
3314 void DecompositionDecl::printName(llvm::raw_ostream
&OS
,
3315 const PrintingPolicy
&Policy
) const {
3318 for (const auto *B
: bindings()) {
3321 B
->printName(OS
, Policy
);
3327 void MSPropertyDecl::anchor() {}
3329 MSPropertyDecl
*MSPropertyDecl::Create(ASTContext
&C
, DeclContext
*DC
,
3330 SourceLocation L
, DeclarationName N
,
3331 QualType T
, TypeSourceInfo
*TInfo
,
3332 SourceLocation StartL
,
3333 IdentifierInfo
*Getter
,
3334 IdentifierInfo
*Setter
) {
3335 return new (C
, DC
) MSPropertyDecl(DC
, L
, N
, T
, TInfo
, StartL
, Getter
, Setter
);
3338 MSPropertyDecl
*MSPropertyDecl::CreateDeserialized(ASTContext
&C
,
3340 return new (C
, ID
) MSPropertyDecl(nullptr, SourceLocation(),
3341 DeclarationName(), QualType(), nullptr,
3342 SourceLocation(), nullptr, nullptr);
3345 void MSGuidDecl::anchor() {}
3347 MSGuidDecl::MSGuidDecl(DeclContext
*DC
, QualType T
, Parts P
)
3348 : ValueDecl(Decl::MSGuid
, DC
, SourceLocation(), DeclarationName(), T
),
3351 MSGuidDecl
*MSGuidDecl::Create(const ASTContext
&C
, QualType T
, Parts P
) {
3352 DeclContext
*DC
= C
.getTranslationUnitDecl();
3353 return new (C
, DC
) MSGuidDecl(DC
, T
, P
);
3356 MSGuidDecl
*MSGuidDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3357 return new (C
, ID
) MSGuidDecl(nullptr, QualType(), Parts());
3360 void MSGuidDecl::printName(llvm::raw_ostream
&OS
,
3361 const PrintingPolicy
&) const {
3362 OS
<< llvm::format("GUID{%08" PRIx32
"-%04" PRIx16
"-%04" PRIx16
"-",
3363 PartVal
.Part1
, PartVal
.Part2
, PartVal
.Part3
);
3365 for (uint8_t Byte
: PartVal
.Part4And5
) {
3366 OS
<< llvm::format("%02" PRIx8
, Byte
);
3373 /// Determine if T is a valid 'struct _GUID' of the shape that we expect.
3374 static bool isValidStructGUID(ASTContext
&Ctx
, QualType T
) {
3375 // FIXME: We only need to check this once, not once each time we compute a
3377 using MatcherRef
= llvm::function_ref
<bool(QualType
)>;
3379 auto IsInt
= [&Ctx
](unsigned N
) {
3380 return [&Ctx
, N
](QualType T
) {
3381 return T
->isUnsignedIntegerOrEnumerationType() &&
3382 Ctx
.getIntWidth(T
) == N
;
3386 auto IsArray
= [&Ctx
](MatcherRef Elem
, unsigned N
) {
3387 return [&Ctx
, Elem
, N
](QualType T
) {
3388 const ConstantArrayType
*CAT
= Ctx
.getAsConstantArrayType(T
);
3389 return CAT
&& CAT
->getSize() == N
&& Elem(CAT
->getElementType());
3393 auto IsStruct
= [](std::initializer_list
<MatcherRef
> Fields
) {
3394 return [Fields
](QualType T
) {
3395 const RecordDecl
*RD
= T
->getAsRecordDecl();
3396 if (!RD
|| RD
->isUnion())
3398 RD
= RD
->getDefinition();
3401 if (auto *CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
))
3402 if (CXXRD
->getNumBases())
3404 auto MatcherIt
= Fields
.begin();
3405 for (const FieldDecl
*FD
: RD
->fields()) {
3406 if (FD
->isUnnamedBitfield()) continue;
3407 if (FD
->isBitField() || MatcherIt
== Fields
.end() ||
3408 !(*MatcherIt
)(FD
->getType()))
3412 return MatcherIt
== Fields
.end();
3416 // We expect an {i32, i16, i16, [8 x i8]}.
3417 return IsStruct({IsInt(32), IsInt(16), IsInt(16), IsArray(IsInt(8), 8)})(T
);
3420 APValue
&MSGuidDecl::getAsAPValue() const {
3421 if (APVal
.isAbsent() && isValidStructGUID(getASTContext(), getType())) {
3424 APVal
= APValue(APValue::UninitStruct(), 0, 4);
3425 APVal
.getStructField(0) = APValue(APSInt(APInt(32, PartVal
.Part1
), true));
3426 APVal
.getStructField(1) = APValue(APSInt(APInt(16, PartVal
.Part2
), true));
3427 APVal
.getStructField(2) = APValue(APSInt(APInt(16, PartVal
.Part3
), true));
3428 APValue
&Arr
= APVal
.getStructField(3) =
3429 APValue(APValue::UninitArray(), 8, 8);
3430 for (unsigned I
= 0; I
!= 8; ++I
) {
3431 Arr
.getArrayInitializedElt(I
) =
3432 APValue(APSInt(APInt(8, PartVal
.Part4And5
[I
]), true));
3434 // Register this APValue to be destroyed if necessary. (Note that the
3435 // MSGuidDecl destructor is never run.)
3436 getASTContext().addDestruction(&APVal
);
3442 void UnnamedGlobalConstantDecl::anchor() {}
3444 UnnamedGlobalConstantDecl::UnnamedGlobalConstantDecl(const ASTContext
&C
,
3448 : ValueDecl(Decl::UnnamedGlobalConstant
, DC
, SourceLocation(),
3449 DeclarationName(), Ty
),
3451 // Cleanup the embedded APValue if required (note that our destructor is never
3453 if (Value
.needsCleanup())
3454 C
.addDestruction(&Value
);
3457 UnnamedGlobalConstantDecl
*
3458 UnnamedGlobalConstantDecl::Create(const ASTContext
&C
, QualType T
,
3459 const APValue
&Value
) {
3460 DeclContext
*DC
= C
.getTranslationUnitDecl();
3461 return new (C
, DC
) UnnamedGlobalConstantDecl(C
, DC
, T
, Value
);
3464 UnnamedGlobalConstantDecl
*
3465 UnnamedGlobalConstantDecl::CreateDeserialized(ASTContext
&C
, unsigned ID
) {
3467 UnnamedGlobalConstantDecl(C
, nullptr, QualType(), APValue());
3470 void UnnamedGlobalConstantDecl::printName(llvm::raw_ostream
&OS
,
3471 const PrintingPolicy
&) const {
3472 OS
<< "unnamed-global-constant";
3475 static const char *getAccessName(AccessSpecifier AS
) {
3478 llvm_unreachable("Invalid access specifier!");
3486 llvm_unreachable("Invalid access specifier!");
3489 const StreamingDiagnostic
&clang::operator<<(const StreamingDiagnostic
&DB
,
3490 AccessSpecifier AS
) {
3491 return DB
<< getAccessName(AS
);