1 //===- DeclCXX.h - Classes for representing C++ declarations --*- C++ -*-=====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the C++ Decl subclasses, other than those for templates
12 /// (found in DeclTemplate.h) and friends (in DeclFriend.h).
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_AST_DECLCXX_H
17 #define LLVM_CLANG_AST_DECLCXX_H
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTUnresolvedSet.h"
21 #include "clang/AST/Attr.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclarationName.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/ExternalASTSource.h"
26 #include "clang/AST/LambdaCapture.h"
27 #include "clang/AST/NestedNameSpecifier.h"
28 #include "clang/AST/Redeclarable.h"
29 #include "clang/AST/Stmt.h"
30 #include "clang/AST/Type.h"
31 #include "clang/AST/TypeLoc.h"
32 #include "clang/AST/UnresolvedSet.h"
33 #include "clang/Basic/LLVM.h"
34 #include "clang/Basic/Lambda.h"
35 #include "clang/Basic/LangOptions.h"
36 #include "clang/Basic/OperatorKinds.h"
37 #include "clang/Basic/SourceLocation.h"
38 #include "clang/Basic/Specifiers.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/DenseMap.h"
41 #include "llvm/ADT/PointerIntPair.h"
42 #include "llvm/ADT/PointerUnion.h"
43 #include "llvm/ADT/STLExtras.h"
44 #include "llvm/ADT/iterator_range.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Compiler.h"
47 #include "llvm/Support/PointerLikeTypeTraits.h"
48 #include "llvm/Support/TrailingObjects.h"
57 class ClassTemplateDecl;
58 class ConstructorUsingShadowDecl;
61 class CXXConstructorDecl;
62 class CXXDestructorDecl;
63 class CXXFinalOverriderMap;
64 class CXXIndirectPrimaryBaseSet;
66 class DiagnosticBuilder;
68 class FunctionTemplateDecl;
70 class MemberSpecializationInfo;
72 class TemplateParameterList;
75 /// \brief Represents any kind of function declaration, whether it is a
76 /// concrete function or a function template.
77 class AnyFunctionDecl {
80 AnyFunctionDecl(NamedDecl *ND) : Function(ND) {}
83 AnyFunctionDecl(FunctionDecl *FD) : Function(FD) {}
84 AnyFunctionDecl(FunctionTemplateDecl *FTD);
86 /// \brief Implicily converts any function or function template into a
87 /// named declaration.
88 operator NamedDecl *() const { return Function; }
90 /// \brief Retrieve the underlying function or function template.
91 NamedDecl *get() const { return Function; }
93 static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
94 return AnyFunctionDecl(ND);
102 // Provide PointerLikeTypeTraits for non-cvr pointers.
104 struct PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
105 static void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
109 static ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
110 return ::clang::AnyFunctionDecl::getFromNamedDecl(
111 static_cast< ::clang::NamedDecl*>(P));
114 enum { NumLowBitsAvailable = 2 };
121 /// \brief Represents an access specifier followed by colon ':'.
123 /// An objects of this class represents sugar for the syntactic occurrence
124 /// of an access specifier followed by a colon in the list of member
125 /// specifiers of a C++ class definition.
127 /// Note that they do not represent other uses of access specifiers,
128 /// such as those occurring in a list of base specifiers.
129 /// Also note that this class has nothing to do with so-called
130 /// "access declarations" (C++98 11.3 [class.access.dcl]).
131 class AccessSpecDecl : public Decl {
132 /// \brief The location of the ':'.
133 SourceLocation ColonLoc;
135 AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
136 SourceLocation ASLoc, SourceLocation ColonLoc)
137 : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
141 AccessSpecDecl(EmptyShell Empty) : Decl(AccessSpec, Empty) {}
143 virtual void anchor();
146 /// \brief The location of the access specifier.
147 SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
149 /// \brief Sets the location of the access specifier.
150 void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
152 /// \brief The location of the colon following the access specifier.
153 SourceLocation getColonLoc() const { return ColonLoc; }
155 /// \brief Sets the location of the colon.
156 void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
158 SourceRange getSourceRange() const override LLVM_READONLY {
159 return SourceRange(getAccessSpecifierLoc(), getColonLoc());
162 static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
163 DeclContext *DC, SourceLocation ASLoc,
164 SourceLocation ColonLoc) {
165 return new (C, DC) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
168 static AccessSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
170 // Implement isa/cast/dyncast/etc.
171 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
172 static bool classofKind(Kind K) { return K == AccessSpec; }
175 /// \brief Represents a base class of a C++ class.
177 /// Each CXXBaseSpecifier represents a single, direct base class (or
178 /// struct) of a C++ class (or struct). It specifies the type of that
179 /// base class, whether it is a virtual or non-virtual base, and what
180 /// level of access (public, protected, private) is used for the
181 /// derivation. For example:
186 /// class C : public virtual A, protected B { };
189 /// In this code, C will have two CXXBaseSpecifiers, one for "public
190 /// virtual A" and the other for "protected B".
191 class CXXBaseSpecifier {
192 /// \brief The source code range that covers the full base
193 /// specifier, including the "virtual" (if present) and access
194 /// specifier (if present).
197 /// \brief The source location of the ellipsis, if this is a pack
199 SourceLocation EllipsisLoc;
201 /// \brief Whether this is a virtual base class or not.
202 unsigned Virtual : 1;
204 /// \brief Whether this is the base of a class (true) or of a struct (false).
206 /// This determines the mapping from the access specifier as written in the
207 /// source code to the access specifier used for semantic analysis.
208 unsigned BaseOfClass : 1;
210 /// \brief Access specifier as written in the source code (may be AS_none).
212 /// The actual type of data stored here is an AccessSpecifier, but we use
213 /// "unsigned" here to work around a VC++ bug.
216 /// \brief Whether the class contains a using declaration
217 /// to inherit the named class's constructors.
218 unsigned InheritConstructors : 1;
220 /// \brief The type of the base class.
222 /// This will be a class or struct (or a typedef of such). The source code
223 /// range does not include the \c virtual or the access specifier.
224 TypeSourceInfo *BaseTypeInfo;
227 CXXBaseSpecifier() = default;
228 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A,
229 TypeSourceInfo *TInfo, SourceLocation EllipsisLoc)
230 : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC),
231 Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) {}
233 /// \brief Retrieves the source range that contains the entire base specifier.
234 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
235 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
236 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
238 /// \brief Get the location at which the base class type was written.
239 SourceLocation getBaseTypeLoc() const LLVM_READONLY {
240 return BaseTypeInfo->getTypeLoc().getLocStart();
243 /// \brief Determines whether the base class is a virtual base class (or not).
244 bool isVirtual() const { return Virtual; }
246 /// \brief Determine whether this base class is a base of a class declared
247 /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
248 bool isBaseOfClass() const { return BaseOfClass; }
250 /// \brief Determine whether this base specifier is a pack expansion.
251 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
253 /// \brief Determine whether this base class's constructors get inherited.
254 bool getInheritConstructors() const { return InheritConstructors; }
256 /// \brief Set that this base class's constructors should be inherited.
257 void setInheritConstructors(bool Inherit = true) {
258 InheritConstructors = Inherit;
261 /// \brief For a pack expansion, determine the location of the ellipsis.
262 SourceLocation getEllipsisLoc() const {
266 /// \brief Returns the access specifier for this base specifier.
268 /// This is the actual base specifier as used for semantic analysis, so
269 /// the result can never be AS_none. To retrieve the access specifier as
270 /// written in the source code, use getAccessSpecifierAsWritten().
271 AccessSpecifier getAccessSpecifier() const {
272 if ((AccessSpecifier)Access == AS_none)
273 return BaseOfClass? AS_private : AS_public;
275 return (AccessSpecifier)Access;
278 /// \brief Retrieves the access specifier as written in the source code
279 /// (which may mean that no access specifier was explicitly written).
281 /// Use getAccessSpecifier() to retrieve the access specifier for use in
282 /// semantic analysis.
283 AccessSpecifier getAccessSpecifierAsWritten() const {
284 return (AccessSpecifier)Access;
287 /// \brief Retrieves the type of the base class.
289 /// This type will always be an unqualified class type.
290 QualType getType() const {
291 return BaseTypeInfo->getType().getUnqualifiedType();
294 /// \brief Retrieves the type and source location of the base class.
295 TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; }
298 /// \brief Represents a C++ struct/union/class.
299 class CXXRecordDecl : public RecordDecl {
300 friend class ASTDeclReader;
301 friend class ASTDeclWriter;
302 friend class ASTNodeImporter;
303 friend class ASTReader;
304 friend class ASTRecordWriter;
305 friend class ASTWriter;
306 friend class DeclContext;
307 friend class LambdaExpr;
309 friend void FunctionDecl::setPure(bool);
310 friend void TagDecl::startDefinition();
312 /// Values used in DefinitionData fields to represent special members.
313 enum SpecialMemberFlags {
314 SMF_DefaultConstructor = 0x1,
315 SMF_CopyConstructor = 0x2,
316 SMF_MoveConstructor = 0x4,
317 SMF_CopyAssignment = 0x8,
318 SMF_MoveAssignment = 0x10,
319 SMF_Destructor = 0x20,
323 struct DefinitionData {
324 /// \brief True if this class has any user-declared constructors.
325 unsigned UserDeclaredConstructor : 1;
327 /// \brief The user-declared special members which this class has.
328 unsigned UserDeclaredSpecialMembers : 6;
330 /// \brief True when this class is an aggregate.
331 unsigned Aggregate : 1;
333 /// \brief True when this class is a POD-type.
334 unsigned PlainOldData : 1;
336 /// true when this class is empty for traits purposes,
337 /// i.e. has no data members other than 0-width bit-fields, has no
338 /// virtual function/base, and doesn't inherit from a non-empty
339 /// class. Doesn't take union-ness into account.
342 /// \brief True when this class is polymorphic, i.e., has at
343 /// least one virtual member or derives from a polymorphic class.
344 unsigned Polymorphic : 1;
346 /// \brief True when this class is abstract, i.e., has at least
347 /// one pure virtual function, (that can come from a base class).
348 unsigned Abstract : 1;
350 /// \brief True when this class has standard layout.
352 /// C++11 [class]p7. A standard-layout class is a class that:
353 /// * has no non-static data members of type non-standard-layout class (or
354 /// array of such types) or reference,
355 /// * has no virtual functions (10.3) and no virtual base classes (10.1),
356 /// * has the same access control (Clause 11) for all non-static data
358 /// * has no non-standard-layout base classes,
359 /// * either has no non-static data members in the most derived class and at
360 /// most one base class with non-static data members, or has no base
361 /// classes with non-static data members, and
362 /// * has no base classes of the same type as the first non-static data
364 unsigned IsStandardLayout : 1;
366 /// \brief True when there are no non-empty base classes.
368 /// This is a helper bit of state used to implement IsStandardLayout more
370 unsigned HasNoNonEmptyBases : 1;
372 /// \brief True when there are private non-static data members.
373 unsigned HasPrivateFields : 1;
375 /// \brief True when there are protected non-static data members.
376 unsigned HasProtectedFields : 1;
378 /// \brief True when there are private non-static data members.
379 unsigned HasPublicFields : 1;
381 /// \brief True if this class (or any subobject) has mutable fields.
382 unsigned HasMutableFields : 1;
384 /// \brief True if this class (or any nested anonymous struct or union)
385 /// has variant members.
386 unsigned HasVariantMembers : 1;
388 /// \brief True if there no non-field members declared by the user.
389 unsigned HasOnlyCMembers : 1;
391 /// \brief True if any field has an in-class initializer, including those
392 /// within anonymous unions or structs.
393 unsigned HasInClassInitializer : 1;
395 /// \brief True if any field is of reference type, and does not have an
396 /// in-class initializer.
398 /// In this case, value-initialization of this class is illegal in C++98
399 /// even if the class has a trivial default constructor.
400 unsigned HasUninitializedReferenceMember : 1;
402 /// \brief True if any non-mutable field whose type doesn't have a user-
403 /// provided default ctor also doesn't have an in-class initializer.
404 unsigned HasUninitializedFields : 1;
406 /// \brief True if there are any member using-declarations that inherit
407 /// constructors from a base class.
408 unsigned HasInheritedConstructor : 1;
410 /// \brief True if there are any member using-declarations named
412 unsigned HasInheritedAssignment : 1;
414 /// \brief These flags are \c true if a defaulted corresponding special
415 /// member can't be fully analyzed without performing overload resolution.
417 unsigned NeedOverloadResolutionForCopyConstructor : 1;
418 unsigned NeedOverloadResolutionForMoveConstructor : 1;
419 unsigned NeedOverloadResolutionForMoveAssignment : 1;
420 unsigned NeedOverloadResolutionForDestructor : 1;
423 /// \brief These flags are \c true if an implicit defaulted corresponding
424 /// special member would be defined as deleted.
426 unsigned DefaultedCopyConstructorIsDeleted : 1;
427 unsigned DefaultedMoveConstructorIsDeleted : 1;
428 unsigned DefaultedMoveAssignmentIsDeleted : 1;
429 unsigned DefaultedDestructorIsDeleted : 1;
432 /// \brief The trivial special members which this class has, per
433 /// C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25,
434 /// C++11 [class.dtor]p5, or would have if the member were not suppressed.
436 /// This excludes any user-declared but not user-provided special members
437 /// which have been declared but not yet defined.
438 unsigned HasTrivialSpecialMembers : 6;
440 /// \brief The declared special members of this class which are known to be
443 /// This excludes any user-declared but not user-provided special members
444 /// which have been declared but not yet defined, and any implicit special
445 /// members which have not yet been declared.
446 unsigned DeclaredNonTrivialSpecialMembers : 6;
448 /// \brief True when this class has a destructor with no semantic effect.
449 unsigned HasIrrelevantDestructor : 1;
451 /// \brief True when this class has at least one user-declared constexpr
452 /// constructor which is neither the copy nor move constructor.
453 unsigned HasConstexprNonCopyMoveConstructor : 1;
455 /// \brief True if this class has a (possibly implicit) defaulted default
457 unsigned HasDefaultedDefaultConstructor : 1;
459 /// \brief True if this class can be passed in a non-address-preserving
460 /// fashion (such as in registers) according to the C++ language rules.
461 /// This does not imply anything about how the ABI in use will actually
462 /// pass an object of this class.
463 unsigned CanPassInRegisters : 1;
465 /// \brief True if a defaulted default constructor for this class would
467 unsigned DefaultedDefaultConstructorIsConstexpr : 1;
469 /// \brief True if this class has a constexpr default constructor.
471 /// This is true for either a user-declared constexpr default constructor
472 /// or an implicitly declared constexpr default constructor.
473 unsigned HasConstexprDefaultConstructor : 1;
475 /// \brief True when this class contains at least one non-static data
476 /// member or base class of non-literal or volatile type.
477 unsigned HasNonLiteralTypeFieldsOrBases : 1;
479 /// \brief True when visible conversion functions are already computed
480 /// and are available.
481 unsigned ComputedVisibleConversions : 1;
483 /// \brief Whether we have a C++11 user-provided default constructor (not
484 /// explicitly deleted or defaulted).
485 unsigned UserProvidedDefaultConstructor : 1;
487 /// \brief The special members which have been declared for this class,
488 /// either by the user or implicitly.
489 unsigned DeclaredSpecialMembers : 6;
491 /// \brief Whether an implicit copy constructor could have a const-qualified
492 /// parameter, for initializing virtual bases and for other subobjects.
493 unsigned ImplicitCopyConstructorCanHaveConstParamForVBase : 1;
494 unsigned ImplicitCopyConstructorCanHaveConstParamForNonVBase : 1;
496 /// \brief Whether an implicit copy assignment operator would have a
497 /// const-qualified parameter.
498 unsigned ImplicitCopyAssignmentHasConstParam : 1;
500 /// \brief Whether any declared copy constructor has a const-qualified
502 unsigned HasDeclaredCopyConstructorWithConstParam : 1;
504 /// \brief Whether any declared copy assignment operator has either a
505 /// const-qualified reference parameter or a non-reference parameter.
506 unsigned HasDeclaredCopyAssignmentWithConstParam : 1;
508 /// \brief Whether this class describes a C++ lambda.
509 unsigned IsLambda : 1;
511 /// \brief Whether we are currently parsing base specifiers.
512 unsigned IsParsingBaseSpecifiers : 1;
514 unsigned HasODRHash : 1;
516 /// \brief A hash of parts of the class to help in ODR checking.
517 unsigned ODRHash = 0;
519 /// \brief The number of base class specifiers in Bases.
520 unsigned NumBases = 0;
522 /// \brief The number of virtual base class specifiers in VBases.
523 unsigned NumVBases = 0;
525 /// \brief Base classes of this class.
527 /// FIXME: This is wasted space for a union.
528 LazyCXXBaseSpecifiersPtr Bases;
530 /// \brief direct and indirect virtual base classes of this class.
531 LazyCXXBaseSpecifiersPtr VBases;
533 /// \brief The conversion functions of this C++ class (but not its
534 /// inherited conversion functions).
536 /// Each of the entries in this overload set is a CXXConversionDecl.
537 LazyASTUnresolvedSet Conversions;
539 /// \brief The conversion functions of this C++ class and all those
540 /// inherited conversion functions that are visible in this class.
542 /// Each of the entries in this overload set is a CXXConversionDecl or a
543 /// FunctionTemplateDecl.
544 LazyASTUnresolvedSet VisibleConversions;
546 /// \brief The declaration which defines this record.
547 CXXRecordDecl *Definition;
549 /// \brief The first friend declaration in this class, or null if there
552 /// This is actually currently stored in reverse order.
553 LazyDeclPtr FirstFriend;
555 DefinitionData(CXXRecordDecl *D);
557 /// \brief Retrieve the set of direct base classes.
558 CXXBaseSpecifier *getBases() const {
559 if (!Bases.isOffset())
560 return Bases.get(nullptr);
561 return getBasesSlowCase();
564 /// \brief Retrieve the set of virtual base classes.
565 CXXBaseSpecifier *getVBases() const {
566 if (!VBases.isOffset())
567 return VBases.get(nullptr);
568 return getVBasesSlowCase();
571 ArrayRef<CXXBaseSpecifier> bases() const {
572 return llvm::makeArrayRef(getBases(), NumBases);
575 ArrayRef<CXXBaseSpecifier> vbases() const {
576 return llvm::makeArrayRef(getVBases(), NumVBases);
580 CXXBaseSpecifier *getBasesSlowCase() const;
581 CXXBaseSpecifier *getVBasesSlowCase() const;
584 struct DefinitionData *DefinitionData;
586 /// \brief Describes a C++ closure type (generated by a lambda expression).
587 struct LambdaDefinitionData : public DefinitionData {
588 using Capture = LambdaCapture;
590 /// \brief Whether this lambda is known to be dependent, even if its
591 /// context isn't dependent.
593 /// A lambda with a non-dependent context can be dependent if it occurs
594 /// within the default argument of a function template, because the
595 /// lambda will have been created with the enclosing context as its
596 /// declaration context, rather than function. This is an unfortunate
597 /// artifact of having to parse the default arguments before.
598 unsigned Dependent : 1;
600 /// \brief Whether this lambda is a generic lambda.
601 unsigned IsGenericLambda : 1;
603 /// \brief The Default Capture.
604 unsigned CaptureDefault : 2;
606 /// \brief The number of captures in this lambda is limited 2^NumCaptures.
607 unsigned NumCaptures : 15;
609 /// \brief The number of explicit captures in this lambda.
610 unsigned NumExplicitCaptures : 13;
612 /// \brief The number used to indicate this lambda expression for name
613 /// mangling in the Itanium C++ ABI.
614 unsigned ManglingNumber = 0;
616 /// \brief The declaration that provides context for this lambda, if the
617 /// actual DeclContext does not suffice. This is used for lambdas that
618 /// occur within default arguments of function parameters within the class
619 /// or within a data member initializer.
620 LazyDeclPtr ContextDecl;
622 /// \brief The list of captures, both explicit and implicit, for this
624 Capture *Captures = nullptr;
626 /// \brief The type of the call method.
627 TypeSourceInfo *MethodTyInfo;
629 LambdaDefinitionData(CXXRecordDecl *D, TypeSourceInfo *Info,
630 bool Dependent, bool IsGeneric,
631 LambdaCaptureDefault CaptureDefault)
632 : DefinitionData(D), Dependent(Dependent), IsGenericLambda(IsGeneric),
633 CaptureDefault(CaptureDefault), NumCaptures(0), NumExplicitCaptures(0),
637 // C++1z [expr.prim.lambda]p4:
638 // This class type is not an aggregate type.
640 PlainOldData = false;
644 struct DefinitionData *dataPtr() const {
645 // Complete the redecl chain (if necessary).
647 return DefinitionData;
650 struct DefinitionData &data() const {
651 auto *DD = dataPtr();
652 assert(DD && "queried property of class with no definition");
656 struct LambdaDefinitionData &getLambdaData() const {
657 // No update required: a merged definition cannot change any lambda
659 auto *DD = DefinitionData;
660 assert(DD && DD->IsLambda && "queried lambda property of non-lambda class");
661 return static_cast<LambdaDefinitionData&>(*DD);
664 /// \brief The template or declaration that this declaration
665 /// describes or was instantiated from, respectively.
667 /// For non-templates, this value will be null. For record
668 /// declarations that describe a class template, this will be a
669 /// pointer to a ClassTemplateDecl. For member
670 /// classes of class template specializations, this will be the
671 /// MemberSpecializationInfo referring to the member class that was
672 /// instantiated or specialized.
673 llvm::PointerUnion<ClassTemplateDecl *, MemberSpecializationInfo *>
674 TemplateOrInstantiation;
676 /// \brief Called from setBases and addedMember to notify the class that a
677 /// direct or virtual base class or a member of class type has been added.
678 void addedClassSubobject(CXXRecordDecl *Base);
680 /// \brief Notify the class that member has been added.
682 /// This routine helps maintain information about the class based on which
683 /// members have been added. It will be invoked by DeclContext::addDecl()
684 /// whenever a member is added to this record.
685 void addedMember(Decl *D);
687 void markedVirtualFunctionPure();
689 /// \brief Get the head of our list of friend declarations, possibly
690 /// deserializing the friends from an external AST source.
691 FriendDecl *getFirstFriend() const;
694 CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C, DeclContext *DC,
695 SourceLocation StartLoc, SourceLocation IdLoc,
696 IdentifierInfo *Id, CXXRecordDecl *PrevDecl);
699 /// \brief Iterator that traverses the base classes of a class.
700 using base_class_iterator = CXXBaseSpecifier *;
702 /// \brief Iterator that traverses the base classes of a class.
703 using base_class_const_iterator = const CXXBaseSpecifier *;
705 CXXRecordDecl *getCanonicalDecl() override {
706 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
709 const CXXRecordDecl *getCanonicalDecl() const {
710 return const_cast<CXXRecordDecl*>(this)->getCanonicalDecl();
713 CXXRecordDecl *getPreviousDecl() {
714 return cast_or_null<CXXRecordDecl>(
715 static_cast<RecordDecl *>(this)->getPreviousDecl());
718 const CXXRecordDecl *getPreviousDecl() const {
719 return const_cast<CXXRecordDecl*>(this)->getPreviousDecl();
722 CXXRecordDecl *getMostRecentDecl() {
723 return cast<CXXRecordDecl>(
724 static_cast<RecordDecl *>(this)->getMostRecentDecl());
727 const CXXRecordDecl *getMostRecentDecl() const {
728 return const_cast<CXXRecordDecl*>(this)->getMostRecentDecl();
731 CXXRecordDecl *getDefinition() const {
732 // We only need an update if we don't already know which
733 // declaration is the definition.
734 auto *DD = DefinitionData ? DefinitionData : dataPtr();
735 return DD ? DD->Definition : nullptr;
738 bool hasDefinition() const { return DefinitionData || dataPtr(); }
740 static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
741 SourceLocation StartLoc, SourceLocation IdLoc,
743 CXXRecordDecl *PrevDecl = nullptr,
744 bool DelayTypeCreation = false);
745 static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC,
746 TypeSourceInfo *Info, SourceLocation Loc,
747 bool DependentLambda, bool IsGeneric,
748 LambdaCaptureDefault CaptureDefault);
749 static CXXRecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
751 bool isDynamicClass() const {
752 return data().Polymorphic || data().NumVBases != 0;
755 void setIsParsingBaseSpecifiers() { data().IsParsingBaseSpecifiers = true; }
757 bool isParsingBaseSpecifiers() const {
758 return data().IsParsingBaseSpecifiers;
761 unsigned getODRHash() const;
763 /// \brief Sets the base classes of this struct or class.
764 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
766 /// \brief Retrieves the number of base classes of this class.
767 unsigned getNumBases() const { return data().NumBases; }
769 using base_class_range = llvm::iterator_range<base_class_iterator>;
770 using base_class_const_range =
771 llvm::iterator_range<base_class_const_iterator>;
773 base_class_range bases() {
774 return base_class_range(bases_begin(), bases_end());
776 base_class_const_range bases() const {
777 return base_class_const_range(bases_begin(), bases_end());
780 base_class_iterator bases_begin() { return data().getBases(); }
781 base_class_const_iterator bases_begin() const { return data().getBases(); }
782 base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
783 base_class_const_iterator bases_end() const {
784 return bases_begin() + data().NumBases;
787 /// \brief Retrieves the number of virtual base classes of this class.
788 unsigned getNumVBases() const { return data().NumVBases; }
790 base_class_range vbases() {
791 return base_class_range(vbases_begin(), vbases_end());
793 base_class_const_range vbases() const {
794 return base_class_const_range(vbases_begin(), vbases_end());
797 base_class_iterator vbases_begin() { return data().getVBases(); }
798 base_class_const_iterator vbases_begin() const { return data().getVBases(); }
799 base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
800 base_class_const_iterator vbases_end() const {
801 return vbases_begin() + data().NumVBases;
804 /// \brief Determine whether this class has any dependent base classes which
805 /// are not the current instantiation.
806 bool hasAnyDependentBases() const;
808 /// Iterator access to method members. The method iterator visits
809 /// all method members of the class, including non-instance methods,
810 /// special methods, etc.
811 using method_iterator = specific_decl_iterator<CXXMethodDecl>;
813 llvm::iterator_range<specific_decl_iterator<CXXMethodDecl>>;
815 method_range methods() const {
816 return method_range(method_begin(), method_end());
819 /// \brief Method begin iterator. Iterates in the order the methods
821 method_iterator method_begin() const {
822 return method_iterator(decls_begin());
825 /// \brief Method past-the-end iterator.
826 method_iterator method_end() const {
827 return method_iterator(decls_end());
830 /// Iterator access to constructor members.
831 using ctor_iterator = specific_decl_iterator<CXXConstructorDecl>;
833 llvm::iterator_range<specific_decl_iterator<CXXConstructorDecl>>;
835 ctor_range ctors() const { return ctor_range(ctor_begin(), ctor_end()); }
837 ctor_iterator ctor_begin() const {
838 return ctor_iterator(decls_begin());
841 ctor_iterator ctor_end() const {
842 return ctor_iterator(decls_end());
845 /// An iterator over friend declarations. All of these are defined
847 class friend_iterator;
848 using friend_range = llvm::iterator_range<friend_iterator>;
850 friend_range friends() const;
851 friend_iterator friend_begin() const;
852 friend_iterator friend_end() const;
853 void pushFriendDecl(FriendDecl *FD);
855 /// Determines whether this record has any friends.
856 bool hasFriends() const {
857 return data().FirstFriend.isValid();
860 /// \brief \c true if a defaulted copy constructor for this class would be
862 bool defaultedCopyConstructorIsDeleted() const {
863 assert((!needsOverloadResolutionForCopyConstructor() ||
864 (data().DeclaredSpecialMembers & SMF_CopyConstructor)) &&
865 "this property has not yet been computed by Sema");
866 return data().DefaultedCopyConstructorIsDeleted;
869 /// \brief \c true if a defaulted move constructor for this class would be
871 bool defaultedMoveConstructorIsDeleted() const {
872 assert((!needsOverloadResolutionForMoveConstructor() ||
873 (data().DeclaredSpecialMembers & SMF_MoveConstructor)) &&
874 "this property has not yet been computed by Sema");
875 return data().DefaultedMoveConstructorIsDeleted;
878 /// \brief \c true if a defaulted destructor for this class would be deleted.
879 bool defaultedDestructorIsDeleted() const {
880 assert((!needsOverloadResolutionForDestructor() ||
881 (data().DeclaredSpecialMembers & SMF_Destructor)) &&
882 "this property has not yet been computed by Sema");
883 return data().DefaultedDestructorIsDeleted;
886 /// \brief \c true if we know for sure that this class has a single,
887 /// accessible, unambiguous copy constructor that is not deleted.
888 bool hasSimpleCopyConstructor() const {
889 return !hasUserDeclaredCopyConstructor() &&
890 !data().DefaultedCopyConstructorIsDeleted;
893 /// \brief \c true if we know for sure that this class has a single,
894 /// accessible, unambiguous move constructor that is not deleted.
895 bool hasSimpleMoveConstructor() const {
896 return !hasUserDeclaredMoveConstructor() && hasMoveConstructor() &&
897 !data().DefaultedMoveConstructorIsDeleted;
900 /// \brief \c true if we know for sure that this class has a single,
901 /// accessible, unambiguous move assignment operator that is not deleted.
902 bool hasSimpleMoveAssignment() const {
903 return !hasUserDeclaredMoveAssignment() && hasMoveAssignment() &&
904 !data().DefaultedMoveAssignmentIsDeleted;
907 /// \brief \c true if we know for sure that this class has an accessible
908 /// destructor that is not deleted.
909 bool hasSimpleDestructor() const {
910 return !hasUserDeclaredDestructor() &&
911 !data().DefaultedDestructorIsDeleted;
914 /// \brief Determine whether this class has any default constructors.
915 bool hasDefaultConstructor() const {
916 return (data().DeclaredSpecialMembers & SMF_DefaultConstructor) ||
917 needsImplicitDefaultConstructor();
920 /// \brief Determine if we need to declare a default constructor for
923 /// This value is used for lazy creation of default constructors.
924 bool needsImplicitDefaultConstructor() const {
925 return !data().UserDeclaredConstructor &&
926 !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) &&
927 // C++14 [expr.prim.lambda]p20:
928 // The closure type associated with a lambda-expression has no
929 // default constructor.
933 /// \brief Determine whether this class has any user-declared constructors.
935 /// When true, a default constructor will not be implicitly declared.
936 bool hasUserDeclaredConstructor() const {
937 return data().UserDeclaredConstructor;
940 /// \brief Whether this class has a user-provided default constructor
942 bool hasUserProvidedDefaultConstructor() const {
943 return data().UserProvidedDefaultConstructor;
946 /// \brief Determine whether this class has a user-declared copy constructor.
948 /// When false, a copy constructor will be implicitly declared.
949 bool hasUserDeclaredCopyConstructor() const {
950 return data().UserDeclaredSpecialMembers & SMF_CopyConstructor;
953 /// \brief Determine whether this class needs an implicit copy
954 /// constructor to be lazily declared.
955 bool needsImplicitCopyConstructor() const {
956 return !(data().DeclaredSpecialMembers & SMF_CopyConstructor);
959 /// \brief Determine whether we need to eagerly declare a defaulted copy
960 /// constructor for this class.
961 bool needsOverloadResolutionForCopyConstructor() const {
962 // C++17 [class.copy.ctor]p6:
963 // If the class definition declares a move constructor or move assignment
964 // operator, the implicitly declared copy constructor is defined as
966 // In MSVC mode, sometimes a declared move assignment does not delete an
967 // implicit copy constructor, so defer this choice to Sema.
968 if (data().UserDeclaredSpecialMembers &
969 (SMF_MoveConstructor | SMF_MoveAssignment))
971 return data().NeedOverloadResolutionForCopyConstructor;
974 /// \brief Determine whether an implicit copy constructor for this type
975 /// would have a parameter with a const-qualified reference type.
976 bool implicitCopyConstructorHasConstParam() const {
977 return data().ImplicitCopyConstructorCanHaveConstParamForNonVBase &&
979 data().ImplicitCopyConstructorCanHaveConstParamForVBase);
982 /// \brief Determine whether this class has a copy constructor with
983 /// a parameter type which is a reference to a const-qualified type.
984 bool hasCopyConstructorWithConstParam() const {
985 return data().HasDeclaredCopyConstructorWithConstParam ||
986 (needsImplicitCopyConstructor() &&
987 implicitCopyConstructorHasConstParam());
990 /// \brief Whether this class has a user-declared move constructor or
991 /// assignment operator.
993 /// When false, a move constructor and assignment operator may be
994 /// implicitly declared.
995 bool hasUserDeclaredMoveOperation() const {
996 return data().UserDeclaredSpecialMembers &
997 (SMF_MoveConstructor | SMF_MoveAssignment);
1000 /// \brief Determine whether this class has had a move constructor
1001 /// declared by the user.
1002 bool hasUserDeclaredMoveConstructor() const {
1003 return data().UserDeclaredSpecialMembers & SMF_MoveConstructor;
1006 /// \brief Determine whether this class has a move constructor.
1007 bool hasMoveConstructor() const {
1008 return (data().DeclaredSpecialMembers & SMF_MoveConstructor) ||
1009 needsImplicitMoveConstructor();
1012 /// \brief Set that we attempted to declare an implicit copy
1013 /// constructor, but overload resolution failed so we deleted it.
1014 void setImplicitCopyConstructorIsDeleted() {
1015 assert((data().DefaultedCopyConstructorIsDeleted ||
1016 needsOverloadResolutionForCopyConstructor()) &&
1017 "Copy constructor should not be deleted");
1018 data().DefaultedCopyConstructorIsDeleted = true;
1021 /// \brief Set that we attempted to declare an implicit move
1022 /// constructor, but overload resolution failed so we deleted it.
1023 void setImplicitMoveConstructorIsDeleted() {
1024 assert((data().DefaultedMoveConstructorIsDeleted ||
1025 needsOverloadResolutionForMoveConstructor()) &&
1026 "move constructor should not be deleted");
1027 data().DefaultedMoveConstructorIsDeleted = true;
1030 /// \brief Set that we attempted to declare an implicit destructor,
1031 /// but overload resolution failed so we deleted it.
1032 void setImplicitDestructorIsDeleted() {
1033 assert((data().DefaultedDestructorIsDeleted ||
1034 needsOverloadResolutionForDestructor()) &&
1035 "destructor should not be deleted");
1036 data().DefaultedDestructorIsDeleted = true;
1039 /// \brief Determine whether this class should get an implicit move
1040 /// constructor or if any existing special member function inhibits this.
1041 bool needsImplicitMoveConstructor() const {
1042 return !(data().DeclaredSpecialMembers & SMF_MoveConstructor) &&
1043 !hasUserDeclaredCopyConstructor() &&
1044 !hasUserDeclaredCopyAssignment() &&
1045 !hasUserDeclaredMoveAssignment() &&
1046 !hasUserDeclaredDestructor();
1049 /// \brief Determine whether we need to eagerly declare a defaulted move
1050 /// constructor for this class.
1051 bool needsOverloadResolutionForMoveConstructor() const {
1052 return data().NeedOverloadResolutionForMoveConstructor;
1055 /// \brief Determine whether this class has a user-declared copy assignment
1058 /// When false, a copy assigment operator will be implicitly declared.
1059 bool hasUserDeclaredCopyAssignment() const {
1060 return data().UserDeclaredSpecialMembers & SMF_CopyAssignment;
1063 /// \brief Determine whether this class needs an implicit copy
1064 /// assignment operator to be lazily declared.
1065 bool needsImplicitCopyAssignment() const {
1066 return !(data().DeclaredSpecialMembers & SMF_CopyAssignment);
1069 /// \brief Determine whether we need to eagerly declare a defaulted copy
1070 /// assignment operator for this class.
1071 bool needsOverloadResolutionForCopyAssignment() const {
1072 return data().HasMutableFields;
1075 /// \brief Determine whether an implicit copy assignment operator for this
1076 /// type would have a parameter with a const-qualified reference type.
1077 bool implicitCopyAssignmentHasConstParam() const {
1078 return data().ImplicitCopyAssignmentHasConstParam;
1081 /// \brief Determine whether this class has a copy assignment operator with
1082 /// a parameter type which is a reference to a const-qualified type or is not
1084 bool hasCopyAssignmentWithConstParam() const {
1085 return data().HasDeclaredCopyAssignmentWithConstParam ||
1086 (needsImplicitCopyAssignment() &&
1087 implicitCopyAssignmentHasConstParam());
1090 /// \brief Determine whether this class has had a move assignment
1091 /// declared by the user.
1092 bool hasUserDeclaredMoveAssignment() const {
1093 return data().UserDeclaredSpecialMembers & SMF_MoveAssignment;
1096 /// \brief Determine whether this class has a move assignment operator.
1097 bool hasMoveAssignment() const {
1098 return (data().DeclaredSpecialMembers & SMF_MoveAssignment) ||
1099 needsImplicitMoveAssignment();
1102 /// \brief Set that we attempted to declare an implicit move assignment
1103 /// operator, but overload resolution failed so we deleted it.
1104 void setImplicitMoveAssignmentIsDeleted() {
1105 assert((data().DefaultedMoveAssignmentIsDeleted ||
1106 needsOverloadResolutionForMoveAssignment()) &&
1107 "move assignment should not be deleted");
1108 data().DefaultedMoveAssignmentIsDeleted = true;
1111 /// \brief Determine whether this class should get an implicit move
1112 /// assignment operator or if any existing special member function inhibits
1114 bool needsImplicitMoveAssignment() const {
1115 return !(data().DeclaredSpecialMembers & SMF_MoveAssignment) &&
1116 !hasUserDeclaredCopyConstructor() &&
1117 !hasUserDeclaredCopyAssignment() &&
1118 !hasUserDeclaredMoveConstructor() &&
1119 !hasUserDeclaredDestructor() &&
1120 // C++1z [expr.prim.lambda]p21: "the closure type has a deleted copy
1121 // assignment operator". The intent is that this counts as a user
1122 // declared copy assignment, but we do not model it that way.
1126 /// \brief Determine whether we need to eagerly declare a move assignment
1127 /// operator for this class.
1128 bool needsOverloadResolutionForMoveAssignment() const {
1129 return data().NeedOverloadResolutionForMoveAssignment;
1132 /// \brief Determine whether this class has a user-declared destructor.
1134 /// When false, a destructor will be implicitly declared.
1135 bool hasUserDeclaredDestructor() const {
1136 return data().UserDeclaredSpecialMembers & SMF_Destructor;
1139 /// \brief Determine whether this class needs an implicit destructor to
1140 /// be lazily declared.
1141 bool needsImplicitDestructor() const {
1142 return !(data().DeclaredSpecialMembers & SMF_Destructor);
1145 /// \brief Determine whether we need to eagerly declare a destructor for this
1147 bool needsOverloadResolutionForDestructor() const {
1148 return data().NeedOverloadResolutionForDestructor;
1151 /// \brief Determine whether this class describes a lambda function object.
1152 bool isLambda() const {
1153 // An update record can't turn a non-lambda into a lambda.
1154 auto *DD = DefinitionData;
1155 return DD && DD->IsLambda;
1158 /// \brief Determine whether this class describes a generic
1159 /// lambda function object (i.e. function call operator is
1161 bool isGenericLambda() const;
1163 /// \brief Retrieve the lambda call operator of the closure type
1164 /// if this is a closure type.
1165 CXXMethodDecl *getLambdaCallOperator() const;
1167 /// \brief Retrieve the lambda static invoker, the address of which
1168 /// is returned by the conversion operator, and the body of which
1169 /// is forwarded to the lambda call operator.
1170 CXXMethodDecl *getLambdaStaticInvoker() const;
1172 /// \brief Retrieve the generic lambda's template parameter list.
1173 /// Returns null if the class does not represent a lambda or a generic
1175 TemplateParameterList *getGenericLambdaTemplateParameterList() const;
1177 LambdaCaptureDefault getLambdaCaptureDefault() const {
1179 return static_cast<LambdaCaptureDefault>(getLambdaData().CaptureDefault);
1182 /// \brief For a closure type, retrieve the mapping from captured
1183 /// variables and \c this to the non-static data members that store the
1184 /// values or references of the captures.
1186 /// \param Captures Will be populated with the mapping from captured
1187 /// variables to the corresponding fields.
1189 /// \param ThisCapture Will be set to the field declaration for the
1190 /// \c this capture.
1192 /// \note No entries will be added for init-captures, as they do not capture
1194 void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
1195 FieldDecl *&ThisCapture) const;
1197 using capture_const_iterator = const LambdaCapture *;
1198 using capture_const_range = llvm::iterator_range<capture_const_iterator>;
1200 capture_const_range captures() const {
1201 return capture_const_range(captures_begin(), captures_end());
1204 capture_const_iterator captures_begin() const {
1205 return isLambda() ? getLambdaData().Captures : nullptr;
1208 capture_const_iterator captures_end() const {
1209 return isLambda() ? captures_begin() + getLambdaData().NumCaptures
1213 using conversion_iterator = UnresolvedSetIterator;
1215 conversion_iterator conversion_begin() const {
1216 return data().Conversions.get(getASTContext()).begin();
1219 conversion_iterator conversion_end() const {
1220 return data().Conversions.get(getASTContext()).end();
1223 /// Removes a conversion function from this class. The conversion
1224 /// function must currently be a member of this class. Furthermore,
1225 /// this class must currently be in the process of being defined.
1226 void removeConversion(const NamedDecl *Old);
1228 /// \brief Get all conversion functions visible in current class,
1229 /// including conversion function templates.
1230 llvm::iterator_range<conversion_iterator> getVisibleConversionFunctions();
1232 /// Determine whether this class is an aggregate (C++ [dcl.init.aggr]),
1233 /// which is a class with no user-declared constructors, no private
1234 /// or protected non-static data members, no base classes, and no virtual
1235 /// functions (C++ [dcl.init.aggr]p1).
1236 bool isAggregate() const { return data().Aggregate; }
1238 /// \brief Whether this class has any in-class initializers
1239 /// for non-static data members (including those in anonymous unions or
1241 bool hasInClassInitializer() const { return data().HasInClassInitializer; }
1243 /// \brief Whether this class or any of its subobjects has any members of
1244 /// reference type which would make value-initialization ill-formed.
1246 /// Per C++03 [dcl.init]p5:
1247 /// - if T is a non-union class type without a user-declared constructor,
1248 /// then every non-static data member and base-class component of T is
1249 /// value-initialized [...] A program that calls for [...]
1250 /// value-initialization of an entity of reference type is ill-formed.
1251 bool hasUninitializedReferenceMember() const {
1252 return !isUnion() && !hasUserDeclaredConstructor() &&
1253 data().HasUninitializedReferenceMember;
1256 /// \brief Whether this class is a POD-type (C++ [class]p4)
1258 /// For purposes of this function a class is POD if it is an aggregate
1259 /// that has no non-static non-POD data members, no reference data
1260 /// members, no user-defined copy assignment operator and no
1261 /// user-defined destructor.
1263 /// Note that this is the C++ TR1 definition of POD.
1264 bool isPOD() const { return data().PlainOldData; }
1266 /// \brief True if this class is C-like, without C++-specific features, e.g.
1267 /// it contains only public fields, no bases, tag kind is not 'class', etc.
1268 bool isCLike() const;
1270 /// \brief Determine whether this is an empty class in the sense of
1271 /// (C++11 [meta.unary.prop]).
1273 /// The CXXRecordDecl is a class type, but not a union type,
1274 /// with no non-static data members other than bit-fields of length 0,
1275 /// no virtual member functions, no virtual base classes,
1276 /// and no base class B for which is_empty<B>::value is false.
1278 /// \note This does NOT include a check for union-ness.
1279 bool isEmpty() const { return data().Empty; }
1281 /// \brief Determine whether this class has direct non-static data members.
1282 bool hasDirectFields() const {
1284 return D.HasPublicFields || D.HasProtectedFields || D.HasPrivateFields;
1287 /// Whether this class is polymorphic (C++ [class.virtual]),
1288 /// which means that the class contains or inherits a virtual function.
1289 bool isPolymorphic() const { return data().Polymorphic; }
1291 /// \brief Determine whether this class has a pure virtual function.
1293 /// The class is is abstract per (C++ [class.abstract]p2) if it declares
1294 /// a pure virtual function or inherits a pure virtual function that is
1296 bool isAbstract() const { return data().Abstract; }
1298 /// \brief Determine whether this class has standard layout per
1300 bool isStandardLayout() const { return data().IsStandardLayout; }
1302 /// \brief Determine whether this class, or any of its class subobjects,
1303 /// contains a mutable field.
1304 bool hasMutableFields() const { return data().HasMutableFields; }
1306 /// \brief Determine whether this class has any variant members.
1307 bool hasVariantMembers() const { return data().HasVariantMembers; }
1309 /// \brief Determine whether this class has a trivial default constructor
1310 /// (C++11 [class.ctor]p5).
1311 bool hasTrivialDefaultConstructor() const {
1312 return hasDefaultConstructor() &&
1313 (data().HasTrivialSpecialMembers & SMF_DefaultConstructor);
1316 /// \brief Determine whether this class has a non-trivial default constructor
1317 /// (C++11 [class.ctor]p5).
1318 bool hasNonTrivialDefaultConstructor() const {
1319 return (data().DeclaredNonTrivialSpecialMembers & SMF_DefaultConstructor) ||
1320 (needsImplicitDefaultConstructor() &&
1321 !(data().HasTrivialSpecialMembers & SMF_DefaultConstructor));
1324 /// \brief Determine whether this class has at least one constexpr constructor
1325 /// other than the copy or move constructors.
1326 bool hasConstexprNonCopyMoveConstructor() const {
1327 return data().HasConstexprNonCopyMoveConstructor ||
1328 (needsImplicitDefaultConstructor() &&
1329 defaultedDefaultConstructorIsConstexpr());
1332 /// \brief Determine whether a defaulted default constructor for this class
1333 /// would be constexpr.
1334 bool defaultedDefaultConstructorIsConstexpr() const {
1335 return data().DefaultedDefaultConstructorIsConstexpr &&
1336 (!isUnion() || hasInClassInitializer() || !hasVariantMembers());
1339 /// \brief Determine whether this class has a constexpr default constructor.
1340 bool hasConstexprDefaultConstructor() const {
1341 return data().HasConstexprDefaultConstructor ||
1342 (needsImplicitDefaultConstructor() &&
1343 defaultedDefaultConstructorIsConstexpr());
1346 /// \brief Determine whether this class has a trivial copy constructor
1347 /// (C++ [class.copy]p6, C++11 [class.copy]p12)
1348 bool hasTrivialCopyConstructor() const {
1349 return data().HasTrivialSpecialMembers & SMF_CopyConstructor;
1352 /// \brief Determine whether this class has a non-trivial copy constructor
1353 /// (C++ [class.copy]p6, C++11 [class.copy]p12)
1354 bool hasNonTrivialCopyConstructor() const {
1355 return data().DeclaredNonTrivialSpecialMembers & SMF_CopyConstructor ||
1356 !hasTrivialCopyConstructor();
1359 /// \brief Determine whether this class has a trivial move constructor
1360 /// (C++11 [class.copy]p12)
1361 bool hasTrivialMoveConstructor() const {
1362 return hasMoveConstructor() &&
1363 (data().HasTrivialSpecialMembers & SMF_MoveConstructor);
1366 /// \brief Determine whether this class has a non-trivial move constructor
1367 /// (C++11 [class.copy]p12)
1368 bool hasNonTrivialMoveConstructor() const {
1369 return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveConstructor) ||
1370 (needsImplicitMoveConstructor() &&
1371 !(data().HasTrivialSpecialMembers & SMF_MoveConstructor));
1374 /// \brief Determine whether this class has a trivial copy assignment operator
1375 /// (C++ [class.copy]p11, C++11 [class.copy]p25)
1376 bool hasTrivialCopyAssignment() const {
1377 return data().HasTrivialSpecialMembers & SMF_CopyAssignment;
1380 /// \brief Determine whether this class has a non-trivial copy assignment
1381 /// operator (C++ [class.copy]p11, C++11 [class.copy]p25)
1382 bool hasNonTrivialCopyAssignment() const {
1383 return data().DeclaredNonTrivialSpecialMembers & SMF_CopyAssignment ||
1384 !hasTrivialCopyAssignment();
1387 /// \brief Determine whether this class has a trivial move assignment operator
1388 /// (C++11 [class.copy]p25)
1389 bool hasTrivialMoveAssignment() const {
1390 return hasMoveAssignment() &&
1391 (data().HasTrivialSpecialMembers & SMF_MoveAssignment);
1394 /// \brief Determine whether this class has a non-trivial move assignment
1395 /// operator (C++11 [class.copy]p25)
1396 bool hasNonTrivialMoveAssignment() const {
1397 return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveAssignment) ||
1398 (needsImplicitMoveAssignment() &&
1399 !(data().HasTrivialSpecialMembers & SMF_MoveAssignment));
1402 /// \brief Determine whether this class has a trivial destructor
1403 /// (C++ [class.dtor]p3)
1404 bool hasTrivialDestructor() const {
1405 return data().HasTrivialSpecialMembers & SMF_Destructor;
1408 /// \brief Determine whether this class has a non-trivial destructor
1409 /// (C++ [class.dtor]p3)
1410 bool hasNonTrivialDestructor() const {
1411 return !(data().HasTrivialSpecialMembers & SMF_Destructor);
1414 /// \brief Determine whether declaring a const variable with this type is ok
1415 /// per core issue 253.
1416 bool allowConstDefaultInit() const {
1417 return !data().HasUninitializedFields ||
1418 !(data().HasDefaultedDefaultConstructor ||
1419 needsImplicitDefaultConstructor());
1422 /// \brief Determine whether this class has a destructor which has no
1423 /// semantic effect.
1425 /// Any such destructor will be trivial, public, defaulted and not deleted,
1426 /// and will call only irrelevant destructors.
1427 bool hasIrrelevantDestructor() const {
1428 return data().HasIrrelevantDestructor;
1431 /// \brief Determine whether this class has at least one trivial, non-deleted
1432 /// copy or move constructor.
1433 bool canPassInRegisters() const {
1434 return data().CanPassInRegisters;
1437 /// \brief Set that we can pass this RecordDecl in registers.
1438 // FIXME: This should be set as part of completeDefinition.
1439 void setCanPassInRegisters(bool CanPass) {
1440 data().CanPassInRegisters = CanPass;
1443 /// \brief Determine whether this class has a non-literal or/ volatile type
1444 /// non-static data member or base class.
1445 bool hasNonLiteralTypeFieldsOrBases() const {
1446 return data().HasNonLiteralTypeFieldsOrBases;
1449 /// \brief Determine whether this class has a using-declaration that names
1450 /// a user-declared base class constructor.
1451 bool hasInheritedConstructor() const {
1452 return data().HasInheritedConstructor;
1455 /// \brief Determine whether this class has a using-declaration that names
1456 /// a base class assignment operator.
1457 bool hasInheritedAssignment() const {
1458 return data().HasInheritedAssignment;
1461 /// \brief Determine whether this class is considered trivially copyable per
1462 /// (C++11 [class]p6).
1463 bool isTriviallyCopyable() const;
1465 /// \brief Determine whether this class is considered trivial.
1467 /// C++11 [class]p6:
1468 /// "A trivial class is a class that has a trivial default constructor and
1469 /// is trivially copiable."
1470 bool isTrivial() const {
1471 return isTriviallyCopyable() && hasTrivialDefaultConstructor();
1474 /// \brief Determine whether this class is a literal type.
1476 /// C++11 [basic.types]p10:
1477 /// A class type that has all the following properties:
1478 /// - it has a trivial destructor
1479 /// - every constructor call and full-expression in the
1480 /// brace-or-equal-intializers for non-static data members (if any) is
1481 /// a constant expression.
1482 /// - it is an aggregate type or has at least one constexpr constructor
1483 /// or constructor template that is not a copy or move constructor, and
1484 /// - all of its non-static data members and base classes are of literal
1487 /// We resolve DR1361 by ignoring the second bullet. We resolve DR1452 by
1488 /// treating types with trivial default constructors as literal types.
1490 /// Only in C++17 and beyond, are lambdas literal types.
1491 bool isLiteral() const {
1492 return hasTrivialDestructor() &&
1493 (!isLambda() || getASTContext().getLangOpts().CPlusPlus17) &&
1494 !hasNonLiteralTypeFieldsOrBases() &&
1495 (isAggregate() || isLambda() ||
1496 hasConstexprNonCopyMoveConstructor() ||
1497 hasTrivialDefaultConstructor());
1500 /// \brief If this record is an instantiation of a member class,
1501 /// retrieves the member class from which it was instantiated.
1503 /// This routine will return non-null for (non-templated) member
1504 /// classes of class templates. For example, given:
1507 /// template<typename T>
1513 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
1514 /// whose parent is the class template specialization X<int>. For
1515 /// this declaration, getInstantiatedFromMemberClass() will return
1516 /// the CXXRecordDecl X<T>::A. When a complete definition of
1517 /// X<int>::A is required, it will be instantiated from the
1518 /// declaration returned by getInstantiatedFromMemberClass().
1519 CXXRecordDecl *getInstantiatedFromMemberClass() const;
1521 /// \brief If this class is an instantiation of a member class of a
1522 /// class template specialization, retrieves the member specialization
1524 MemberSpecializationInfo *getMemberSpecializationInfo() const;
1526 /// \brief Specify that this record is an instantiation of the
1527 /// member class \p RD.
1528 void setInstantiationOfMemberClass(CXXRecordDecl *RD,
1529 TemplateSpecializationKind TSK);
1531 /// \brief Retrieves the class template that is described by this
1532 /// class declaration.
1534 /// Every class template is represented as a ClassTemplateDecl and a
1535 /// CXXRecordDecl. The former contains template properties (such as
1536 /// the template parameter lists) while the latter contains the
1537 /// actual description of the template's
1538 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
1539 /// CXXRecordDecl that from a ClassTemplateDecl, while
1540 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
1541 /// a CXXRecordDecl.
1542 ClassTemplateDecl *getDescribedClassTemplate() const;
1544 void setDescribedClassTemplate(ClassTemplateDecl *Template);
1546 /// \brief Determine whether this particular class is a specialization or
1547 /// instantiation of a class template or member class of a class template,
1548 /// and how it was instantiated or specialized.
1549 TemplateSpecializationKind getTemplateSpecializationKind() const;
1551 /// \brief Set the kind of specialization or template instantiation this is.
1552 void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
1554 /// \brief Retrieve the record declaration from which this record could be
1555 /// instantiated. Returns null if this class is not a template instantiation.
1556 const CXXRecordDecl *getTemplateInstantiationPattern() const;
1558 CXXRecordDecl *getTemplateInstantiationPattern() {
1559 return const_cast<CXXRecordDecl *>(const_cast<const CXXRecordDecl *>(this)
1560 ->getTemplateInstantiationPattern());
1563 /// \brief Returns the destructor decl for this class.
1564 CXXDestructorDecl *getDestructor() const;
1566 /// \brief Returns true if the class destructor, or any implicitly invoked
1567 /// destructors are marked noreturn.
1568 bool isAnyDestructorNoReturn() const;
1570 /// \brief If the class is a local class [class.local], returns
1571 /// the enclosing function declaration.
1572 const FunctionDecl *isLocalClass() const {
1573 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
1574 return RD->isLocalClass();
1576 return dyn_cast<FunctionDecl>(getDeclContext());
1579 FunctionDecl *isLocalClass() {
1580 return const_cast<FunctionDecl*>(
1581 const_cast<const CXXRecordDecl*>(this)->isLocalClass());
1584 /// \brief Determine whether this dependent class is a current instantiation,
1585 /// when viewed from within the given context.
1586 bool isCurrentInstantiation(const DeclContext *CurContext) const;
1588 /// \brief Determine whether this class is derived from the class \p Base.
1590 /// This routine only determines whether this class is derived from \p Base,
1591 /// but does not account for factors that may make a Derived -> Base class
1592 /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1593 /// base class subobjects.
1595 /// \param Base the base class we are searching for.
1597 /// \returns true if this class is derived from Base, false otherwise.
1598 bool isDerivedFrom(const CXXRecordDecl *Base) const;
1600 /// \brief Determine whether this class is derived from the type \p Base.
1602 /// This routine only determines whether this class is derived from \p Base,
1603 /// but does not account for factors that may make a Derived -> Base class
1604 /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1605 /// base class subobjects.
1607 /// \param Base the base class we are searching for.
1609 /// \param Paths will contain the paths taken from the current class to the
1610 /// given \p Base class.
1612 /// \returns true if this class is derived from \p Base, false otherwise.
1614 /// \todo add a separate parameter to configure IsDerivedFrom, rather than
1615 /// tangling input and output in \p Paths
1616 bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
1618 /// \brief Determine whether this class is virtually derived from
1619 /// the class \p Base.
1621 /// This routine only determines whether this class is virtually
1622 /// derived from \p Base, but does not account for factors that may
1623 /// make a Derived -> Base class ill-formed, such as
1624 /// private/protected inheritance or multiple, ambiguous base class
1627 /// \param Base the base class we are searching for.
1629 /// \returns true if this class is virtually derived from Base,
1630 /// false otherwise.
1631 bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const;
1633 /// \brief Determine whether this class is provably not derived from
1634 /// the type \p Base.
1635 bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
1637 /// \brief Function type used by forallBases() as a callback.
1639 /// \param BaseDefinition the definition of the base class
1641 /// \returns true if this base matched the search criteria
1642 using ForallBasesCallback =
1643 llvm::function_ref<bool(const CXXRecordDecl *BaseDefinition)>;
1645 /// \brief Determines if the given callback holds for all the direct
1646 /// or indirect base classes of this type.
1648 /// The class itself does not count as a base class. This routine
1649 /// returns false if the class has non-computable base classes.
1651 /// \param BaseMatches Callback invoked for each (direct or indirect) base
1652 /// class of this type, or if \p AllowShortCircuit is true then until a call
1655 /// \param AllowShortCircuit if false, forces the callback to be called
1656 /// for every base class, even if a dependent or non-matching base was
1658 bool forallBases(ForallBasesCallback BaseMatches,
1659 bool AllowShortCircuit = true) const;
1661 /// \brief Function type used by lookupInBases() to determine whether a
1662 /// specific base class subobject matches the lookup criteria.
1664 /// \param Specifier the base-class specifier that describes the inheritance
1665 /// from the base class we are trying to match.
1667 /// \param Path the current path, from the most-derived class down to the
1668 /// base named by the \p Specifier.
1670 /// \returns true if this base matched the search criteria, false otherwise.
1671 using BaseMatchesCallback =
1672 llvm::function_ref<bool(const CXXBaseSpecifier *Specifier,
1673 CXXBasePath &Path)>;
1675 /// \brief Look for entities within the base classes of this C++ class,
1676 /// transitively searching all base class subobjects.
1678 /// This routine uses the callback function \p BaseMatches to find base
1679 /// classes meeting some search criteria, walking all base class subobjects
1680 /// and populating the given \p Paths structure with the paths through the
1681 /// inheritance hierarchy that resulted in a match. On a successful search,
1682 /// the \p Paths structure can be queried to retrieve the matching paths and
1683 /// to determine if there were any ambiguities.
1685 /// \param BaseMatches callback function used to determine whether a given
1686 /// base matches the user-defined search criteria.
1688 /// \param Paths used to record the paths from this class to its base class
1689 /// subobjects that match the search criteria.
1691 /// \param LookupInDependent can be set to true to extend the search to
1692 /// dependent base classes.
1694 /// \returns true if there exists any path from this class to a base class
1695 /// subobject that matches the search criteria.
1696 bool lookupInBases(BaseMatchesCallback BaseMatches, CXXBasePaths &Paths,
1697 bool LookupInDependent = false) const;
1699 /// \brief Base-class lookup callback that determines whether the given
1700 /// base class specifier refers to a specific class declaration.
1702 /// This callback can be used with \c lookupInBases() to determine whether
1703 /// a given derived class has is a base class subobject of a particular type.
1704 /// The base record pointer should refer to the canonical CXXRecordDecl of the
1705 /// base class that we are searching for.
1706 static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
1707 CXXBasePath &Path, const CXXRecordDecl *BaseRecord);
1709 /// \brief Base-class lookup callback that determines whether the
1710 /// given base class specifier refers to a specific class
1711 /// declaration and describes virtual derivation.
1713 /// This callback can be used with \c lookupInBases() to determine
1714 /// whether a given derived class has is a virtual base class
1715 /// subobject of a particular type. The base record pointer should
1716 /// refer to the canonical CXXRecordDecl of the base class that we
1717 /// are searching for.
1718 static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
1720 const CXXRecordDecl *BaseRecord);
1722 /// \brief Base-class lookup callback that determines whether there exists
1723 /// a tag with the given name.
1725 /// This callback can be used with \c lookupInBases() to find tag members
1726 /// of the given name within a C++ class hierarchy.
1727 static bool FindTagMember(const CXXBaseSpecifier *Specifier,
1728 CXXBasePath &Path, DeclarationName Name);
1730 /// \brief Base-class lookup callback that determines whether there exists
1731 /// a member with the given name.
1733 /// This callback can be used with \c lookupInBases() to find members
1734 /// of the given name within a C++ class hierarchy.
1735 static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
1736 CXXBasePath &Path, DeclarationName Name);
1738 /// \brief Base-class lookup callback that determines whether there exists
1739 /// a member with the given name.
1741 /// This callback can be used with \c lookupInBases() to find members
1742 /// of the given name within a C++ class hierarchy, including dependent
1745 FindOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier,
1746 CXXBasePath &Path, DeclarationName Name);
1748 /// \brief Base-class lookup callback that determines whether there exists
1749 /// an OpenMP declare reduction member with the given name.
1751 /// This callback can be used with \c lookupInBases() to find members
1752 /// of the given name within a C++ class hierarchy.
1753 static bool FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
1754 CXXBasePath &Path, DeclarationName Name);
1756 /// \brief Base-class lookup callback that determines whether there exists
1757 /// a member with the given name that can be used in a nested-name-specifier.
1759 /// This callback can be used with \c lookupInBases() to find members of
1760 /// the given name within a C++ class hierarchy that can occur within
1761 /// nested-name-specifiers.
1762 static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
1764 DeclarationName Name);
1766 /// \brief Retrieve the final overriders for each virtual member
1767 /// function in the class hierarchy where this class is the
1768 /// most-derived class in the class hierarchy.
1769 void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
1771 /// \brief Get the indirect primary bases for this class.
1772 void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const;
1774 /// Performs an imprecise lookup of a dependent name in this class.
1776 /// This function does not follow strict semantic rules and should be used
1777 /// only when lookup rules can be relaxed, e.g. indexing.
1778 std::vector<const NamedDecl *>
1779 lookupDependentName(const DeclarationName &Name,
1780 llvm::function_ref<bool(const NamedDecl *ND)> Filter);
1782 /// Renders and displays an inheritance diagram
1783 /// for this C++ class and all of its base classes (transitively) using
1785 void viewInheritance(ASTContext& Context) const;
1787 /// \brief Calculates the access of a decl that is reached
1789 static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
1790 AccessSpecifier DeclAccess) {
1791 assert(DeclAccess != AS_none);
1792 if (DeclAccess == AS_private) return AS_none;
1793 return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
1796 /// \brief Indicates that the declaration of a defaulted or deleted special
1797 /// member function is now complete.
1798 void finishedDefaultedOrDeletedMember(CXXMethodDecl *MD);
1800 /// \brief Indicates that the definition of this class is now complete.
1801 void completeDefinition() override;
1803 /// \brief Indicates that the definition of this class is now complete,
1804 /// and provides a final overrider map to help determine
1806 /// \param FinalOverriders The final overrider map for this class, which can
1807 /// be provided as an optimization for abstract-class checking. If NULL,
1808 /// final overriders will be computed if they are needed to complete the
1810 void completeDefinition(CXXFinalOverriderMap *FinalOverriders);
1812 /// \brief Determine whether this class may end up being abstract, even though
1813 /// it is not yet known to be abstract.
1815 /// \returns true if this class is not known to be abstract but has any
1816 /// base classes that are abstract. In this case, \c completeDefinition()
1817 /// will need to compute final overriders to determine whether the class is
1818 /// actually abstract.
1819 bool mayBeAbstract() const;
1821 /// \brief If this is the closure type of a lambda expression, retrieve the
1822 /// number to be used for name mangling in the Itanium C++ ABI.
1824 /// Zero indicates that this closure type has internal linkage, so the
1825 /// mangling number does not matter, while a non-zero value indicates which
1826 /// lambda expression this is in this particular context.
1827 unsigned getLambdaManglingNumber() const {
1828 assert(isLambda() && "Not a lambda closure type!");
1829 return getLambdaData().ManglingNumber;
1832 /// \brief Retrieve the declaration that provides additional context for a
1833 /// lambda, when the normal declaration context is not specific enough.
1835 /// Certain contexts (default arguments of in-class function parameters and
1836 /// the initializers of data members) have separate name mangling rules for
1837 /// lambdas within the Itanium C++ ABI. For these cases, this routine provides
1838 /// the declaration in which the lambda occurs, e.g., the function parameter
1839 /// or the non-static data member. Otherwise, it returns NULL to imply that
1840 /// the declaration context suffices.
1841 Decl *getLambdaContextDecl() const;
1843 /// \brief Set the mangling number and context declaration for a lambda
1845 void setLambdaMangling(unsigned ManglingNumber, Decl *ContextDecl) {
1846 getLambdaData().ManglingNumber = ManglingNumber;
1847 getLambdaData().ContextDecl = ContextDecl;
1850 /// \brief Returns the inheritance model used for this record.
1851 MSInheritanceAttr::Spelling getMSInheritanceModel() const;
1853 /// \brief Calculate what the inheritance model would be for this class.
1854 MSInheritanceAttr::Spelling calculateInheritanceModel() const;
1856 /// In the Microsoft C++ ABI, use zero for the field offset of a null data
1857 /// member pointer if we can guarantee that zero is not a valid field offset,
1858 /// or if the member pointer has multiple fields. Polymorphic classes have a
1859 /// vfptr at offset zero, so we can use zero for null. If there are multiple
1860 /// fields, we can use zero even if it is a valid field offset because
1861 /// null-ness testing will check the other fields.
1862 bool nullFieldOffsetIsZero() const {
1863 return !MSInheritanceAttr::hasOnlyOneField(/*IsMemberFunction=*/false,
1864 getMSInheritanceModel()) ||
1865 (hasDefinition() && isPolymorphic());
1868 /// \brief Controls when vtordisps will be emitted if this record is used as a
1870 MSVtorDispAttr::Mode getMSVtorDispMode() const;
1872 /// \brief Determine whether this lambda expression was known to be dependent
1873 /// at the time it was created, even if its context does not appear to be
1876 /// This flag is a workaround for an issue with parsing, where default
1877 /// arguments are parsed before their enclosing function declarations have
1878 /// been created. This means that any lambda expressions within those
1879 /// default arguments will have as their DeclContext the context enclosing
1880 /// the function declaration, which may be non-dependent even when the
1881 /// function declaration itself is dependent. This flag indicates when we
1882 /// know that the lambda is dependent despite that.
1883 bool isDependentLambda() const {
1884 return isLambda() && getLambdaData().Dependent;
1887 TypeSourceInfo *getLambdaTypeInfo() const {
1888 return getLambdaData().MethodTyInfo;
1891 // \brief Determine whether this type is an Interface Like type for
1892 // __interface inheritence purposes.
1893 bool isInterfaceLike() const;
1895 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1896 static bool classofKind(Kind K) {
1897 return K >= firstCXXRecord && K <= lastCXXRecord;
1901 /// \brief Represents a C++ deduction guide declaration.
1904 /// template<typename T> struct A { A(); A(T); };
1908 /// In this example, there will be an explicit deduction guide from the
1909 /// second line, and implicit deduction guide templates synthesized from
1910 /// the constructors of \c A.
1911 class CXXDeductionGuideDecl : public FunctionDecl {
1912 void anchor() override;
1915 CXXDeductionGuideDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1916 bool IsExplicit, const DeclarationNameInfo &NameInfo,
1917 QualType T, TypeSourceInfo *TInfo,
1918 SourceLocation EndLocation)
1919 : FunctionDecl(CXXDeductionGuide, C, DC, StartLoc, NameInfo, T, TInfo,
1920 SC_None, false, false) {
1921 if (EndLocation.isValid())
1922 setRangeEnd(EndLocation);
1923 IsExplicitSpecified = IsExplicit;
1927 friend class ASTDeclReader;
1928 friend class ASTDeclWriter;
1930 static CXXDeductionGuideDecl *Create(ASTContext &C, DeclContext *DC,
1931 SourceLocation StartLoc, bool IsExplicit,
1932 const DeclarationNameInfo &NameInfo,
1933 QualType T, TypeSourceInfo *TInfo,
1934 SourceLocation EndLocation);
1936 static CXXDeductionGuideDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1938 /// Whether this deduction guide is explicit.
1939 bool isExplicit() const { return IsExplicitSpecified; }
1941 /// Whether this deduction guide was declared with the 'explicit' specifier.
1942 bool isExplicitSpecified() const { return IsExplicitSpecified; }
1944 /// Get the template for which this guide performs deduction.
1945 TemplateDecl *getDeducedTemplate() const {
1946 return getDeclName().getCXXDeductionGuideTemplate();
1949 void setIsCopyDeductionCandidate() {
1950 IsCopyDeductionCandidate = true;
1953 bool isCopyDeductionCandidate() const { return IsCopyDeductionCandidate; }
1955 // Implement isa/cast/dyncast/etc.
1956 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1957 static bool classofKind(Kind K) { return K == CXXDeductionGuide; }
1960 /// \brief Represents a static or instance method of a struct/union/class.
1962 /// In the terminology of the C++ Standard, these are the (static and
1963 /// non-static) member functions, whether virtual or not.
1964 class CXXMethodDecl : public FunctionDecl {
1965 void anchor() override;
1968 CXXMethodDecl(Kind DK, ASTContext &C, CXXRecordDecl *RD,
1969 SourceLocation StartLoc, const DeclarationNameInfo &NameInfo,
1970 QualType T, TypeSourceInfo *TInfo,
1971 StorageClass SC, bool isInline,
1972 bool isConstexpr, SourceLocation EndLocation)
1973 : FunctionDecl(DK, C, RD, StartLoc, NameInfo, T, TInfo,
1974 SC, isInline, isConstexpr) {
1975 if (EndLocation.isValid())
1976 setRangeEnd(EndLocation);
1980 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
1981 SourceLocation StartLoc,
1982 const DeclarationNameInfo &NameInfo,
1983 QualType T, TypeSourceInfo *TInfo,
1987 SourceLocation EndLocation);
1989 static CXXMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1991 bool isStatic() const;
1992 bool isInstance() const { return !isStatic(); }
1994 /// Returns true if the given operator is implicitly static in a record
1996 static bool isStaticOverloadedOperator(OverloadedOperatorKind OOK) {
1998 // Any allocation function for a class T is a static member
1999 // (even if not explicitly declared static).
2000 // [class.free]p6 Any deallocation function for a class X is a static member
2001 // (even if not explicitly declared static).
2002 return OOK == OO_New || OOK == OO_Array_New || OOK == OO_Delete ||
2003 OOK == OO_Array_Delete;
2006 bool isConst() const { return getType()->castAs<FunctionType>()->isConst(); }
2007 bool isVolatile() const { return getType()->castAs<FunctionType>()->isVolatile(); }
2009 bool isVirtual() const {
2011 cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl());
2013 // Member function is virtual if it is marked explicitly so, or if it is
2014 // declared in __interface -- then it is automatically pure virtual.
2015 if (CD->isVirtualAsWritten() || CD->isPure())
2018 return CD->size_overridden_methods() != 0;
2021 /// If it's possible to devirtualize a call to this method, return the called
2022 /// function. Otherwise, return null.
2024 /// \param Base The object on which this virtual function is called.
2025 /// \param IsAppleKext True if we are compiling for Apple kext.
2026 CXXMethodDecl *getDevirtualizedMethod(const Expr *Base, bool IsAppleKext);
2028 const CXXMethodDecl *getDevirtualizedMethod(const Expr *Base,
2029 bool IsAppleKext) const {
2030 return const_cast<CXXMethodDecl *>(this)->getDevirtualizedMethod(
2034 /// \brief Determine whether this is a usual deallocation function
2035 /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded
2036 /// delete or delete[] operator with a particular signature.
2037 bool isUsualDeallocationFunction() const;
2039 /// \brief Determine whether this is a copy-assignment operator, regardless
2040 /// of whether it was declared implicitly or explicitly.
2041 bool isCopyAssignmentOperator() const;
2043 /// \brief Determine whether this is a move assignment operator.
2044 bool isMoveAssignmentOperator() const;
2046 CXXMethodDecl *getCanonicalDecl() override {
2047 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
2049 const CXXMethodDecl *getCanonicalDecl() const {
2050 return const_cast<CXXMethodDecl*>(this)->getCanonicalDecl();
2053 CXXMethodDecl *getMostRecentDecl() {
2054 return cast<CXXMethodDecl>(
2055 static_cast<FunctionDecl *>(this)->getMostRecentDecl());
2057 const CXXMethodDecl *getMostRecentDecl() const {
2058 return const_cast<CXXMethodDecl*>(this)->getMostRecentDecl();
2061 /// True if this method is user-declared and was not
2062 /// deleted or defaulted on its first declaration.
2063 bool isUserProvided() const {
2064 auto *DeclAsWritten = this;
2065 if (auto *Pattern = getTemplateInstantiationPattern())
2066 DeclAsWritten = cast<CXXMethodDecl>(Pattern);
2067 return !(DeclAsWritten->isDeleted() ||
2068 DeclAsWritten->getCanonicalDecl()->isDefaulted());
2071 void addOverriddenMethod(const CXXMethodDecl *MD);
2073 using method_iterator = const CXXMethodDecl *const *;
2075 method_iterator begin_overridden_methods() const;
2076 method_iterator end_overridden_methods() const;
2077 unsigned size_overridden_methods() const;
2079 using overridden_method_range= ASTContext::overridden_method_range;
2081 overridden_method_range overridden_methods() const;
2083 /// Returns the parent of this method declaration, which
2084 /// is the class in which this method is defined.
2085 const CXXRecordDecl *getParent() const {
2086 return cast<CXXRecordDecl>(FunctionDecl::getParent());
2089 /// Returns the parent of this method declaration, which
2090 /// is the class in which this method is defined.
2091 CXXRecordDecl *getParent() {
2092 return const_cast<CXXRecordDecl *>(
2093 cast<CXXRecordDecl>(FunctionDecl::getParent()));
2096 /// \brief Returns the type of the \c this pointer.
2098 /// Should only be called for instance (i.e., non-static) methods. Note
2099 /// that for the call operator of a lambda closure type, this returns the
2100 /// desugared 'this' type (a pointer to the closure type), not the captured
2102 QualType getThisType(ASTContext &C) const;
2104 unsigned getTypeQualifiers() const {
2105 return getType()->getAs<FunctionProtoType>()->getTypeQuals();
2108 /// \brief Retrieve the ref-qualifier associated with this method.
2110 /// In the following example, \c f() has an lvalue ref-qualifier, \c g()
2111 /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier.
2119 RefQualifierKind getRefQualifier() const {
2120 return getType()->getAs<FunctionProtoType>()->getRefQualifier();
2123 bool hasInlineBody() const;
2125 /// \brief Determine whether this is a lambda closure type's static member
2126 /// function that is used for the result of the lambda's conversion to
2127 /// function pointer (for a lambda with no captures).
2129 /// The function itself, if used, will have a placeholder body that will be
2130 /// supplied by IR generation to either forward to the function call operator
2131 /// or clone the function call operator.
2132 bool isLambdaStaticInvoker() const;
2134 /// \brief Find the method in \p RD that corresponds to this one.
2136 /// Find if \p RD or one of the classes it inherits from override this method.
2137 /// If so, return it. \p RD is assumed to be a subclass of the class defining
2138 /// this method (or be the class itself), unless \p MayBeBase is set to true.
2140 getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2141 bool MayBeBase = false);
2143 const CXXMethodDecl *
2144 getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2145 bool MayBeBase = false) const {
2146 return const_cast<CXXMethodDecl *>(this)
2147 ->getCorrespondingMethodInClass(RD, MayBeBase);
2150 // Implement isa/cast/dyncast/etc.
2151 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2152 static bool classofKind(Kind K) {
2153 return K >= firstCXXMethod && K <= lastCXXMethod;
2157 /// \brief Represents a C++ base or member initializer.
2159 /// This is part of a constructor initializer that
2160 /// initializes one non-static member variable or one base class. For
2161 /// example, in the following, both 'A(a)' and 'f(3.14159)' are member
2166 /// class B : public A {
2169 /// B(A& a) : A(a), f(3.14159) { }
2172 class CXXCtorInitializer final {
2173 /// \brief Either the base class name/delegating constructor type (stored as
2174 /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
2175 /// (IndirectFieldDecl*) being initialized.
2176 llvm::PointerUnion3<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *>
2179 /// \brief The source location for the field name or, for a base initializer
2180 /// pack expansion, the location of the ellipsis.
2182 /// In the case of a delegating
2183 /// constructor, it will still include the type's source location as the
2184 /// Initializee points to the CXXConstructorDecl (to allow loop detection).
2185 SourceLocation MemberOrEllipsisLocation;
2187 /// \brief The argument used to initialize the base or member, which may
2188 /// end up constructing an object (when multiple arguments are involved).
2191 /// \brief Location of the left paren of the ctor-initializer.
2192 SourceLocation LParenLoc;
2194 /// \brief Location of the right paren of the ctor-initializer.
2195 SourceLocation RParenLoc;
2197 /// \brief If the initializee is a type, whether that type makes this
2198 /// a delegating initialization.
2199 unsigned IsDelegating : 1;
2201 /// \brief If the initializer is a base initializer, this keeps track
2202 /// of whether the base is virtual or not.
2203 unsigned IsVirtual : 1;
2205 /// \brief Whether or not the initializer is explicitly written
2207 unsigned IsWritten : 1;
2209 /// If IsWritten is true, then this number keeps track of the textual order
2210 /// of this initializer in the original sources, counting from 0.
2211 unsigned SourceOrder : 13;
2214 /// \brief Creates a new base-class initializer.
2216 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual,
2217 SourceLocation L, Expr *Init, SourceLocation R,
2218 SourceLocation EllipsisLoc);
2220 /// \brief Creates a new member initializer.
2222 CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
2223 SourceLocation MemberLoc, SourceLocation L, Expr *Init,
2226 /// \brief Creates a new anonymous field initializer.
2228 CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member,
2229 SourceLocation MemberLoc, SourceLocation L, Expr *Init,
2232 /// \brief Creates a new delegating initializer.
2234 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo,
2235 SourceLocation L, Expr *Init, SourceLocation R);
2237 /// \brief Determine whether this initializer is initializing a base class.
2238 bool isBaseInitializer() const {
2239 return Initializee.is<TypeSourceInfo*>() && !IsDelegating;
2242 /// \brief Determine whether this initializer is initializing a non-static
2244 bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); }
2246 bool isAnyMemberInitializer() const {
2247 return isMemberInitializer() || isIndirectMemberInitializer();
2250 bool isIndirectMemberInitializer() const {
2251 return Initializee.is<IndirectFieldDecl*>();
2254 /// \brief Determine whether this initializer is an implicit initializer
2255 /// generated for a field with an initializer defined on the member
2258 /// In-class member initializers (also known as "non-static data member
2259 /// initializations", NSDMIs) were introduced in C++11.
2260 bool isInClassMemberInitializer() const {
2261 return Init->getStmtClass() == Stmt::CXXDefaultInitExprClass;
2264 /// \brief Determine whether this initializer is creating a delegating
2266 bool isDelegatingInitializer() const {
2267 return Initializee.is<TypeSourceInfo*>() && IsDelegating;
2270 /// \brief Determine whether this initializer is a pack expansion.
2271 bool isPackExpansion() const {
2272 return isBaseInitializer() && MemberOrEllipsisLocation.isValid();
2275 // \brief For a pack expansion, returns the location of the ellipsis.
2276 SourceLocation getEllipsisLoc() const {
2277 assert(isPackExpansion() && "Initializer is not a pack expansion");
2278 return MemberOrEllipsisLocation;
2281 /// If this is a base class initializer, returns the type of the
2282 /// base class with location information. Otherwise, returns an NULL
2284 TypeLoc getBaseClassLoc() const;
2286 /// If this is a base class initializer, returns the type of the base class.
2287 /// Otherwise, returns null.
2288 const Type *getBaseClass() const;
2290 /// Returns whether the base is virtual or not.
2291 bool isBaseVirtual() const {
2292 assert(isBaseInitializer() && "Must call this on base initializer!");
2297 /// \brief Returns the declarator information for a base class or delegating
2299 TypeSourceInfo *getTypeSourceInfo() const {
2300 return Initializee.dyn_cast<TypeSourceInfo *>();
2303 /// \brief If this is a member initializer, returns the declaration of the
2304 /// non-static data member being initialized. Otherwise, returns null.
2305 FieldDecl *getMember() const {
2306 if (isMemberInitializer())
2307 return Initializee.get<FieldDecl*>();
2311 FieldDecl *getAnyMember() const {
2312 if (isMemberInitializer())
2313 return Initializee.get<FieldDecl*>();
2314 if (isIndirectMemberInitializer())
2315 return Initializee.get<IndirectFieldDecl*>()->getAnonField();
2319 IndirectFieldDecl *getIndirectMember() const {
2320 if (isIndirectMemberInitializer())
2321 return Initializee.get<IndirectFieldDecl*>();
2325 SourceLocation getMemberLocation() const {
2326 return MemberOrEllipsisLocation;
2329 /// \brief Determine the source location of the initializer.
2330 SourceLocation getSourceLocation() const;
2332 /// \brief Determine the source range covering the entire initializer.
2333 SourceRange getSourceRange() const LLVM_READONLY;
2335 /// \brief Determine whether this initializer is explicitly written
2336 /// in the source code.
2337 bool isWritten() const { return IsWritten; }
2339 /// \brief Return the source position of the initializer, counting from 0.
2340 /// If the initializer was implicit, -1 is returned.
2341 int getSourceOrder() const {
2342 return IsWritten ? static_cast<int>(SourceOrder) : -1;
2345 /// \brief Set the source order of this initializer.
2347 /// This can only be called once for each initializer; it cannot be called
2348 /// on an initializer having a positive number of (implicit) array indices.
2350 /// This assumes that the initializer was written in the source code, and
2351 /// ensures that isWritten() returns true.
2352 void setSourceOrder(int Pos) {
2353 assert(!IsWritten &&
2354 "setSourceOrder() used on implicit initializer");
2355 assert(SourceOrder == 0 &&
2356 "calling twice setSourceOrder() on the same initializer");
2358 "setSourceOrder() used to make an initializer implicit");
2360 SourceOrder = static_cast<unsigned>(Pos);
2363 SourceLocation getLParenLoc() const { return LParenLoc; }
2364 SourceLocation getRParenLoc() const { return RParenLoc; }
2366 /// \brief Get the initializer.
2367 Expr *getInit() const { return static_cast<Expr*>(Init); }
2370 /// Description of a constructor that was inherited from a base class.
2371 class InheritedConstructor {
2372 ConstructorUsingShadowDecl *Shadow = nullptr;
2373 CXXConstructorDecl *BaseCtor = nullptr;
2376 InheritedConstructor() = default;
2377 InheritedConstructor(ConstructorUsingShadowDecl *Shadow,
2378 CXXConstructorDecl *BaseCtor)
2379 : Shadow(Shadow), BaseCtor(BaseCtor) {}
2381 explicit operator bool() const { return Shadow; }
2383 ConstructorUsingShadowDecl *getShadowDecl() const { return Shadow; }
2384 CXXConstructorDecl *getConstructor() const { return BaseCtor; }
2387 /// \brief Represents a C++ constructor within a class.
2394 /// explicit X(int); // represented by a CXXConstructorDecl.
2397 class CXXConstructorDecl final
2398 : public CXXMethodDecl,
2399 private llvm::TrailingObjects<CXXConstructorDecl, InheritedConstructor> {
2400 /// \name Support for base and member initializers.
2402 /// \brief The arguments used to initialize the base or member.
2403 LazyCXXCtorInitializersPtr CtorInitializers;
2404 unsigned NumCtorInitializers : 31;
2407 /// \brief Whether this constructor declaration is an implicitly-declared
2408 /// inheriting constructor.
2409 unsigned IsInheritingConstructor : 1;
2411 CXXConstructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2412 const DeclarationNameInfo &NameInfo,
2413 QualType T, TypeSourceInfo *TInfo,
2414 bool isExplicitSpecified, bool isInline,
2415 bool isImplicitlyDeclared, bool isConstexpr,
2416 InheritedConstructor Inherited)
2417 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2418 SC_None, isInline, isConstexpr, SourceLocation()),
2419 NumCtorInitializers(0), IsInheritingConstructor((bool)Inherited) {
2420 setImplicit(isImplicitlyDeclared);
2422 *getTrailingObjects<InheritedConstructor>() = Inherited;
2423 IsExplicitSpecified = isExplicitSpecified;
2426 void anchor() override;
2429 friend class ASTDeclReader;
2430 friend class ASTDeclWriter;
2431 friend TrailingObjects;
2433 static CXXConstructorDecl *CreateDeserialized(ASTContext &C, unsigned ID,
2434 bool InheritsConstructor);
2435 static CXXConstructorDecl *
2436 Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2437 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2438 bool isExplicit, bool isInline, bool isImplicitlyDeclared,
2440 InheritedConstructor Inherited = InheritedConstructor());
2442 /// \brief Iterates through the member/base initializer list.
2443 using init_iterator = CXXCtorInitializer **;
2445 /// \brief Iterates through the member/base initializer list.
2446 using init_const_iterator = CXXCtorInitializer *const *;
2448 using init_range = llvm::iterator_range<init_iterator>;
2449 using init_const_range = llvm::iterator_range<init_const_iterator>;
2451 init_range inits() { return init_range(init_begin(), init_end()); }
2452 init_const_range inits() const {
2453 return init_const_range(init_begin(), init_end());
2456 /// \brief Retrieve an iterator to the first initializer.
2457 init_iterator init_begin() {
2458 const auto *ConstThis = this;
2459 return const_cast<init_iterator>(ConstThis->init_begin());
2462 /// \brief Retrieve an iterator to the first initializer.
2463 init_const_iterator init_begin() const;
2465 /// \brief Retrieve an iterator past the last initializer.
2466 init_iterator init_end() {
2467 return init_begin() + NumCtorInitializers;
2470 /// \brief Retrieve an iterator past the last initializer.
2471 init_const_iterator init_end() const {
2472 return init_begin() + NumCtorInitializers;
2475 using init_reverse_iterator = std::reverse_iterator<init_iterator>;
2476 using init_const_reverse_iterator =
2477 std::reverse_iterator<init_const_iterator>;
2479 init_reverse_iterator init_rbegin() {
2480 return init_reverse_iterator(init_end());
2482 init_const_reverse_iterator init_rbegin() const {
2483 return init_const_reverse_iterator(init_end());
2486 init_reverse_iterator init_rend() {
2487 return init_reverse_iterator(init_begin());
2489 init_const_reverse_iterator init_rend() const {
2490 return init_const_reverse_iterator(init_begin());
2493 /// \brief Determine the number of arguments used to initialize the member
2495 unsigned getNumCtorInitializers() const {
2496 return NumCtorInitializers;
2499 void setNumCtorInitializers(unsigned numCtorInitializers) {
2500 NumCtorInitializers = numCtorInitializers;
2503 void setCtorInitializers(CXXCtorInitializer **Initializers) {
2504 CtorInitializers = Initializers;
2507 /// Whether this function is marked as explicit explicitly.
2508 bool isExplicitSpecified() const { return IsExplicitSpecified; }
2510 /// Whether this function is explicit.
2511 bool isExplicit() const {
2512 return getCanonicalDecl()->isExplicitSpecified();
2515 /// \brief Determine whether this constructor is a delegating constructor.
2516 bool isDelegatingConstructor() const {
2517 return (getNumCtorInitializers() == 1) &&
2518 init_begin()[0]->isDelegatingInitializer();
2521 /// \brief When this constructor delegates to another, retrieve the target.
2522 CXXConstructorDecl *getTargetConstructor() const;
2524 /// Whether this constructor is a default
2525 /// constructor (C++ [class.ctor]p5), which can be used to
2526 /// default-initialize a class of this type.
2527 bool isDefaultConstructor() const;
2529 /// \brief Whether this constructor is a copy constructor (C++ [class.copy]p2,
2530 /// which can be used to copy the class.
2532 /// \p TypeQuals will be set to the qualifiers on the
2533 /// argument type. For example, \p TypeQuals would be set to \c
2534 /// Qualifiers::Const for the following copy constructor:
2542 bool isCopyConstructor(unsigned &TypeQuals) const;
2544 /// Whether this constructor is a copy
2545 /// constructor (C++ [class.copy]p2, which can be used to copy the
2547 bool isCopyConstructor() const {
2548 unsigned TypeQuals = 0;
2549 return isCopyConstructor(TypeQuals);
2552 /// \brief Determine whether this constructor is a move constructor
2553 /// (C++11 [class.copy]p3), which can be used to move values of the class.
2555 /// \param TypeQuals If this constructor is a move constructor, will be set
2556 /// to the type qualifiers on the referent of the first parameter's type.
2557 bool isMoveConstructor(unsigned &TypeQuals) const;
2559 /// \brief Determine whether this constructor is a move constructor
2560 /// (C++11 [class.copy]p3), which can be used to move values of the class.
2561 bool isMoveConstructor() const {
2562 unsigned TypeQuals = 0;
2563 return isMoveConstructor(TypeQuals);
2566 /// \brief Determine whether this is a copy or move constructor.
2568 /// \param TypeQuals Will be set to the type qualifiers on the reference
2569 /// parameter, if in fact this is a copy or move constructor.
2570 bool isCopyOrMoveConstructor(unsigned &TypeQuals) const;
2572 /// \brief Determine whether this a copy or move constructor.
2573 bool isCopyOrMoveConstructor() const {
2575 return isCopyOrMoveConstructor(Quals);
2578 /// Whether this constructor is a
2579 /// converting constructor (C++ [class.conv.ctor]), which can be
2580 /// used for user-defined conversions.
2581 bool isConvertingConstructor(bool AllowExplicit) const;
2583 /// \brief Determine whether this is a member template specialization that
2584 /// would copy the object to itself. Such constructors are never used to copy
2586 bool isSpecializationCopyingObject() const;
2588 /// \brief Determine whether this is an implicit constructor synthesized to
2589 /// model a call to a constructor inherited from a base class.
2590 bool isInheritingConstructor() const { return IsInheritingConstructor; }
2592 /// \brief Get the constructor that this inheriting constructor is based on.
2593 InheritedConstructor getInheritedConstructor() const {
2594 return IsInheritingConstructor ? *getTrailingObjects<InheritedConstructor>()
2595 : InheritedConstructor();
2598 CXXConstructorDecl *getCanonicalDecl() override {
2599 return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl());
2601 const CXXConstructorDecl *getCanonicalDecl() const {
2602 return const_cast<CXXConstructorDecl*>(this)->getCanonicalDecl();
2605 // Implement isa/cast/dyncast/etc.
2606 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2607 static bool classofKind(Kind K) { return K == CXXConstructor; }
2610 /// \brief Represents a C++ destructor within a class.
2617 /// ~X(); // represented by a CXXDestructorDecl.
2620 class CXXDestructorDecl : public CXXMethodDecl {
2621 friend class ASTDeclReader;
2622 friend class ASTDeclWriter;
2624 // FIXME: Don't allocate storage for these except in the first declaration
2625 // of a virtual destructor.
2626 FunctionDecl *OperatorDelete = nullptr;
2627 Expr *OperatorDeleteThisArg = nullptr;
2629 CXXDestructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2630 const DeclarationNameInfo &NameInfo,
2631 QualType T, TypeSourceInfo *TInfo,
2632 bool isInline, bool isImplicitlyDeclared)
2633 : CXXMethodDecl(CXXDestructor, C, RD, StartLoc, NameInfo, T, TInfo,
2634 SC_None, isInline, /*isConstexpr=*/false, SourceLocation())
2636 setImplicit(isImplicitlyDeclared);
2639 void anchor() override;
2642 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2643 SourceLocation StartLoc,
2644 const DeclarationNameInfo &NameInfo,
2645 QualType T, TypeSourceInfo* TInfo,
2647 bool isImplicitlyDeclared);
2648 static CXXDestructorDecl *CreateDeserialized(ASTContext & C, unsigned ID);
2650 void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
2652 const FunctionDecl *getOperatorDelete() const {
2653 return getCanonicalDecl()->OperatorDelete;
2656 Expr *getOperatorDeleteThisArg() const {
2657 return getCanonicalDecl()->OperatorDeleteThisArg;
2660 CXXDestructorDecl *getCanonicalDecl() override {
2661 return cast<CXXDestructorDecl>(FunctionDecl::getCanonicalDecl());
2663 const CXXDestructorDecl *getCanonicalDecl() const {
2664 return const_cast<CXXDestructorDecl*>(this)->getCanonicalDecl();
2667 // Implement isa/cast/dyncast/etc.
2668 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2669 static bool classofKind(Kind K) { return K == CXXDestructor; }
2672 /// \brief Represents a C++ conversion function within a class.
2679 /// operator bool();
2682 class CXXConversionDecl : public CXXMethodDecl {
2683 CXXConversionDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2684 const DeclarationNameInfo &NameInfo, QualType T,
2685 TypeSourceInfo *TInfo, bool isInline,
2686 bool isExplicitSpecified, bool isConstexpr,
2687 SourceLocation EndLocation)
2688 : CXXMethodDecl(CXXConversion, C, RD, StartLoc, NameInfo, T, TInfo,
2689 SC_None, isInline, isConstexpr, EndLocation) {
2690 IsExplicitSpecified = isExplicitSpecified;
2693 void anchor() override;
2696 friend class ASTDeclReader;
2697 friend class ASTDeclWriter;
2699 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2700 SourceLocation StartLoc,
2701 const DeclarationNameInfo &NameInfo,
2702 QualType T, TypeSourceInfo *TInfo,
2703 bool isInline, bool isExplicit,
2705 SourceLocation EndLocation);
2706 static CXXConversionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2708 /// Whether this function is marked as explicit explicitly.
2709 bool isExplicitSpecified() const { return IsExplicitSpecified; }
2711 /// Whether this function is explicit.
2712 bool isExplicit() const {
2713 return getCanonicalDecl()->isExplicitSpecified();
2716 /// \brief Returns the type that this conversion function is converting to.
2717 QualType getConversionType() const {
2718 return getType()->getAs<FunctionType>()->getReturnType();
2721 /// \brief Determine whether this conversion function is a conversion from
2722 /// a lambda closure type to a block pointer.
2723 bool isLambdaToBlockPointerConversion() const;
2725 CXXConversionDecl *getCanonicalDecl() override {
2726 return cast<CXXConversionDecl>(FunctionDecl::getCanonicalDecl());
2728 const CXXConversionDecl *getCanonicalDecl() const {
2729 return const_cast<CXXConversionDecl*>(this)->getCanonicalDecl();
2732 // Implement isa/cast/dyncast/etc.
2733 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2734 static bool classofKind(Kind K) { return K == CXXConversion; }
2737 /// \brief Represents a linkage specification.
2741 /// extern "C" void foo();
2743 class LinkageSpecDecl : public Decl, public DeclContext {
2744 virtual void anchor();
2747 /// \brief Represents the language in a linkage specification.
2749 /// The values are part of the serialization ABI for
2750 /// ASTs and cannot be changed without altering that ABI. To help
2751 /// ensure a stable ABI for this, we choose the DW_LANG_ encodings
2752 /// from the dwarf standard.
2754 lang_c = /* DW_LANG_C */ 0x0002,
2755 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
2759 /// \brief The language for this linkage specification.
2760 unsigned Language : 3;
2762 /// \brief True if this linkage spec has braces.
2764 /// This is needed so that hasBraces() returns the correct result while the
2765 /// linkage spec body is being parsed. Once RBraceLoc has been set this is
2766 /// not used, so it doesn't need to be serialized.
2767 unsigned HasBraces : 1;
2769 /// \brief The source location for the extern keyword.
2770 SourceLocation ExternLoc;
2772 /// \brief The source location for the right brace (if valid).
2773 SourceLocation RBraceLoc;
2775 LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2776 SourceLocation LangLoc, LanguageIDs lang, bool HasBraces)
2777 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2778 Language(lang), HasBraces(HasBraces), ExternLoc(ExternLoc),
2779 RBraceLoc(SourceLocation()) {}
2782 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
2783 SourceLocation ExternLoc,
2784 SourceLocation LangLoc, LanguageIDs Lang,
2786 static LinkageSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2788 /// \brief Return the language specified by this linkage specification.
2789 LanguageIDs getLanguage() const { return LanguageIDs(Language); }
2791 /// \brief Set the language specified by this linkage specification.
2792 void setLanguage(LanguageIDs L) { Language = L; }
2794 /// \brief Determines whether this linkage specification had braces in
2795 /// its syntactic form.
2796 bool hasBraces() const {
2797 assert(!RBraceLoc.isValid() || HasBraces);
2801 SourceLocation getExternLoc() const { return ExternLoc; }
2802 SourceLocation getRBraceLoc() const { return RBraceLoc; }
2803 void setExternLoc(SourceLocation L) { ExternLoc = L; }
2804 void setRBraceLoc(SourceLocation L) {
2806 HasBraces = RBraceLoc.isValid();
2809 SourceLocation getLocEnd() const LLVM_READONLY {
2811 return getRBraceLoc();
2812 // No braces: get the end location of the (only) declaration in context
2814 return decls_empty() ? getLocation() : decls_begin()->getLocEnd();
2817 SourceRange getSourceRange() const override LLVM_READONLY {
2818 return SourceRange(ExternLoc, getLocEnd());
2821 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2822 static bool classofKind(Kind K) { return K == LinkageSpec; }
2824 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
2825 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
2828 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
2829 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
2833 /// \brief Represents C++ using-directive.
2837 /// using namespace std;
2840 /// \note UsingDirectiveDecl should be Decl not NamedDecl, but we provide
2841 /// artificial names for all using-directives in order to store
2842 /// them in DeclContext effectively.
2843 class UsingDirectiveDecl : public NamedDecl {
2844 /// \brief The location of the \c using keyword.
2845 SourceLocation UsingLoc;
2847 /// \brief The location of the \c namespace keyword.
2848 SourceLocation NamespaceLoc;
2850 /// \brief The nested-name-specifier that precedes the namespace.
2851 NestedNameSpecifierLoc QualifierLoc;
2853 /// \brief The namespace nominated by this using-directive.
2854 NamedDecl *NominatedNamespace;
2856 /// Enclosing context containing both using-directive and nominated
2858 DeclContext *CommonAncestor;
2860 UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
2861 SourceLocation NamespcLoc,
2862 NestedNameSpecifierLoc QualifierLoc,
2863 SourceLocation IdentLoc,
2864 NamedDecl *Nominated,
2865 DeclContext *CommonAncestor)
2866 : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
2867 NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
2868 NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) {}
2870 /// \brief Returns special DeclarationName used by using-directives.
2872 /// This is only used by DeclContext for storing UsingDirectiveDecls in
2873 /// its lookup structure.
2874 static DeclarationName getName() {
2875 return DeclarationName::getUsingDirectiveName();
2878 void anchor() override;
2881 friend class ASTDeclReader;
2883 // Friend for getUsingDirectiveName.
2884 friend class DeclContext;
2886 /// \brief Retrieve the nested-name-specifier that qualifies the
2887 /// name of the namespace, with source-location information.
2888 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2890 /// \brief Retrieve the nested-name-specifier that qualifies the
2891 /// name of the namespace.
2892 NestedNameSpecifier *getQualifier() const {
2893 return QualifierLoc.getNestedNameSpecifier();
2896 NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
2897 const NamedDecl *getNominatedNamespaceAsWritten() const {
2898 return NominatedNamespace;
2901 /// \brief Returns the namespace nominated by this using-directive.
2902 NamespaceDecl *getNominatedNamespace();
2904 const NamespaceDecl *getNominatedNamespace() const {
2905 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
2908 /// \brief Returns the common ancestor context of this using-directive and
2909 /// its nominated namespace.
2910 DeclContext *getCommonAncestor() { return CommonAncestor; }
2911 const DeclContext *getCommonAncestor() const { return CommonAncestor; }
2913 /// \brief Return the location of the \c using keyword.
2914 SourceLocation getUsingLoc() const { return UsingLoc; }
2916 // FIXME: Could omit 'Key' in name.
2917 /// \brief Returns the location of the \c namespace keyword.
2918 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
2920 /// \brief Returns the location of this using declaration's identifier.
2921 SourceLocation getIdentLocation() const { return getLocation(); }
2923 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
2924 SourceLocation UsingLoc,
2925 SourceLocation NamespaceLoc,
2926 NestedNameSpecifierLoc QualifierLoc,
2927 SourceLocation IdentLoc,
2928 NamedDecl *Nominated,
2929 DeclContext *CommonAncestor);
2930 static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2932 SourceRange getSourceRange() const override LLVM_READONLY {
2933 return SourceRange(UsingLoc, getLocation());
2936 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2937 static bool classofKind(Kind K) { return K == UsingDirective; }
2940 /// \brief Represents a C++ namespace alias.
2945 /// namespace Foo = Bar;
2947 class NamespaceAliasDecl : public NamedDecl,
2948 public Redeclarable<NamespaceAliasDecl> {
2949 friend class ASTDeclReader;
2951 /// \brief The location of the \c namespace keyword.
2952 SourceLocation NamespaceLoc;
2954 /// \brief The location of the namespace's identifier.
2956 /// This is accessed by TargetNameLoc.
2957 SourceLocation IdentLoc;
2959 /// \brief The nested-name-specifier that precedes the namespace.
2960 NestedNameSpecifierLoc QualifierLoc;
2962 /// \brief The Decl that this alias points to, either a NamespaceDecl or
2963 /// a NamespaceAliasDecl.
2964 NamedDecl *Namespace;
2966 NamespaceAliasDecl(ASTContext &C, DeclContext *DC,
2967 SourceLocation NamespaceLoc, SourceLocation AliasLoc,
2968 IdentifierInfo *Alias, NestedNameSpecifierLoc QualifierLoc,
2969 SourceLocation IdentLoc, NamedDecl *Namespace)
2970 : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), redeclarable_base(C),
2971 NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
2972 QualifierLoc(QualifierLoc), Namespace(Namespace) {}
2974 void anchor() override;
2976 using redeclarable_base = Redeclarable<NamespaceAliasDecl>;
2978 NamespaceAliasDecl *getNextRedeclarationImpl() override;
2979 NamespaceAliasDecl *getPreviousDeclImpl() override;
2980 NamespaceAliasDecl *getMostRecentDeclImpl() override;
2983 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
2984 SourceLocation NamespaceLoc,
2985 SourceLocation AliasLoc,
2986 IdentifierInfo *Alias,
2987 NestedNameSpecifierLoc QualifierLoc,
2988 SourceLocation IdentLoc,
2989 NamedDecl *Namespace);
2991 static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2993 using redecl_range = redeclarable_base::redecl_range;
2994 using redecl_iterator = redeclarable_base::redecl_iterator;
2996 using redeclarable_base::redecls_begin;
2997 using redeclarable_base::redecls_end;
2998 using redeclarable_base::redecls;
2999 using redeclarable_base::getPreviousDecl;
3000 using redeclarable_base::getMostRecentDecl;
3002 NamespaceAliasDecl *getCanonicalDecl() override {
3003 return getFirstDecl();
3005 const NamespaceAliasDecl *getCanonicalDecl() const {
3006 return getFirstDecl();
3009 /// \brief Retrieve the nested-name-specifier that qualifies the
3010 /// name of the namespace, with source-location information.
3011 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3013 /// \brief Retrieve the nested-name-specifier that qualifies the
3014 /// name of the namespace.
3015 NestedNameSpecifier *getQualifier() const {
3016 return QualifierLoc.getNestedNameSpecifier();
3019 /// \brief Retrieve the namespace declaration aliased by this directive.
3020 NamespaceDecl *getNamespace() {
3021 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
3022 return AD->getNamespace();
3024 return cast<NamespaceDecl>(Namespace);
3027 const NamespaceDecl *getNamespace() const {
3028 return const_cast<NamespaceAliasDecl*>(this)->getNamespace();
3031 /// Returns the location of the alias name, i.e. 'foo' in
3032 /// "namespace foo = ns::bar;".
3033 SourceLocation getAliasLoc() const { return getLocation(); }
3035 /// Returns the location of the \c namespace keyword.
3036 SourceLocation getNamespaceLoc() const { return NamespaceLoc; }
3038 /// Returns the location of the identifier in the named namespace.
3039 SourceLocation getTargetNameLoc() const { return IdentLoc; }
3041 /// \brief Retrieve the namespace that this alias refers to, which
3042 /// may either be a NamespaceDecl or a NamespaceAliasDecl.
3043 NamedDecl *getAliasedNamespace() const { return Namespace; }
3045 SourceRange getSourceRange() const override LLVM_READONLY {
3046 return SourceRange(NamespaceLoc, IdentLoc);
3049 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3050 static bool classofKind(Kind K) { return K == NamespaceAlias; }
3053 /// \brief Represents a shadow declaration introduced into a scope by a
3054 /// (resolved) using declaration.
3062 /// using A::foo; // <- a UsingDecl
3063 /// // Also creates a UsingShadowDecl for A::foo() in B
3066 class UsingShadowDecl : public NamedDecl, public Redeclarable<UsingShadowDecl> {
3067 friend class UsingDecl;
3069 /// The referenced declaration.
3070 NamedDecl *Underlying = nullptr;
3072 /// \brief The using declaration which introduced this decl or the next using
3073 /// shadow declaration contained in the aforementioned using declaration.
3074 NamedDecl *UsingOrNextShadow = nullptr;
3076 void anchor() override;
3078 using redeclarable_base = Redeclarable<UsingShadowDecl>;
3080 UsingShadowDecl *getNextRedeclarationImpl() override {
3081 return getNextRedeclaration();
3084 UsingShadowDecl *getPreviousDeclImpl() override {
3085 return getPreviousDecl();
3088 UsingShadowDecl *getMostRecentDeclImpl() override {
3089 return getMostRecentDecl();
3093 UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, SourceLocation Loc,
3094 UsingDecl *Using, NamedDecl *Target);
3095 UsingShadowDecl(Kind K, ASTContext &C, EmptyShell);
3098 friend class ASTDeclReader;
3099 friend class ASTDeclWriter;
3101 static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
3102 SourceLocation Loc, UsingDecl *Using,
3103 NamedDecl *Target) {
3104 return new (C, DC) UsingShadowDecl(UsingShadow, C, DC, Loc, Using, Target);
3107 static UsingShadowDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3109 using redecl_range = redeclarable_base::redecl_range;
3110 using redecl_iterator = redeclarable_base::redecl_iterator;
3112 using redeclarable_base::redecls_begin;
3113 using redeclarable_base::redecls_end;
3114 using redeclarable_base::redecls;
3115 using redeclarable_base::getPreviousDecl;
3116 using redeclarable_base::getMostRecentDecl;
3117 using redeclarable_base::isFirstDecl;
3119 UsingShadowDecl *getCanonicalDecl() override {
3120 return getFirstDecl();
3122 const UsingShadowDecl *getCanonicalDecl() const {
3123 return getFirstDecl();
3126 /// \brief Gets the underlying declaration which has been brought into the
3128 NamedDecl *getTargetDecl() const { return Underlying; }
3130 /// \brief Sets the underlying declaration which has been brought into the
3132 void setTargetDecl(NamedDecl* ND) {
3133 assert(ND && "Target decl is null!");
3135 IdentifierNamespace = ND->getIdentifierNamespace();
3138 /// \brief Gets the using declaration to which this declaration is tied.
3139 UsingDecl *getUsingDecl() const;
3141 /// \brief The next using shadow declaration contained in the shadow decl
3142 /// chain of the using declaration which introduced this decl.
3143 UsingShadowDecl *getNextUsingShadowDecl() const {
3144 return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow);
3147 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3148 static bool classofKind(Kind K) {
3149 return K == Decl::UsingShadow || K == Decl::ConstructorUsingShadow;
3153 /// \brief Represents a shadow constructor declaration introduced into a
3154 /// class by a C++11 using-declaration that names a constructor.
3158 /// struct Base { Base(int); };
3159 /// struct Derived {
3160 /// using Base::Base; // creates a UsingDecl and a ConstructorUsingShadowDecl
3163 class ConstructorUsingShadowDecl final : public UsingShadowDecl {
3164 /// \brief If this constructor using declaration inherted the constructor
3165 /// from an indirect base class, this is the ConstructorUsingShadowDecl
3166 /// in the named direct base class from which the declaration was inherited.
3167 ConstructorUsingShadowDecl *NominatedBaseClassShadowDecl = nullptr;
3169 /// \brief If this constructor using declaration inherted the constructor
3170 /// from an indirect base class, this is the ConstructorUsingShadowDecl
3171 /// that will be used to construct the unique direct or virtual base class
3172 /// that receives the constructor arguments.
3173 ConstructorUsingShadowDecl *ConstructedBaseClassShadowDecl = nullptr;
3175 /// \brief \c true if the constructor ultimately named by this using shadow
3176 /// declaration is within a virtual base class subobject of the class that
3177 /// contains this declaration.
3178 unsigned IsVirtual : 1;
3180 ConstructorUsingShadowDecl(ASTContext &C, DeclContext *DC, SourceLocation Loc,
3181 UsingDecl *Using, NamedDecl *Target,
3182 bool TargetInVirtualBase)
3183 : UsingShadowDecl(ConstructorUsingShadow, C, DC, Loc, Using,
3184 Target->getUnderlyingDecl()),
3185 NominatedBaseClassShadowDecl(
3186 dyn_cast<ConstructorUsingShadowDecl>(Target)),
3187 ConstructedBaseClassShadowDecl(NominatedBaseClassShadowDecl),
3188 IsVirtual(TargetInVirtualBase) {
3189 // If we found a constructor that chains to a constructor for a virtual
3190 // base, we should directly call that virtual base constructor instead.
3191 // FIXME: This logic belongs in Sema.
3192 if (NominatedBaseClassShadowDecl &&
3193 NominatedBaseClassShadowDecl->constructsVirtualBase()) {
3194 ConstructedBaseClassShadowDecl =
3195 NominatedBaseClassShadowDecl->ConstructedBaseClassShadowDecl;
3200 ConstructorUsingShadowDecl(ASTContext &C, EmptyShell Empty)
3201 : UsingShadowDecl(ConstructorUsingShadow, C, Empty), IsVirtual(false) {}
3203 void anchor() override;
3206 friend class ASTDeclReader;
3207 friend class ASTDeclWriter;
3209 static ConstructorUsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
3211 UsingDecl *Using, NamedDecl *Target,
3213 static ConstructorUsingShadowDecl *CreateDeserialized(ASTContext &C,
3216 /// Returns the parent of this using shadow declaration, which
3217 /// is the class in which this is declared.
3219 const CXXRecordDecl *getParent() const {
3220 return cast<CXXRecordDecl>(getDeclContext());
3222 CXXRecordDecl *getParent() {
3223 return cast<CXXRecordDecl>(getDeclContext());
3227 /// \brief Get the inheriting constructor declaration for the direct base
3228 /// class from which this using shadow declaration was inherited, if there is
3229 /// one. This can be different for each redeclaration of the same shadow decl.
3230 ConstructorUsingShadowDecl *getNominatedBaseClassShadowDecl() const {
3231 return NominatedBaseClassShadowDecl;
3234 /// \brief Get the inheriting constructor declaration for the base class
3235 /// for which we don't have an explicit initializer, if there is one.
3236 ConstructorUsingShadowDecl *getConstructedBaseClassShadowDecl() const {
3237 return ConstructedBaseClassShadowDecl;
3240 /// \brief Get the base class that was named in the using declaration. This
3241 /// can be different for each redeclaration of this same shadow decl.
3242 CXXRecordDecl *getNominatedBaseClass() const;
3244 /// \brief Get the base class whose constructor or constructor shadow
3245 /// declaration is passed the constructor arguments.
3246 CXXRecordDecl *getConstructedBaseClass() const {
3247 return cast<CXXRecordDecl>((ConstructedBaseClassShadowDecl
3248 ? ConstructedBaseClassShadowDecl
3250 ->getDeclContext());
3253 /// \brief Returns \c true if the constructed base class is a virtual base
3254 /// class subobject of this declaration's class.
3255 bool constructsVirtualBase() const {
3259 /// \brief Get the constructor or constructor template in the derived class
3260 /// correspnding to this using shadow declaration, if it has been implicitly
3261 /// declared already.
3262 CXXConstructorDecl *getConstructor() const;
3263 void setConstructor(NamedDecl *Ctor);
3265 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3266 static bool classofKind(Kind K) { return K == ConstructorUsingShadow; }
3269 /// \brief Represents a C++ using-declaration.
3273 /// using someNameSpace::someIdentifier;
3275 class UsingDecl : public NamedDecl, public Mergeable<UsingDecl> {
3276 /// \brief The source location of the 'using' keyword itself.
3277 SourceLocation UsingLocation;
3279 /// \brief The nested-name-specifier that precedes the name.
3280 NestedNameSpecifierLoc QualifierLoc;
3282 /// \brief Provides source/type location info for the declaration name
3283 /// embedded in the ValueDecl base class.
3284 DeclarationNameLoc DNLoc;
3286 /// \brief The first shadow declaration of the shadow decl chain associated
3287 /// with this using declaration.
3289 /// The bool member of the pair store whether this decl has the \c typename
3291 llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow;
3293 UsingDecl(DeclContext *DC, SourceLocation UL,
3294 NestedNameSpecifierLoc QualifierLoc,
3295 const DeclarationNameInfo &NameInfo, bool HasTypenameKeyword)
3296 : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()),
3297 UsingLocation(UL), QualifierLoc(QualifierLoc),
3298 DNLoc(NameInfo.getInfo()), FirstUsingShadow(nullptr, HasTypenameKeyword) {
3301 void anchor() override;
3304 friend class ASTDeclReader;
3305 friend class ASTDeclWriter;
3307 /// \brief Return the source location of the 'using' keyword.
3308 SourceLocation getUsingLoc() const { return UsingLocation; }
3310 /// \brief Set the source location of the 'using' keyword.
3311 void setUsingLoc(SourceLocation L) { UsingLocation = L; }
3313 /// \brief Retrieve the nested-name-specifier that qualifies the name,
3314 /// with source-location information.
3315 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3317 /// \brief Retrieve the nested-name-specifier that qualifies the name.
3318 NestedNameSpecifier *getQualifier() const {
3319 return QualifierLoc.getNestedNameSpecifier();
3322 DeclarationNameInfo getNameInfo() const {
3323 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
3326 /// \brief Return true if it is a C++03 access declaration (no 'using').
3327 bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
3329 /// \brief Return true if the using declaration has 'typename'.
3330 bool hasTypename() const { return FirstUsingShadow.getInt(); }
3332 /// \brief Sets whether the using declaration has 'typename'.
3333 void setTypename(bool TN) { FirstUsingShadow.setInt(TN); }
3335 /// \brief Iterates through the using shadow declarations associated with
3336 /// this using declaration.
3337 class shadow_iterator {
3338 /// \brief The current using shadow declaration.
3339 UsingShadowDecl *Current = nullptr;
3342 using value_type = UsingShadowDecl *;
3343 using reference = UsingShadowDecl *;
3344 using pointer = UsingShadowDecl *;
3345 using iterator_category = std::forward_iterator_tag;
3346 using difference_type = std::ptrdiff_t;
3348 shadow_iterator() = default;
3349 explicit shadow_iterator(UsingShadowDecl *C) : Current(C) {}
3351 reference operator*() const { return Current; }
3352 pointer operator->() const { return Current; }
3354 shadow_iterator& operator++() {
3355 Current = Current->getNextUsingShadowDecl();
3359 shadow_iterator operator++(int) {
3360 shadow_iterator tmp(*this);
3365 friend bool operator==(shadow_iterator x, shadow_iterator y) {
3366 return x.Current == y.Current;
3368 friend bool operator!=(shadow_iterator x, shadow_iterator y) {
3369 return x.Current != y.Current;
3373 using shadow_range = llvm::iterator_range<shadow_iterator>;
3375 shadow_range shadows() const {
3376 return shadow_range(shadow_begin(), shadow_end());
3379 shadow_iterator shadow_begin() const {
3380 return shadow_iterator(FirstUsingShadow.getPointer());
3383 shadow_iterator shadow_end() const { return shadow_iterator(); }
3385 /// \brief Return the number of shadowed declarations associated with this
3386 /// using declaration.
3387 unsigned shadow_size() const {
3388 return std::distance(shadow_begin(), shadow_end());
3391 void addShadowDecl(UsingShadowDecl *S);
3392 void removeShadowDecl(UsingShadowDecl *S);
3394 static UsingDecl *Create(ASTContext &C, DeclContext *DC,
3395 SourceLocation UsingL,
3396 NestedNameSpecifierLoc QualifierLoc,
3397 const DeclarationNameInfo &NameInfo,
3398 bool HasTypenameKeyword);
3400 static UsingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3402 SourceRange getSourceRange() const override LLVM_READONLY;
3404 /// Retrieves the canonical declaration of this declaration.
3405 UsingDecl *getCanonicalDecl() override { return getFirstDecl(); }
3406 const UsingDecl *getCanonicalDecl() const { return getFirstDecl(); }
3408 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3409 static bool classofKind(Kind K) { return K == Using; }
3412 /// Represents a pack of using declarations that a single
3413 /// using-declarator pack-expanded into.
3416 /// template<typename ...T> struct X : T... {
3417 /// using T::operator()...;
3418 /// using T::operator T...;
3422 /// In the second case above, the UsingPackDecl will have the name
3423 /// 'operator T' (which contains an unexpanded pack), but the individual
3424 /// UsingDecls and UsingShadowDecls will have more reasonable names.
3425 class UsingPackDecl final
3426 : public NamedDecl, public Mergeable<UsingPackDecl>,
3427 private llvm::TrailingObjects<UsingPackDecl, NamedDecl *> {
3428 /// The UnresolvedUsingValueDecl or UnresolvedUsingTypenameDecl from
3429 /// which this waas instantiated.
3430 NamedDecl *InstantiatedFrom;
3432 /// The number of using-declarations created by this pack expansion.
3433 unsigned NumExpansions;
3435 UsingPackDecl(DeclContext *DC, NamedDecl *InstantiatedFrom,
3436 ArrayRef<NamedDecl *> UsingDecls)
3437 : NamedDecl(UsingPack, DC,
3438 InstantiatedFrom ? InstantiatedFrom->getLocation()
3440 InstantiatedFrom ? InstantiatedFrom->getDeclName()
3441 : DeclarationName()),
3442 InstantiatedFrom(InstantiatedFrom), NumExpansions(UsingDecls.size()) {
3443 std::uninitialized_copy(UsingDecls.begin(), UsingDecls.end(),
3444 getTrailingObjects<NamedDecl *>());
3447 void anchor() override;
3450 friend class ASTDeclReader;
3451 friend class ASTDeclWriter;
3452 friend TrailingObjects;
3454 /// Get the using declaration from which this was instantiated. This will
3455 /// always be an UnresolvedUsingValueDecl or an UnresolvedUsingTypenameDecl
3456 /// that is a pack expansion.
3457 NamedDecl *getInstantiatedFromUsingDecl() const { return InstantiatedFrom; }
3459 /// Get the set of using declarations that this pack expanded into. Note that
3460 /// some of these may still be unresolved.
3461 ArrayRef<NamedDecl *> expansions() const {
3462 return llvm::makeArrayRef(getTrailingObjects<NamedDecl *>(), NumExpansions);
3465 static UsingPackDecl *Create(ASTContext &C, DeclContext *DC,
3466 NamedDecl *InstantiatedFrom,
3467 ArrayRef<NamedDecl *> UsingDecls);
3469 static UsingPackDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3470 unsigned NumExpansions);
3472 SourceRange getSourceRange() const override LLVM_READONLY {
3473 return InstantiatedFrom->getSourceRange();
3476 UsingPackDecl *getCanonicalDecl() override { return getFirstDecl(); }
3477 const UsingPackDecl *getCanonicalDecl() const { return getFirstDecl(); }
3479 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3480 static bool classofKind(Kind K) { return K == UsingPack; }
3483 /// \brief Represents a dependent using declaration which was not marked with
3486 /// Unlike non-dependent using declarations, these *only* bring through
3487 /// non-types; otherwise they would break two-phase lookup.
3490 /// template \<class T> class A : public Base<T> {
3491 /// using Base<T>::foo;
3494 class UnresolvedUsingValueDecl : public ValueDecl,
3495 public Mergeable<UnresolvedUsingValueDecl> {
3496 /// \brief The source location of the 'using' keyword
3497 SourceLocation UsingLocation;
3499 /// \brief If this is a pack expansion, the location of the '...'.
3500 SourceLocation EllipsisLoc;
3502 /// \brief The nested-name-specifier that precedes the name.
3503 NestedNameSpecifierLoc QualifierLoc;
3505 /// \brief Provides source/type location info for the declaration name
3506 /// embedded in the ValueDecl base class.
3507 DeclarationNameLoc DNLoc;
3509 UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
3510 SourceLocation UsingLoc,
3511 NestedNameSpecifierLoc QualifierLoc,
3512 const DeclarationNameInfo &NameInfo,
3513 SourceLocation EllipsisLoc)
3514 : ValueDecl(UnresolvedUsingValue, DC,
3515 NameInfo.getLoc(), NameInfo.getName(), Ty),
3516 UsingLocation(UsingLoc), EllipsisLoc(EllipsisLoc),
3517 QualifierLoc(QualifierLoc), DNLoc(NameInfo.getInfo()) {}
3519 void anchor() override;
3522 friend class ASTDeclReader;
3523 friend class ASTDeclWriter;
3525 /// \brief Returns the source location of the 'using' keyword.
3526 SourceLocation getUsingLoc() const { return UsingLocation; }
3528 /// \brief Set the source location of the 'using' keyword.
3529 void setUsingLoc(SourceLocation L) { UsingLocation = L; }
3531 /// \brief Return true if it is a C++03 access declaration (no 'using').
3532 bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
3534 /// \brief Retrieve the nested-name-specifier that qualifies the name,
3535 /// with source-location information.
3536 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3538 /// \brief Retrieve the nested-name-specifier that qualifies the name.
3539 NestedNameSpecifier *getQualifier() const {
3540 return QualifierLoc.getNestedNameSpecifier();
3543 DeclarationNameInfo getNameInfo() const {
3544 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
3547 /// \brief Determine whether this is a pack expansion.
3548 bool isPackExpansion() const {
3549 return EllipsisLoc.isValid();
3552 /// \brief Get the location of the ellipsis if this is a pack expansion.
3553 SourceLocation getEllipsisLoc() const {
3557 static UnresolvedUsingValueDecl *
3558 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
3559 NestedNameSpecifierLoc QualifierLoc,
3560 const DeclarationNameInfo &NameInfo, SourceLocation EllipsisLoc);
3562 static UnresolvedUsingValueDecl *
3563 CreateDeserialized(ASTContext &C, unsigned ID);
3565 SourceRange getSourceRange() const override LLVM_READONLY;
3567 /// Retrieves the canonical declaration of this declaration.
3568 UnresolvedUsingValueDecl *getCanonicalDecl() override {
3569 return getFirstDecl();
3571 const UnresolvedUsingValueDecl *getCanonicalDecl() const {
3572 return getFirstDecl();
3575 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3576 static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
3579 /// \brief Represents a dependent using declaration which was marked with
3583 /// template \<class T> class A : public Base<T> {
3584 /// using typename Base<T>::foo;
3588 /// The type associated with an unresolved using typename decl is
3589 /// currently always a typename type.
3590 class UnresolvedUsingTypenameDecl
3592 public Mergeable<UnresolvedUsingTypenameDecl> {
3593 friend class ASTDeclReader;
3595 /// \brief The source location of the 'typename' keyword
3596 SourceLocation TypenameLocation;
3598 /// \brief If this is a pack expansion, the location of the '...'.
3599 SourceLocation EllipsisLoc;
3601 /// \brief The nested-name-specifier that precedes the name.
3602 NestedNameSpecifierLoc QualifierLoc;
3604 UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
3605 SourceLocation TypenameLoc,
3606 NestedNameSpecifierLoc QualifierLoc,
3607 SourceLocation TargetNameLoc,
3608 IdentifierInfo *TargetName,
3609 SourceLocation EllipsisLoc)
3610 : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName,
3612 TypenameLocation(TypenameLoc), EllipsisLoc(EllipsisLoc),
3613 QualifierLoc(QualifierLoc) {}
3615 void anchor() override;
3618 /// \brief Returns the source location of the 'using' keyword.
3619 SourceLocation getUsingLoc() const { return getLocStart(); }
3621 /// \brief Returns the source location of the 'typename' keyword.
3622 SourceLocation getTypenameLoc() const { return TypenameLocation; }
3624 /// \brief Retrieve the nested-name-specifier that qualifies the name,
3625 /// with source-location information.
3626 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3628 /// \brief Retrieve the nested-name-specifier that qualifies the name.
3629 NestedNameSpecifier *getQualifier() const {
3630 return QualifierLoc.getNestedNameSpecifier();
3633 DeclarationNameInfo getNameInfo() const {
3634 return DeclarationNameInfo(getDeclName(), getLocation());
3637 /// \brief Determine whether this is a pack expansion.
3638 bool isPackExpansion() const {
3639 return EllipsisLoc.isValid();
3642 /// \brief Get the location of the ellipsis if this is a pack expansion.
3643 SourceLocation getEllipsisLoc() const {
3647 static UnresolvedUsingTypenameDecl *
3648 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
3649 SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc,
3650 SourceLocation TargetNameLoc, DeclarationName TargetName,
3651 SourceLocation EllipsisLoc);
3653 static UnresolvedUsingTypenameDecl *
3654 CreateDeserialized(ASTContext &C, unsigned ID);
3656 /// Retrieves the canonical declaration of this declaration.
3657 UnresolvedUsingTypenameDecl *getCanonicalDecl() override {
3658 return getFirstDecl();
3660 const UnresolvedUsingTypenameDecl *getCanonicalDecl() const {
3661 return getFirstDecl();
3664 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3665 static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
3668 /// \brief Represents a C++11 static_assert declaration.
3669 class StaticAssertDecl : public Decl {
3670 llvm::PointerIntPair<Expr *, 1, bool> AssertExprAndFailed;
3671 StringLiteral *Message;
3672 SourceLocation RParenLoc;
3674 StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc,
3675 Expr *AssertExpr, StringLiteral *Message,
3676 SourceLocation RParenLoc, bool Failed)
3677 : Decl(StaticAssert, DC, StaticAssertLoc),
3678 AssertExprAndFailed(AssertExpr, Failed), Message(Message),
3679 RParenLoc(RParenLoc) {}
3681 virtual void anchor();
3684 friend class ASTDeclReader;
3686 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
3687 SourceLocation StaticAssertLoc,
3688 Expr *AssertExpr, StringLiteral *Message,
3689 SourceLocation RParenLoc, bool Failed);
3690 static StaticAssertDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3692 Expr *getAssertExpr() { return AssertExprAndFailed.getPointer(); }
3693 const Expr *getAssertExpr() const { return AssertExprAndFailed.getPointer(); }
3695 StringLiteral *getMessage() { return Message; }
3696 const StringLiteral *getMessage() const { return Message; }
3698 bool isFailed() const { return AssertExprAndFailed.getInt(); }
3700 SourceLocation getRParenLoc() const { return RParenLoc; }
3702 SourceRange getSourceRange() const override LLVM_READONLY {
3703 return SourceRange(getLocation(), getRParenLoc());
3706 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3707 static bool classofKind(Kind K) { return K == StaticAssert; }
3710 /// A binding in a decomposition declaration. For instance, given:
3713 /// auto &[a, b, c] = n;
3715 /// a, b, and c are BindingDecls, whose bindings are the expressions
3716 /// x[0], x[1], and x[2] respectively, where x is the implicit
3717 /// DecompositionDecl of type 'int (&)[3]'.
3718 class BindingDecl : public ValueDecl {
3719 /// The binding represented by this declaration. References to this
3720 /// declaration are effectively equivalent to this expression (except
3721 /// that it is only evaluated once at the point of declaration of the
3723 Expr *Binding = nullptr;
3725 BindingDecl(DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id)
3726 : ValueDecl(Decl::Binding, DC, IdLoc, Id, QualType()) {}
3728 void anchor() override;
3731 friend class ASTDeclReader;
3733 static BindingDecl *Create(ASTContext &C, DeclContext *DC,
3734 SourceLocation IdLoc, IdentifierInfo *Id);
3735 static BindingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3737 /// Get the expression to which this declaration is bound. This may be null
3738 /// in two different cases: while parsing the initializer for the
3739 /// decomposition declaration, and when the initializer is type-dependent.
3740 Expr *getBinding() const { return Binding; }
3742 /// Get the variable (if any) that holds the value of evaluating the binding.
3743 /// Only present for user-defined bindings for tuple-like types.
3744 VarDecl *getHoldingVar() const;
3746 /// Set the binding for this BindingDecl, along with its declared type (which
3747 /// should be a possibly-cv-qualified form of the type of the binding, or a
3748 /// reference to such a type).
3749 void setBinding(QualType DeclaredType, Expr *Binding) {
3750 setType(DeclaredType);
3751 this->Binding = Binding;
3754 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3755 static bool classofKind(Kind K) { return K == Decl::Binding; }
3758 /// A decomposition declaration. For instance, given:
3761 /// auto &[a, b, c] = n;
3763 /// the second line declares a DecompositionDecl of type 'int (&)[3]', and
3764 /// three BindingDecls (named a, b, and c). An instance of this class is always
3765 /// unnamed, but behaves in almost all other respects like a VarDecl.
3766 class DecompositionDecl final
3768 private llvm::TrailingObjects<DecompositionDecl, BindingDecl *> {
3769 /// The number of BindingDecl*s following this object.
3770 unsigned NumBindings;
3772 DecompositionDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
3773 SourceLocation LSquareLoc, QualType T,
3774 TypeSourceInfo *TInfo, StorageClass SC,
3775 ArrayRef<BindingDecl *> Bindings)
3776 : VarDecl(Decomposition, C, DC, StartLoc, LSquareLoc, nullptr, T, TInfo,
3778 NumBindings(Bindings.size()) {
3779 std::uninitialized_copy(Bindings.begin(), Bindings.end(),
3780 getTrailingObjects<BindingDecl *>());
3783 void anchor() override;
3786 friend class ASTDeclReader;
3787 friend TrailingObjects;
3789 static DecompositionDecl *Create(ASTContext &C, DeclContext *DC,
3790 SourceLocation StartLoc,
3791 SourceLocation LSquareLoc,
3792 QualType T, TypeSourceInfo *TInfo,
3794 ArrayRef<BindingDecl *> Bindings);
3795 static DecompositionDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3796 unsigned NumBindings);
3798 ArrayRef<BindingDecl *> bindings() const {
3799 return llvm::makeArrayRef(getTrailingObjects<BindingDecl *>(), NumBindings);
3802 void printName(raw_ostream &os) const override;
3804 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3805 static bool classofKind(Kind K) { return K == Decomposition; }
3808 /// An instance of this class represents the declaration of a property
3809 /// member. This is a Microsoft extension to C++, first introduced in
3810 /// Visual Studio .NET 2003 as a parallel to similar features in C#
3811 /// and Managed C++.
3813 /// A property must always be a non-static class member.
3815 /// A property member superficially resembles a non-static data
3816 /// member, except preceded by a property attribute:
3817 /// __declspec(property(get=GetX, put=PutX)) int x;
3818 /// Either (but not both) of the 'get' and 'put' names may be omitted.
3820 /// A reference to a property is always an lvalue. If the lvalue
3821 /// undergoes lvalue-to-rvalue conversion, then a getter name is
3822 /// required, and that member is called with no arguments.
3823 /// If the lvalue is assigned into, then a setter name is required,
3824 /// and that member is called with one argument, the value assigned.
3825 /// Both operations are potentially overloaded. Compound assignments
3826 /// are permitted, as are the increment and decrement operators.
3828 /// The getter and putter methods are permitted to be overloaded,
3829 /// although their return and parameter types are subject to certain
3830 /// restrictions according to the type of the property.
3832 /// A property declared using an incomplete array type may
3833 /// additionally be subscripted, adding extra parameters to the getter
3834 /// and putter methods.
3835 class MSPropertyDecl : public DeclaratorDecl {
3836 IdentifierInfo *GetterId, *SetterId;
3838 MSPropertyDecl(DeclContext *DC, SourceLocation L, DeclarationName N,
3839 QualType T, TypeSourceInfo *TInfo, SourceLocation StartL,
3840 IdentifierInfo *Getter, IdentifierInfo *Setter)
3841 : DeclaratorDecl(MSProperty, DC, L, N, T, TInfo, StartL),
3842 GetterId(Getter), SetterId(Setter) {}
3845 friend class ASTDeclReader;
3847 static MSPropertyDecl *Create(ASTContext &C, DeclContext *DC,
3848 SourceLocation L, DeclarationName N, QualType T,
3849 TypeSourceInfo *TInfo, SourceLocation StartL,
3850 IdentifierInfo *Getter, IdentifierInfo *Setter);
3851 static MSPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3853 static bool classof(const Decl *D) { return D->getKind() == MSProperty; }
3855 bool hasGetter() const { return GetterId != nullptr; }
3856 IdentifierInfo* getGetterId() const { return GetterId; }
3857 bool hasSetter() const { return SetterId != nullptr; }
3858 IdentifierInfo* getSetterId() const { return SetterId; }
3861 /// Insertion operator for diagnostics. This allows sending an AccessSpecifier
3862 /// into a diagnostic with <<.
3863 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
3864 AccessSpecifier AS);
3866 const PartialDiagnostic &operator<<(const PartialDiagnostic &DB,
3867 AccessSpecifier AS);
3869 } // namespace clang
3871 #endif // LLVM_CLANG_AST_DECLCXX_H