1//===- DeclCXX.h - Classes for representing C++ declarations --*- C++ -*-=====//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// Defines the C++ Decl subclasses, other than those for templates
12/// (found in DeclTemplate.h) and friends (in DeclFriend.h).
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_AST_DECLCXX_H
17#define LLVM_CLANG_AST_DECLCXX_H
18
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/DeclBase.h"
24#include "clang/AST/DeclarationName.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/ExternalASTSource.h"
27#include "clang/AST/LambdaCapture.h"
28#include "clang/AST/NestedNameSpecifier.h"
29#include "clang/AST/Redeclarable.h"
30#include "clang/AST/Stmt.h"
31#include "clang/AST/Type.h"
32#include "clang/AST/TypeLoc.h"
33#include "clang/AST/UnresolvedSet.h"
34#include "clang/Basic/LLVM.h"
35#include "clang/Basic/Lambda.h"
36#include "clang/Basic/LangOptions.h"
37#include "clang/Basic/OperatorKinds.h"
38#include "clang/Basic/SourceLocation.h"
39#include "clang/Basic/Specifiers.h"
40#include "llvm/ADT/ArrayRef.h"
41#include "llvm/ADT/DenseMap.h"
42#include "llvm/ADT/PointerIntPair.h"
43#include "llvm/ADT/PointerUnion.h"
44#include "llvm/ADT/STLExtras.h"
45#include "llvm/ADT/iterator_range.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/Compiler.h"
48#include "llvm/Support/PointerLikeTypeTraits.h"
49#include "llvm/Support/TrailingObjects.h"
50#include <cassert>
51#include <cstddef>
52#include <iterator>
53#include <memory>
54#include <vector>
55
56namespace clang {
57
58class ClassTemplateDecl;
59class ConstructorUsingShadowDecl;
60class CXXBasePath;
61class CXXBasePaths;
62class CXXConstructorDecl;
63class CXXDestructorDecl;
64class CXXFinalOverriderMap;
65class CXXIndirectPrimaryBaseSet;
66class CXXMethodDecl;
67class DiagnosticBuilder;
68class FriendDecl;
69class FunctionTemplateDecl;
70class IdentifierInfo;
71class MemberSpecializationInfo;
72class TemplateDecl;
73class TemplateParameterList;
74class UsingDecl;
75
76/// Represents any kind of function declaration, whether it is a
77/// concrete function or a function template.
78class AnyFunctionDecl {
79 NamedDecl *Function;
80
81 AnyFunctionDecl(NamedDecl *ND) : Function(ND) {}
82
83public:
84 AnyFunctionDecl(FunctionDecl *FD) : Function(FD) {}
85 AnyFunctionDecl(FunctionTemplateDecl *FTD);
86
87 /// Implicily converts any function or function template into a
88 /// named declaration.
89 operator NamedDecl *() const { return Function; }
90
91 /// Retrieve the underlying function or function template.
92 NamedDecl *get() const { return Function; }
93
94 static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) {
95 return AnyFunctionDecl(ND);
96 }
97};
98
99} // namespace clang
100
101namespace llvm {
102
103 // Provide PointerLikeTypeTraits for non-cvr pointers.
104 template<>
105 struct PointerLikeTypeTraits< ::clang::AnyFunctionDecl> {
106 static void *getAsVoidPointer(::clang::AnyFunctionDecl F) {
107 return F.get();
108 }
109
110 static ::clang::AnyFunctionDecl getFromVoidPointer(void *P) {
111 return ::clang::AnyFunctionDecl::getFromNamedDecl(
112 static_cast< ::clang::NamedDecl*>(P));
113 }
114
115 enum { NumLowBitsAvailable = 2 };
116 };
117
118} // namespace llvm
119
120namespace clang {
121
122/// Represents an access specifier followed by colon ':'.
123///
124/// An objects of this class represents sugar for the syntactic occurrence
125/// of an access specifier followed by a colon in the list of member
126/// specifiers of a C++ class definition.
127///
128/// Note that they do not represent other uses of access specifiers,
129/// such as those occurring in a list of base specifiers.
130/// Also note that this class has nothing to do with so-called
131/// "access declarations" (C++98 11.3 [class.access.dcl]).
132class AccessSpecDecl : public Decl {
133 /// The location of the ':'.
134 SourceLocation ColonLoc;
135
136 AccessSpecDecl(AccessSpecifier AS, DeclContext *DC,
137 SourceLocation ASLoc, SourceLocation ColonLoc)
138 : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) {
139 setAccess(AS);
140 }
141
142 AccessSpecDecl(EmptyShell Empty) : Decl(AccessSpec, Empty) {}
143
144 virtual void anchor();
145
146public:
147 /// The location of the access specifier.
148 SourceLocation getAccessSpecifierLoc() const { return getLocation(); }
149
150 /// Sets the location of the access specifier.
151 void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); }
152
153 /// The location of the colon following the access specifier.
154 SourceLocation getColonLoc() const { return ColonLoc; }
155
156 /// Sets the location of the colon.
157 void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; }
158
159 SourceRange getSourceRange() const override LLVM_READONLY {
160 return SourceRange(getAccessSpecifierLoc(), getColonLoc());
161 }
162
163 static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS,
164 DeclContext *DC, SourceLocation ASLoc,
165 SourceLocation ColonLoc) {
166 return new (C, DC) AccessSpecDecl(AS, DC, ASLoc, ColonLoc);
167 }
168
169 static AccessSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
170
171 // Implement isa/cast/dyncast/etc.
172 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
173 static bool classofKind(Kind K) { return K == AccessSpec; }
174};
175
176/// Represents a base class of a C++ class.
177///
178/// Each CXXBaseSpecifier represents a single, direct base class (or
179/// struct) of a C++ class (or struct). It specifies the type of that
180/// base class, whether it is a virtual or non-virtual base, and what
181/// level of access (public, protected, private) is used for the
182/// derivation. For example:
183///
184/// \code
185/// class A { };
186/// class B { };
187/// class C : public virtual A, protected B { };
188/// \endcode
189///
190/// In this code, C will have two CXXBaseSpecifiers, one for "public
191/// virtual A" and the other for "protected B".
192class CXXBaseSpecifier {
193 /// The source code range that covers the full base
194 /// specifier, including the "virtual" (if present) and access
195 /// specifier (if present).
196 SourceRange Range;
197
198 /// The source location of the ellipsis, if this is a pack
199 /// expansion.
200 SourceLocation EllipsisLoc;
201
202 /// Whether this is a virtual base class or not.
203 unsigned Virtual : 1;
204
205 /// Whether this is the base of a class (true) or of a struct (false).
206 ///
207 /// This determines the mapping from the access specifier as written in the
208 /// source code to the access specifier used for semantic analysis.
209 unsigned BaseOfClass : 1;
210
211 /// Access specifier as written in the source code (may be AS_none).
212 ///
213 /// The actual type of data stored here is an AccessSpecifier, but we use
214 /// "unsigned" here to work around a VC++ bug.
215 unsigned Access : 2;
216
217 /// Whether the class contains a using declaration
218 /// to inherit the named class's constructors.
219 unsigned InheritConstructors : 1;
220
221 /// The type of the base class.
222 ///
223 /// This will be a class or struct (or a typedef of such). The source code
224 /// range does not include the \c virtual or the access specifier.
225 TypeSourceInfo *BaseTypeInfo;
226
227public:
228 CXXBaseSpecifier() = default;
229 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A,
230 TypeSourceInfo *TInfo, SourceLocation EllipsisLoc)
231 : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC),
232 Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) {}
233
234 /// Retrieves the source range that contains the entire base specifier.
235 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
236 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
237 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
238
239 /// Get the location at which the base class type was written.
240 SourceLocation getBaseTypeLoc() const LLVM_READONLY {
241 return BaseTypeInfo->getTypeLoc().getLocStart();
242 }
243
244 /// Determines whether the base class is a virtual base class (or not).
245 bool isVirtual() const { return Virtual; }
246
247 /// Determine whether this base class is a base of a class declared
248 /// with the 'class' keyword (vs. one declared with the 'struct' keyword).
249 bool isBaseOfClass() const { return BaseOfClass; }
250
251 /// Determine whether this base specifier is a pack expansion.
252 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
253
254 /// Determine whether this base class's constructors get inherited.
255 bool getInheritConstructors() const { return InheritConstructors; }
256
257 /// Set that this base class's constructors should be inherited.
258 void setInheritConstructors(bool Inherit = true) {
259 InheritConstructors = Inherit;
260 }
261
262 /// For a pack expansion, determine the location of the ellipsis.
263 SourceLocation getEllipsisLoc() const {
264 return EllipsisLoc;
265 }
266
267 /// Returns the access specifier for this base specifier.
268 ///
269 /// This is the actual base specifier as used for semantic analysis, so
270 /// the result can never be AS_none. To retrieve the access specifier as
271 /// written in the source code, use getAccessSpecifierAsWritten().
272 AccessSpecifier getAccessSpecifier() const {
273 if ((AccessSpecifier)Access == AS_none)
274 return BaseOfClass? AS_private : AS_public;
275 else
276 return (AccessSpecifier)Access;
277 }
278
279 /// Retrieves the access specifier as written in the source code
280 /// (which may mean that no access specifier was explicitly written).
281 ///
282 /// Use getAccessSpecifier() to retrieve the access specifier for use in
283 /// semantic analysis.
284 AccessSpecifier getAccessSpecifierAsWritten() const {
285 return (AccessSpecifier)Access;
286 }
287
288 /// Retrieves the type of the base class.
289 ///
290 /// This type will always be an unqualified class type.
291 QualType getType() const {
292 return BaseTypeInfo->getType().getUnqualifiedType();
293 }
294
295 /// Retrieves the type and source location of the base class.
296 TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; }
297};
298
299/// Represents a C++ struct/union/class.
300class CXXRecordDecl : public RecordDecl {
301 friend class ASTDeclReader;
302 friend class ASTDeclWriter;
303 friend class ASTNodeImporter;
304 friend class ASTReader;
305 friend class ASTRecordWriter;
306 friend class ASTWriter;
307 friend class DeclContext;
308 friend class LambdaExpr;
309
310 friend void FunctionDecl::setPure(bool);
311 friend void TagDecl::startDefinition();
312
313 /// Values used in DefinitionData fields to represent special members.
314 enum SpecialMemberFlags {
315 SMF_DefaultConstructor = 0x1,
316 SMF_CopyConstructor = 0x2,
317 SMF_MoveConstructor = 0x4,
318 SMF_CopyAssignment = 0x8,
319 SMF_MoveAssignment = 0x10,
320 SMF_Destructor = 0x20,
321 SMF_All = 0x3f
322 };
323
324 struct DefinitionData {
325 /// True if this class has any user-declared constructors.
326 unsigned UserDeclaredConstructor : 1;
327
328 /// The user-declared special members which this class has.
329 unsigned UserDeclaredSpecialMembers : 6;
330
331 /// True when this class is an aggregate.
332 unsigned Aggregate : 1;
333
334 /// True when this class is a POD-type.
335 unsigned PlainOldData : 1;
336
337 /// true when this class is empty for traits purposes,
338 /// i.e. has no data members other than 0-width bit-fields, has no
339 /// virtual function/base, and doesn't inherit from a non-empty
340 /// class. Doesn't take union-ness into account.
341 unsigned Empty : 1;
342
343 /// True when this class is polymorphic, i.e., has at
344 /// least one virtual member or derives from a polymorphic class.
345 unsigned Polymorphic : 1;
346
347 /// True when this class is abstract, i.e., has at least
348 /// one pure virtual function, (that can come from a base class).
349 unsigned Abstract : 1;
350
351 /// True when this class is standard-layout, per the applicable
352 /// language rules (including DRs).
353 unsigned IsStandardLayout : 1;
354
355 /// True when this class was standard-layout under the C++11
356 /// definition.
357 ///
358 /// C++11 [class]p7. A standard-layout class is a class that:
359 /// * has no non-static data members of type non-standard-layout class (or
360 /// array of such types) or reference,
361 /// * has no virtual functions (10.3) and no virtual base classes (10.1),
362 /// * has the same access control (Clause 11) for all non-static data
363 /// members
364 /// * has no non-standard-layout base classes,
365 /// * either has no non-static data members in the most derived class and at
366 /// most one base class with non-static data members, or has no base
367 /// classes with non-static data members, and
368 /// * has no base classes of the same type as the first non-static data
369 /// member.
370 unsigned IsCXX11StandardLayout : 1;
371
372 /// True when any base class has any declared non-static data
373 /// members or bit-fields.
374 /// This is a helper bit of state used to implement IsStandardLayout more
375 /// efficiently.
376 unsigned HasBasesWithFields : 1;
377
378 /// True when any base class has any declared non-static data
379 /// members.
380 /// This is a helper bit of state used to implement IsCXX11StandardLayout
381 /// more efficiently.
382 unsigned HasBasesWithNonStaticDataMembers : 1;
383
384 /// True when there are private non-static data members.
385 unsigned HasPrivateFields : 1;
386
387 /// True when there are protected non-static data members.
388 unsigned HasProtectedFields : 1;
389
390 /// True when there are private non-static data members.
391 unsigned HasPublicFields : 1;
392
393 /// True if this class (or any subobject) has mutable fields.
394 unsigned HasMutableFields : 1;
395
396 /// True if this class (or any nested anonymous struct or union)
397 /// has variant members.
398 unsigned HasVariantMembers : 1;
399
400 /// True if there no non-field members declared by the user.
401 unsigned HasOnlyCMembers : 1;
402
403 /// True if any field has an in-class initializer, including those
404 /// within anonymous unions or structs.
405 unsigned HasInClassInitializer : 1;
406
407 /// True if any field is of reference type, and does not have an
408 /// in-class initializer.
409 ///
410 /// In this case, value-initialization of this class is illegal in C++98
411 /// even if the class has a trivial default constructor.
412 unsigned HasUninitializedReferenceMember : 1;
413
414 /// True if any non-mutable field whose type doesn't have a user-
415 /// provided default ctor also doesn't have an in-class initializer.
416 unsigned HasUninitializedFields : 1;
417
418 /// True if there are any member using-declarations that inherit
419 /// constructors from a base class.
420 unsigned HasInheritedConstructor : 1;
421
422 /// True if there are any member using-declarations named
423 /// 'operator='.
424 unsigned HasInheritedAssignment : 1;
425
426 /// These flags are \c true if a defaulted corresponding special
427 /// member can't be fully analyzed without performing overload resolution.
428 /// @{
429 unsigned NeedOverloadResolutionForCopyConstructor : 1;
430 unsigned NeedOverloadResolutionForMoveConstructor : 1;
431 unsigned NeedOverloadResolutionForMoveAssignment : 1;
432 unsigned NeedOverloadResolutionForDestructor : 1;
433 /// @}
434
435 /// These flags are \c true if an implicit defaulted corresponding
436 /// special member would be defined as deleted.
437 /// @{
438 unsigned DefaultedCopyConstructorIsDeleted : 1;
439 unsigned DefaultedMoveConstructorIsDeleted : 1;
440 unsigned DefaultedMoveAssignmentIsDeleted : 1;
441 unsigned DefaultedDestructorIsDeleted : 1;
442 /// @}
443
444 /// The trivial special members which this class has, per
445 /// C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25,
446 /// C++11 [class.dtor]p5, or would have if the member were not suppressed.
447 ///
448 /// This excludes any user-declared but not user-provided special members
449 /// which have been declared but not yet defined.
450 unsigned HasTrivialSpecialMembers : 6;
451
452 /// These bits keep track of the triviality of special functions for the
453 /// purpose of calls. Only the bits corresponding to SMF_CopyConstructor,
454 /// SMF_MoveConstructor, and SMF_Destructor are meaningful here.
455 unsigned HasTrivialSpecialMembersForCall : 6;
456
457 /// The declared special members of this class which are known to be
458 /// non-trivial.
459 ///
460 /// This excludes any user-declared but not user-provided special members
461 /// which have been declared but not yet defined, and any implicit special
462 /// members which have not yet been declared.
463 unsigned DeclaredNonTrivialSpecialMembers : 6;
464
465 /// These bits keep track of the declared special members that are
466 /// non-trivial for the purpose of calls.
467 /// Only the bits corresponding to SMF_CopyConstructor,
468 /// SMF_MoveConstructor, and SMF_Destructor are meaningful here.
469 unsigned DeclaredNonTrivialSpecialMembersForCall : 6;
470
471 /// True when this class has a destructor with no semantic effect.
472 unsigned HasIrrelevantDestructor : 1;
473
474 /// True when this class has at least one user-declared constexpr
475 /// constructor which is neither the copy nor move constructor.
476 unsigned HasConstexprNonCopyMoveConstructor : 1;
477
478 /// True if this class has a (possibly implicit) defaulted default
479 /// constructor.
480 unsigned HasDefaultedDefaultConstructor : 1;
481
482 /// True if a defaulted default constructor for this class would
483 /// be constexpr.
484 unsigned DefaultedDefaultConstructorIsConstexpr : 1;
485
486 /// True if this class has a constexpr default constructor.
487 ///
488 /// This is true for either a user-declared constexpr default constructor
489 /// or an implicitly declared constexpr default constructor.
490 unsigned HasConstexprDefaultConstructor : 1;
491
492 /// True when this class contains at least one non-static data
493 /// member or base class of non-literal or volatile type.
494 unsigned HasNonLiteralTypeFieldsOrBases : 1;
495
496 /// True when visible conversion functions are already computed
497 /// and are available.
498 unsigned ComputedVisibleConversions : 1;
499
500 /// Whether we have a C++11 user-provided default constructor (not
501 /// explicitly deleted or defaulted).
502 unsigned UserProvidedDefaultConstructor : 1;
503
504 /// The special members which have been declared for this class,
505 /// either by the user or implicitly.
506 unsigned DeclaredSpecialMembers : 6;
507
508 /// Whether an implicit copy constructor could have a const-qualified
509 /// parameter, for initializing virtual bases and for other subobjects.
510 unsigned ImplicitCopyConstructorCanHaveConstParamForVBase : 1;
511 unsigned ImplicitCopyConstructorCanHaveConstParamForNonVBase : 1;
512
513 /// Whether an implicit copy assignment operator would have a
514 /// const-qualified parameter.
515 unsigned ImplicitCopyAssignmentHasConstParam : 1;
516
517 /// Whether any declared copy constructor has a const-qualified
518 /// parameter.
519 unsigned HasDeclaredCopyConstructorWithConstParam : 1;
520
521 /// Whether any declared copy assignment operator has either a
522 /// const-qualified reference parameter or a non-reference parameter.
523 unsigned HasDeclaredCopyAssignmentWithConstParam : 1;
524
525 /// Whether this class describes a C++ lambda.
526 unsigned IsLambda : 1;
527
528 /// Whether we are currently parsing base specifiers.
529 unsigned IsParsingBaseSpecifiers : 1;
530
531 unsigned HasODRHash : 1;
532
533 /// A hash of parts of the class to help in ODR checking.
534 unsigned ODRHash = 0;
535
536 /// The number of base class specifiers in Bases.
537 unsigned NumBases = 0;
538
539 /// The number of virtual base class specifiers in VBases.
540 unsigned NumVBases = 0;
541
542 /// Base classes of this class.
543 ///
544 /// FIXME: This is wasted space for a union.
545 LazyCXXBaseSpecifiersPtr Bases;
546
547 /// direct and indirect virtual base classes of this class.
548 LazyCXXBaseSpecifiersPtr VBases;
549
550 /// The conversion functions of this C++ class (but not its
551 /// inherited conversion functions).
552 ///
553 /// Each of the entries in this overload set is a CXXConversionDecl.
554 LazyASTUnresolvedSet Conversions;
555
556 /// The conversion functions of this C++ class and all those
557 /// inherited conversion functions that are visible in this class.
558 ///
559 /// Each of the entries in this overload set is a CXXConversionDecl or a
560 /// FunctionTemplateDecl.
561 LazyASTUnresolvedSet VisibleConversions;
562
563 /// The declaration which defines this record.
564 CXXRecordDecl *Definition;
565
566 /// The first friend declaration in this class, or null if there
567 /// aren't any.
568 ///
569 /// This is actually currently stored in reverse order.
570 LazyDeclPtr FirstFriend;
571
572 DefinitionData(CXXRecordDecl *D);
573
574 /// Retrieve the set of direct base classes.
575 CXXBaseSpecifier *getBases() const {
576 if (!Bases.isOffset())
577 return Bases.get(nullptr);
578 return getBasesSlowCase();
579 }
580
581 /// Retrieve the set of virtual base classes.
582 CXXBaseSpecifier *getVBases() const {
583 if (!VBases.isOffset())
584 return VBases.get(nullptr);
585 return getVBasesSlowCase();
586 }
587
588 ArrayRef<CXXBaseSpecifier> bases() const {
589 return llvm::makeArrayRef(getBases(), NumBases);
590 }
591
592 ArrayRef<CXXBaseSpecifier> vbases() const {
593 return llvm::makeArrayRef(getVBases(), NumVBases);
594 }
595
596 private:
597 CXXBaseSpecifier *getBasesSlowCase() const;
598 CXXBaseSpecifier *getVBasesSlowCase() const;
599 };
600
601 struct DefinitionData *DefinitionData;
602
603 /// Describes a C++ closure type (generated by a lambda expression).
604 struct LambdaDefinitionData : public DefinitionData {
605 using Capture = LambdaCapture;
606
607 /// Whether this lambda is known to be dependent, even if its
608 /// context isn't dependent.
609 ///
610 /// A lambda with a non-dependent context can be dependent if it occurs
611 /// within the default argument of a function template, because the
612 /// lambda will have been created with the enclosing context as its
613 /// declaration context, rather than function. This is an unfortunate
614 /// artifact of having to parse the default arguments before.
615 unsigned Dependent : 1;
616
617 /// Whether this lambda is a generic lambda.
618 unsigned IsGenericLambda : 1;
619
620 /// The Default Capture.
621 unsigned CaptureDefault : 2;
622
623 /// The number of captures in this lambda is limited 2^NumCaptures.
624 unsigned NumCaptures : 15;
625
626 /// The number of explicit captures in this lambda.
627 unsigned NumExplicitCaptures : 13;
628
629 /// The number used to indicate this lambda expression for name
630 /// mangling in the Itanium C++ ABI.
631 unsigned ManglingNumber = 0;
632
633 /// The declaration that provides context for this lambda, if the
634 /// actual DeclContext does not suffice. This is used for lambdas that
635 /// occur within default arguments of function parameters within the class
636 /// or within a data member initializer.
637 LazyDeclPtr ContextDecl;
638
639 /// The list of captures, both explicit and implicit, for this
640 /// lambda.
641 Capture *Captures = nullptr;
642
643 /// The type of the call method.
644 TypeSourceInfo *MethodTyInfo;
645
646 LambdaDefinitionData(CXXRecordDecl *D, TypeSourceInfo *Info,
647 bool Dependent, bool IsGeneric,
648 LambdaCaptureDefault CaptureDefault)
649 : DefinitionData(D), Dependent(Dependent), IsGenericLambda(IsGeneric),
650 CaptureDefault(CaptureDefault), NumCaptures(0), NumExplicitCaptures(0),
651 MethodTyInfo(Info) {
652 IsLambda = true;
653
654 // C++1z [expr.prim.lambda]p4:
655 // This class type is not an aggregate type.
656 Aggregate = false;
657 PlainOldData = false;
658 }
659 };
660
661 struct DefinitionData *dataPtr() const {
662 // Complete the redecl chain (if necessary).
663 getMostRecentDecl();
664 return DefinitionData;
665 }
666
667 struct DefinitionData &data() const {
668 auto *DD = dataPtr();
669 assert(DD && "queried property of class with no definition");
670 return *DD;
671 }
672
673 struct LambdaDefinitionData &getLambdaData() const {
674 // No update required: a merged definition cannot change any lambda
675 // properties.
676 auto *DD = DefinitionData;
677 assert(DD && DD->IsLambda && "queried lambda property of non-lambda class");
678 return static_cast<LambdaDefinitionData&>(*DD);
679 }
680
681 /// The template or declaration that this declaration
682 /// describes or was instantiated from, respectively.
683 ///
684 /// For non-templates, this value will be null. For record
685 /// declarations that describe a class template, this will be a
686 /// pointer to a ClassTemplateDecl. For member
687 /// classes of class template specializations, this will be the
688 /// MemberSpecializationInfo referring to the member class that was
689 /// instantiated or specialized.
690 llvm::PointerUnion<ClassTemplateDecl *, MemberSpecializationInfo *>
691 TemplateOrInstantiation;
692
693 /// Called from setBases and addedMember to notify the class that a
694 /// direct or virtual base class or a member of class type has been added.
695 void addedClassSubobject(CXXRecordDecl *Base);
696
697 /// Notify the class that member has been added.
698 ///
699 /// This routine helps maintain information about the class based on which
700 /// members have been added. It will be invoked by DeclContext::addDecl()
701 /// whenever a member is added to this record.
702 void addedMember(Decl *D);
703
704 void markedVirtualFunctionPure();
705
706 /// Get the head of our list of friend declarations, possibly
707 /// deserializing the friends from an external AST source.
708 FriendDecl *getFirstFriend() const;
709
710 /// Determine whether this class has an empty base class subobject of type X
711 /// or of one of the types that might be at offset 0 within X (per the C++
712 /// "standard layout" rules).
713 bool hasSubobjectAtOffsetZeroOfEmptyBaseType(ASTContext &Ctx,
714 const CXXRecordDecl *X);
715
716protected:
717 CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C, DeclContext *DC,
718 SourceLocation StartLoc, SourceLocation IdLoc,
719 IdentifierInfo *Id, CXXRecordDecl *PrevDecl);
720
721public:
722 /// Iterator that traverses the base classes of a class.
723 using base_class_iterator = CXXBaseSpecifier *;
724
725 /// Iterator that traverses the base classes of a class.
726 using base_class_const_iterator = const CXXBaseSpecifier *;
727
728 CXXRecordDecl *getCanonicalDecl() override {
729 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl());
730 }
731
732 const CXXRecordDecl *getCanonicalDecl() const {
733 return const_cast<CXXRecordDecl*>(this)->getCanonicalDecl();
734 }
735
736 CXXRecordDecl *getPreviousDecl() {
737 return cast_or_null<CXXRecordDecl>(
738 static_cast<RecordDecl *>(this)->getPreviousDecl());
739 }
740
741 const CXXRecordDecl *getPreviousDecl() const {
742 return const_cast<CXXRecordDecl*>(this)->getPreviousDecl();
743 }
744
745 CXXRecordDecl *getMostRecentDecl() {
746 return cast<CXXRecordDecl>(
747 static_cast<RecordDecl *>(this)->getMostRecentDecl());
748 }
749
750 const CXXRecordDecl *getMostRecentDecl() const {
751 return const_cast<CXXRecordDecl*>(this)->getMostRecentDecl();
752 }
753
754 CXXRecordDecl *getMostRecentNonInjectedDecl() {
755 CXXRecordDecl *Recent =
756 static_cast<CXXRecordDecl *>(this)->getMostRecentDecl();
757 while (Recent->isInjectedClassName()) {
758 // FIXME: Does injected class name need to be in the redeclarations chain?
759 assert(Recent->getPreviousDecl());
760 Recent = Recent->getPreviousDecl();
761 }
762 return Recent;
763 }
764
765 const CXXRecordDecl *getMostRecentNonInjectedDecl() const {
766 return const_cast<CXXRecordDecl*>(this)->getMostRecentNonInjectedDecl();
767 }
768
769 CXXRecordDecl *getDefinition() const {
770 // We only need an update if we don't already know which
771 // declaration is the definition.
772 auto *DD = DefinitionData ? DefinitionData : dataPtr();
773 return DD ? DD->Definition : nullptr;
774 }
775
776 bool hasDefinition() const { return DefinitionData || dataPtr(); }
777
778 static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
779 SourceLocation StartLoc, SourceLocation IdLoc,
780 IdentifierInfo *Id,
781 CXXRecordDecl *PrevDecl = nullptr,
782 bool DelayTypeCreation = false);
783 static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC,
784 TypeSourceInfo *Info, SourceLocation Loc,
785 bool DependentLambda, bool IsGeneric,
786 LambdaCaptureDefault CaptureDefault);
787 static CXXRecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
788
789 bool isDynamicClass() const {
790 return data().Polymorphic || data().NumVBases != 0;
791 }
792
793 void setIsParsingBaseSpecifiers() { data().IsParsingBaseSpecifiers = true; }
794
795 bool isParsingBaseSpecifiers() const {
796 return data().IsParsingBaseSpecifiers;
797 }
798
799 unsigned getODRHash() const;
800
801 /// Sets the base classes of this struct or class.
802 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);
803
804 /// Retrieves the number of base classes of this class.
805 unsigned getNumBases() const { return data().NumBases; }
806
807 using base_class_range = llvm::iterator_range<base_class_iterator>;
808 using base_class_const_range =
809 llvm::iterator_range<base_class_const_iterator>;
810
811 base_class_range bases() {
812 return base_class_range(bases_begin(), bases_end());
813 }
814 base_class_const_range bases() const {
815 return base_class_const_range(bases_begin(), bases_end());
816 }
817
818 base_class_iterator bases_begin() { return data().getBases(); }
819 base_class_const_iterator bases_begin() const { return data().getBases(); }
820 base_class_iterator bases_end() { return bases_begin() + data().NumBases; }
821 base_class_const_iterator bases_end() const {
822 return bases_begin() + data().NumBases;
823 }
824
825 /// Retrieves the number of virtual base classes of this class.
826 unsigned getNumVBases() const { return data().NumVBases; }
827
828 base_class_range vbases() {
829 return base_class_range(vbases_begin(), vbases_end());
830 }
831 base_class_const_range vbases() const {
832 return base_class_const_range(vbases_begin(), vbases_end());
833 }
834
835 base_class_iterator vbases_begin() { return data().getVBases(); }
836 base_class_const_iterator vbases_begin() const { return data().getVBases(); }
837 base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; }
838 base_class_const_iterator vbases_end() const {
839 return vbases_begin() + data().NumVBases;
840 }
841
842 /// Determine whether this class has any dependent base classes which
843 /// are not the current instantiation.
844 bool hasAnyDependentBases() const;
845
846 /// Iterator access to method members. The method iterator visits
847 /// all method members of the class, including non-instance methods,
848 /// special methods, etc.
849 using method_iterator = specific_decl_iterator<CXXMethodDecl>;
850 using method_range =
851 llvm::iterator_range<specific_decl_iterator<CXXMethodDecl>>;
852
853 method_range methods() const {
854 return method_range(method_begin(), method_end());
855 }
856
857 /// Method begin iterator. Iterates in the order the methods
858 /// were declared.
859 method_iterator method_begin() const {
860 return method_iterator(decls_begin());
861 }
862
863 /// Method past-the-end iterator.
864 method_iterator method_end() const {
865 return method_iterator(decls_end());
866 }
867
868 /// Iterator access to constructor members.
869 using ctor_iterator = specific_decl_iterator<CXXConstructorDecl>;
870 using ctor_range =
871 llvm::iterator_range<specific_decl_iterator<CXXConstructorDecl>>;
872
873 ctor_range ctors() const { return ctor_range(ctor_begin(), ctor_end()); }
874
875 ctor_iterator ctor_begin() const {
876 return ctor_iterator(decls_begin());
877 }
878
879 ctor_iterator ctor_end() const {
880 return ctor_iterator(decls_end());
881 }
882
883 /// An iterator over friend declarations. All of these are defined
884 /// in DeclFriend.h.
885 class friend_iterator;
886 using friend_range = llvm::iterator_range<friend_iterator>;
887
888 friend_range friends() const;
889 friend_iterator friend_begin() const;
890 friend_iterator friend_end() const;
891 void pushFriendDecl(FriendDecl *FD);
892
893 /// Determines whether this record has any friends.
894 bool hasFriends() const {
895 return data().FirstFriend.isValid();
896 }
897
898 /// \c true if a defaulted copy constructor for this class would be
899 /// deleted.
900 bool defaultedCopyConstructorIsDeleted() const {
901 assert((!needsOverloadResolutionForCopyConstructor() ||
902 (data().DeclaredSpecialMembers & SMF_CopyConstructor)) &&
903 "this property has not yet been computed by Sema");
904 return data().DefaultedCopyConstructorIsDeleted;
905 }
906
907 /// \c true if a defaulted move constructor for this class would be
908 /// deleted.
909 bool defaultedMoveConstructorIsDeleted() const {
910 assert((!needsOverloadResolutionForMoveConstructor() ||
911 (data().DeclaredSpecialMembers & SMF_MoveConstructor)) &&
912 "this property has not yet been computed by Sema");
913 return data().DefaultedMoveConstructorIsDeleted;
914 }
915
916 /// \c true if a defaulted destructor for this class would be deleted.
917 bool defaultedDestructorIsDeleted() const {
918 assert((!needsOverloadResolutionForDestructor() ||
919 (data().DeclaredSpecialMembers & SMF_Destructor)) &&
920 "this property has not yet been computed by Sema");
921 return data().DefaultedDestructorIsDeleted;
922 }
923
924 /// \c true if we know for sure that this class has a single,
925 /// accessible, unambiguous copy constructor that is not deleted.
926 bool hasSimpleCopyConstructor() const {
927 return !hasUserDeclaredCopyConstructor() &&
928 !data().DefaultedCopyConstructorIsDeleted;
929 }
930
931 /// \c true if we know for sure that this class has a single,
932 /// accessible, unambiguous move constructor that is not deleted.
933 bool hasSimpleMoveConstructor() const {
934 return !hasUserDeclaredMoveConstructor() && hasMoveConstructor() &&
935 !data().DefaultedMoveConstructorIsDeleted;
936 }
937
938 /// \c true if we know for sure that this class has a single,
939 /// accessible, unambiguous move assignment operator that is not deleted.
940 bool hasSimpleMoveAssignment() const {
941 return !hasUserDeclaredMoveAssignment() && hasMoveAssignment() &&
942 !data().DefaultedMoveAssignmentIsDeleted;
943 }
944
945 /// \c true if we know for sure that this class has an accessible
946 /// destructor that is not deleted.
947 bool hasSimpleDestructor() const {
948 return !hasUserDeclaredDestructor() &&
949 !data().DefaultedDestructorIsDeleted;
950 }
951
952 /// Determine whether this class has any default constructors.
953 bool hasDefaultConstructor() const {
954 return (data().DeclaredSpecialMembers & SMF_DefaultConstructor) ||
955 needsImplicitDefaultConstructor();
956 }
957
958 /// Determine if we need to declare a default constructor for
959 /// this class.
960 ///
961 /// This value is used for lazy creation of default constructors.
962 bool needsImplicitDefaultConstructor() const {
963 return !data().UserDeclaredConstructor &&
964 !(data().DeclaredSpecialMembers & SMF_DefaultConstructor) &&
965 // C++14 [expr.prim.lambda]p20:
966 // The closure type associated with a lambda-expression has no
967 // default constructor.
968 !isLambda();
969 }
970
971 /// Determine whether this class has any user-declared constructors.
972 ///
973 /// When true, a default constructor will not be implicitly declared.
974 bool hasUserDeclaredConstructor() const {
975 return data().UserDeclaredConstructor;
976 }
977
978 /// Whether this class has a user-provided default constructor
979 /// per C++11.
980 bool hasUserProvidedDefaultConstructor() const {
981 return data().UserProvidedDefaultConstructor;
982 }
983
984 /// Determine whether this class has a user-declared copy constructor.
985 ///
986 /// When false, a copy constructor will be implicitly declared.
987 bool hasUserDeclaredCopyConstructor() const {
988 return data().UserDeclaredSpecialMembers & SMF_CopyConstructor;
989 }
990
991 /// Determine whether this class needs an implicit copy
992 /// constructor to be lazily declared.
993 bool needsImplicitCopyConstructor() const {
994 return !(data().DeclaredSpecialMembers & SMF_CopyConstructor);
995 }
996
997 /// Determine whether we need to eagerly declare a defaulted copy
998 /// constructor for this class.
999 bool needsOverloadResolutionForCopyConstructor() const {
1000 // C++17 [class.copy.ctor]p6:
1001 // If the class definition declares a move constructor or move assignment
1002 // operator, the implicitly declared copy constructor is defined as
1003 // deleted.
1004 // In MSVC mode, sometimes a declared move assignment does not delete an
1005 // implicit copy constructor, so defer this choice to Sema.
1006 if (data().UserDeclaredSpecialMembers &
1007 (SMF_MoveConstructor | SMF_MoveAssignment))
1008 return true;
1009 return data().NeedOverloadResolutionForCopyConstructor;
1010 }
1011
1012 /// Determine whether an implicit copy constructor for this type
1013 /// would have a parameter with a const-qualified reference type.
1014 bool implicitCopyConstructorHasConstParam() const {
1015 return data().ImplicitCopyConstructorCanHaveConstParamForNonVBase &&
1016 (isAbstract() ||
1017 data().ImplicitCopyConstructorCanHaveConstParamForVBase);
1018 }
1019
1020 /// Determine whether this class has a copy constructor with
1021 /// a parameter type which is a reference to a const-qualified type.
1022 bool hasCopyConstructorWithConstParam() const {
1023 return data().HasDeclaredCopyConstructorWithConstParam ||
1024 (needsImplicitCopyConstructor() &&
1025 implicitCopyConstructorHasConstParam());
1026 }
1027
1028 /// Whether this class has a user-declared move constructor or
1029 /// assignment operator.
1030 ///
1031 /// When false, a move constructor and assignment operator may be
1032 /// implicitly declared.
1033 bool hasUserDeclaredMoveOperation() const {
1034 return data().UserDeclaredSpecialMembers &
1035 (SMF_MoveConstructor | SMF_MoveAssignment);
1036 }
1037
1038 /// Determine whether this class has had a move constructor
1039 /// declared by the user.
1040 bool hasUserDeclaredMoveConstructor() const {
1041 return data().UserDeclaredSpecialMembers & SMF_MoveConstructor;
1042 }
1043
1044 /// Determine whether this class has a move constructor.
1045 bool hasMoveConstructor() const {
1046 return (data().DeclaredSpecialMembers & SMF_MoveConstructor) ||
1047 needsImplicitMoveConstructor();
1048 }
1049
1050 /// Set that we attempted to declare an implicit copy
1051 /// constructor, but overload resolution failed so we deleted it.
1052 void setImplicitCopyConstructorIsDeleted() {
1053 assert((data().DefaultedCopyConstructorIsDeleted ||
1054 needsOverloadResolutionForCopyConstructor()) &&
1055 "Copy constructor should not be deleted");
1056 data().DefaultedCopyConstructorIsDeleted = true;
1057 }
1058
1059 /// Set that we attempted to declare an implicit move
1060 /// constructor, but overload resolution failed so we deleted it.
1061 void setImplicitMoveConstructorIsDeleted() {
1062 assert((data().DefaultedMoveConstructorIsDeleted ||
1063 needsOverloadResolutionForMoveConstructor()) &&
1064 "move constructor should not be deleted");
1065 data().DefaultedMoveConstructorIsDeleted = true;
1066 }
1067
1068 /// Set that we attempted to declare an implicit destructor,
1069 /// but overload resolution failed so we deleted it.
1070 void setImplicitDestructorIsDeleted() {
1071 assert((data().DefaultedDestructorIsDeleted ||
1072 needsOverloadResolutionForDestructor()) &&
1073 "destructor should not be deleted");
1074 data().DefaultedDestructorIsDeleted = true;
1075 }
1076
1077 /// Determine whether this class should get an implicit move
1078 /// constructor or if any existing special member function inhibits this.
1079 bool needsImplicitMoveConstructor() const {
1080 return !(data().DeclaredSpecialMembers & SMF_MoveConstructor) &&
1081 !hasUserDeclaredCopyConstructor() &&
1082 !hasUserDeclaredCopyAssignment() &&
1083 !hasUserDeclaredMoveAssignment() &&
1084 !hasUserDeclaredDestructor();
1085 }
1086
1087 /// Determine whether we need to eagerly declare a defaulted move
1088 /// constructor for this class.
1089 bool needsOverloadResolutionForMoveConstructor() const {
1090 return data().NeedOverloadResolutionForMoveConstructor;
1091 }
1092
1093 /// Determine whether this class has a user-declared copy assignment
1094 /// operator.
1095 ///
1096 /// When false, a copy assignment operator will be implicitly declared.
1097 bool hasUserDeclaredCopyAssignment() const {
1098 return data().UserDeclaredSpecialMembers & SMF_CopyAssignment;
1099 }
1100
1101 /// Determine whether this class needs an implicit copy
1102 /// assignment operator to be lazily declared.
1103 bool needsImplicitCopyAssignment() const {
1104 return !(data().DeclaredSpecialMembers & SMF_CopyAssignment);
1105 }
1106
1107 /// Determine whether we need to eagerly declare a defaulted copy
1108 /// assignment operator for this class.
1109 bool needsOverloadResolutionForCopyAssignment() const {
1110 return data().HasMutableFields;
1111 }
1112
1113 /// Determine whether an implicit copy assignment operator for this
1114 /// type would have a parameter with a const-qualified reference type.
1115 bool implicitCopyAssignmentHasConstParam() const {
1116 return data().ImplicitCopyAssignmentHasConstParam;
1117 }
1118
1119 /// Determine whether this class has a copy assignment operator with
1120 /// a parameter type which is a reference to a const-qualified type or is not
1121 /// a reference.
1122 bool hasCopyAssignmentWithConstParam() const {
1123 return data().HasDeclaredCopyAssignmentWithConstParam ||
1124 (needsImplicitCopyAssignment() &&
1125 implicitCopyAssignmentHasConstParam());
1126 }
1127
1128 /// Determine whether this class has had a move assignment
1129 /// declared by the user.
1130 bool hasUserDeclaredMoveAssignment() const {
1131 return data().UserDeclaredSpecialMembers & SMF_MoveAssignment;
1132 }
1133
1134 /// Determine whether this class has a move assignment operator.
1135 bool hasMoveAssignment() const {
1136 return (data().DeclaredSpecialMembers & SMF_MoveAssignment) ||
1137 needsImplicitMoveAssignment();
1138 }
1139
1140 /// Set that we attempted to declare an implicit move assignment
1141 /// operator, but overload resolution failed so we deleted it.
1142 void setImplicitMoveAssignmentIsDeleted() {
1143 assert((data().DefaultedMoveAssignmentIsDeleted ||
1144 needsOverloadResolutionForMoveAssignment()) &&
1145 "move assignment should not be deleted");
1146 data().DefaultedMoveAssignmentIsDeleted = true;
1147 }
1148
1149 /// Determine whether this class should get an implicit move
1150 /// assignment operator or if any existing special member function inhibits
1151 /// this.
1152 bool needsImplicitMoveAssignment() const {
1153 return !(data().DeclaredSpecialMembers & SMF_MoveAssignment) &&
1154 !hasUserDeclaredCopyConstructor() &&
1155 !hasUserDeclaredCopyAssignment() &&
1156 !hasUserDeclaredMoveConstructor() &&
1157 !hasUserDeclaredDestructor() &&
1158 // C++1z [expr.prim.lambda]p21: "the closure type has a deleted copy
1159 // assignment operator". The intent is that this counts as a user
1160 // declared copy assignment, but we do not model it that way.
1161 !isLambda();
1162 }
1163
1164 /// Determine whether we need to eagerly declare a move assignment
1165 /// operator for this class.
1166 bool needsOverloadResolutionForMoveAssignment() const {
1167 return data().NeedOverloadResolutionForMoveAssignment;
1168 }
1169
1170 /// Determine whether this class has a user-declared destructor.
1171 ///
1172 /// When false, a destructor will be implicitly declared.
1173 bool hasUserDeclaredDestructor() const {
1174 return data().UserDeclaredSpecialMembers & SMF_Destructor;
1175 }
1176
1177 /// Determine whether this class needs an implicit destructor to
1178 /// be lazily declared.
1179 bool needsImplicitDestructor() const {
1180 return !(data().DeclaredSpecialMembers & SMF_Destructor);
1181 }
1182
1183 /// Determine whether we need to eagerly declare a destructor for this
1184 /// class.
1185 bool needsOverloadResolutionForDestructor() const {
1186 return data().NeedOverloadResolutionForDestructor;
1187 }
1188
1189 /// Determine whether this class describes a lambda function object.
1190 bool isLambda() const {
1191 // An update record can't turn a non-lambda into a lambda.
1192 auto *DD = DefinitionData;
1193 return DD && DD->IsLambda;
1194 }
1195
1196 /// Determine whether this class describes a generic
1197 /// lambda function object (i.e. function call operator is
1198 /// a template).
1199 bool isGenericLambda() const;
1200
1201 /// Retrieve the lambda call operator of the closure type
1202 /// if this is a closure type.
1203 CXXMethodDecl *getLambdaCallOperator() const;
1204
1205 /// Retrieve the lambda static invoker, the address of which
1206 /// is returned by the conversion operator, and the body of which
1207 /// is forwarded to the lambda call operator.
1208 CXXMethodDecl *getLambdaStaticInvoker() const;
1209
1210 /// Retrieve the generic lambda's template parameter list.
1211 /// Returns null if the class does not represent a lambda or a generic
1212 /// lambda.
1213 TemplateParameterList *getGenericLambdaTemplateParameterList() const;
1214
1215 LambdaCaptureDefault getLambdaCaptureDefault() const {
1216 assert(isLambda());
1217 return static_cast<LambdaCaptureDefault>(getLambdaData().CaptureDefault);
1218 }
1219
1220 /// For a closure type, retrieve the mapping from captured
1221 /// variables and \c this to the non-static data members that store the
1222 /// values or references of the captures.
1223 ///
1224 /// \param Captures Will be populated with the mapping from captured
1225 /// variables to the corresponding fields.
1226 ///
1227 /// \param ThisCapture Will be set to the field declaration for the
1228 /// \c this capture.
1229 ///
1230 /// \note No entries will be added for init-captures, as they do not capture
1231 /// variables.
1232 void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
1233 FieldDecl *&ThisCapture) const;
1234
1235 using capture_const_iterator = const LambdaCapture *;
1236 using capture_const_range = llvm::iterator_range<capture_const_iterator>;
1237
1238 capture_const_range captures() const {
1239 return capture_const_range(captures_begin(), captures_end());
1240 }
1241
1242 capture_const_iterator captures_begin() const {
1243 return isLambda() ? getLambdaData().Captures : nullptr;
1244 }
1245
1246 capture_const_iterator captures_end() const {
1247 return isLambda() ? captures_begin() + getLambdaData().NumCaptures
1248 : nullptr;
1249 }
1250
1251 using conversion_iterator = UnresolvedSetIterator;
1252
1253 conversion_iterator conversion_begin() const {
1254 return data().Conversions.get(getASTContext()).begin();
1255 }
1256
1257 conversion_iterator conversion_end() const {
1258 return data().Conversions.get(getASTContext()).end();
1259 }
1260
1261 /// Removes a conversion function from this class. The conversion
1262 /// function must currently be a member of this class. Furthermore,
1263 /// this class must currently be in the process of being defined.
1264 void removeConversion(const NamedDecl *Old);
1265
1266 /// Get all conversion functions visible in current class,
1267 /// including conversion function templates.
1268 llvm::iterator_range<conversion_iterator> getVisibleConversionFunctions();
1269
1270 /// Determine whether this class is an aggregate (C++ [dcl.init.aggr]),
1271 /// which is a class with no user-declared constructors, no private
1272 /// or protected non-static data members, no base classes, and no virtual
1273 /// functions (C++ [dcl.init.aggr]p1).
1274 bool isAggregate() const { return data().Aggregate; }
1275
1276 /// Whether this class has any in-class initializers
1277 /// for non-static data members (including those in anonymous unions or
1278 /// structs).
1279 bool hasInClassInitializer() const { return data().HasInClassInitializer; }
1280
1281 /// Whether this class or any of its subobjects has any members of
1282 /// reference type which would make value-initialization ill-formed.
1283 ///
1284 /// Per C++03 [dcl.init]p5:
1285 /// - if T is a non-union class type without a user-declared constructor,
1286 /// then every non-static data member and base-class component of T is
1287 /// value-initialized [...] A program that calls for [...]
1288 /// value-initialization of an entity of reference type is ill-formed.
1289 bool hasUninitializedReferenceMember() const {
1290 return !isUnion() && !hasUserDeclaredConstructor() &&
1291 data().HasUninitializedReferenceMember;
1292 }
1293
1294 /// Whether this class is a POD-type (C++ [class]p4)
1295 ///
1296 /// For purposes of this function a class is POD if it is an aggregate
1297 /// that has no non-static non-POD data members, no reference data
1298 /// members, no user-defined copy assignment operator and no
1299 /// user-defined destructor.
1300 ///
1301 /// Note that this is the C++ TR1 definition of POD.
1302 bool isPOD() const { return data().PlainOldData; }
1303
1304 /// True if this class is C-like, without C++-specific features, e.g.
1305 /// it contains only public fields, no bases, tag kind is not 'class', etc.
1306 bool isCLike() const;
1307
1308 /// Determine whether this is an empty class in the sense of
1309 /// (C++11 [meta.unary.prop]).
1310 ///
1311 /// The CXXRecordDecl is a class type, but not a union type,
1312 /// with no non-static data members other than bit-fields of length 0,
1313 /// no virtual member functions, no virtual base classes,
1314 /// and no base class B for which is_empty<B>::value is false.
1315 ///
1316 /// \note This does NOT include a check for union-ness.
1317 bool isEmpty() const { return data().Empty; }
1318
1319 /// Determine whether this class has direct non-static data members.
1320 bool hasDirectFields() const {
1321 auto &D = data();
1322 return D.HasPublicFields || D.HasProtectedFields || D.HasPrivateFields;
1323 }
1324
1325 /// Whether this class is polymorphic (C++ [class.virtual]),
1326 /// which means that the class contains or inherits a virtual function.
1327 bool isPolymorphic() const { return data().Polymorphic; }
1328
1329 /// Determine whether this class has a pure virtual function.
1330 ///
1331 /// The class is is abstract per (C++ [class.abstract]p2) if it declares
1332 /// a pure virtual function or inherits a pure virtual function that is
1333 /// not overridden.
1334 bool isAbstract() const { return data().Abstract; }
1335
1336 /// Determine whether this class is standard-layout per
1337 /// C++ [class]p7.
1338 bool isStandardLayout() const { return data().IsStandardLayout; }
1339
1340 /// Determine whether this class was standard-layout per
1341 /// C++11 [class]p7, specifically using the C++11 rules without any DRs.
1342 bool isCXX11StandardLayout() const { return data().IsCXX11StandardLayout; }
1343
1344 /// Determine whether this class, or any of its class subobjects,
1345 /// contains a mutable field.
1346 bool hasMutableFields() const { return data().HasMutableFields; }
1347
1348 /// Determine whether this class has any variant members.
1349 bool hasVariantMembers() const { return data().HasVariantMembers; }
1350
1351 /// Determine whether this class has a trivial default constructor
1352 /// (C++11 [class.ctor]p5).
1353 bool hasTrivialDefaultConstructor() const {
1354 return hasDefaultConstructor() &&
1355 (data().HasTrivialSpecialMembers & SMF_DefaultConstructor);
1356 }
1357
1358 /// Determine whether this class has a non-trivial default constructor
1359 /// (C++11 [class.ctor]p5).
1360 bool hasNonTrivialDefaultConstructor() const {
1361 return (data().DeclaredNonTrivialSpecialMembers & SMF_DefaultConstructor) ||
1362 (needsImplicitDefaultConstructor() &&
1363 !(data().HasTrivialSpecialMembers & SMF_DefaultConstructor));
1364 }
1365
1366 /// Determine whether this class has at least one constexpr constructor
1367 /// other than the copy or move constructors.
1368 bool hasConstexprNonCopyMoveConstructor() const {
1369 return data().HasConstexprNonCopyMoveConstructor ||
1370 (needsImplicitDefaultConstructor() &&
1371 defaultedDefaultConstructorIsConstexpr());
1372 }
1373
1374 /// Determine whether a defaulted default constructor for this class
1375 /// would be constexpr.
1376 bool defaultedDefaultConstructorIsConstexpr() const {
1377 return data().DefaultedDefaultConstructorIsConstexpr &&
1378 (!isUnion() || hasInClassInitializer() || !hasVariantMembers());
1379 }
1380
1381 /// Determine whether this class has a constexpr default constructor.
1382 bool hasConstexprDefaultConstructor() const {
1383 return data().HasConstexprDefaultConstructor ||
1384 (needsImplicitDefaultConstructor() &&
1385 defaultedDefaultConstructorIsConstexpr());
1386 }
1387
1388 /// Determine whether this class has a trivial copy constructor
1389 /// (C++ [class.copy]p6, C++11 [class.copy]p12)
1390 bool hasTrivialCopyConstructor() const {
1391 return data().HasTrivialSpecialMembers & SMF_CopyConstructor;
1392 }
1393
1394 bool hasTrivialCopyConstructorForCall() const {
1395 return data().HasTrivialSpecialMembersForCall & SMF_CopyConstructor;
1396 }
1397
1398 /// Determine whether this class has a non-trivial copy constructor
1399 /// (C++ [class.copy]p6, C++11 [class.copy]p12)
1400 bool hasNonTrivialCopyConstructor() const {
1401 return data().DeclaredNonTrivialSpecialMembers & SMF_CopyConstructor ||
1402 !hasTrivialCopyConstructor();
1403 }
1404
1405 bool hasNonTrivialCopyConstructorForCall() const {
1406 return (data().DeclaredNonTrivialSpecialMembersForCall &
1407 SMF_CopyConstructor) ||
1408 !hasTrivialCopyConstructorForCall();
1409 }
1410
1411 /// Determine whether this class has a trivial move constructor
1412 /// (C++11 [class.copy]p12)
1413 bool hasTrivialMoveConstructor() const {
1414 return hasMoveConstructor() &&
1415 (data().HasTrivialSpecialMembers & SMF_MoveConstructor);
1416 }
1417
1418 bool hasTrivialMoveConstructorForCall() const {
1419 return hasMoveConstructor() &&
1420 (data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor);
1421 }
1422
1423 /// Determine whether this class has a non-trivial move constructor
1424 /// (C++11 [class.copy]p12)
1425 bool hasNonTrivialMoveConstructor() const {
1426 return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveConstructor) ||
1427 (needsImplicitMoveConstructor() &&
1428 !(data().HasTrivialSpecialMembers & SMF_MoveConstructor));
1429 }
1430
1431 bool hasNonTrivialMoveConstructorForCall() const {
1432 return (data().DeclaredNonTrivialSpecialMembersForCall &
1433 SMF_MoveConstructor) ||
1434 (needsImplicitMoveConstructor() &&
1435 !(data().HasTrivialSpecialMembersForCall & SMF_MoveConstructor));
1436 }
1437
1438 /// Determine whether this class has a trivial copy assignment operator
1439 /// (C++ [class.copy]p11, C++11 [class.copy]p25)
1440 bool hasTrivialCopyAssignment() const {
1441 return data().HasTrivialSpecialMembers & SMF_CopyAssignment;
1442 }
1443
1444 /// Determine whether this class has a non-trivial copy assignment
1445 /// operator (C++ [class.copy]p11, C++11 [class.copy]p25)
1446 bool hasNonTrivialCopyAssignment() const {
1447 return data().DeclaredNonTrivialSpecialMembers & SMF_CopyAssignment ||
1448 !hasTrivialCopyAssignment();
1449 }
1450
1451 /// Determine whether this class has a trivial move assignment operator
1452 /// (C++11 [class.copy]p25)
1453 bool hasTrivialMoveAssignment() const {
1454 return hasMoveAssignment() &&
1455 (data().HasTrivialSpecialMembers & SMF_MoveAssignment);
1456 }
1457
1458 /// Determine whether this class has a non-trivial move assignment
1459 /// operator (C++11 [class.copy]p25)
1460 bool hasNonTrivialMoveAssignment() const {
1461 return (data().DeclaredNonTrivialSpecialMembers & SMF_MoveAssignment) ||
1462 (needsImplicitMoveAssignment() &&
1463 !(data().HasTrivialSpecialMembers & SMF_MoveAssignment));
1464 }
1465
1466 /// Determine whether this class has a trivial destructor
1467 /// (C++ [class.dtor]p3)
1468 bool hasTrivialDestructor() const {
1469 return data().HasTrivialSpecialMembers & SMF_Destructor;
1470 }
1471
1472 bool hasTrivialDestructorForCall() const {
1473 return data().HasTrivialSpecialMembersForCall & SMF_Destructor;
1474 }
1475
1476 /// Determine whether this class has a non-trivial destructor
1477 /// (C++ [class.dtor]p3)
1478 bool hasNonTrivialDestructor() const {
1479 return !(data().HasTrivialSpecialMembers & SMF_Destructor);
1480 }
1481
1482 bool hasNonTrivialDestructorForCall() const {
1483 return !(data().HasTrivialSpecialMembersForCall & SMF_Destructor);
1484 }
1485
1486 void setHasTrivialSpecialMemberForCall() {
1487 data().HasTrivialSpecialMembersForCall =
1488 (SMF_CopyConstructor | SMF_MoveConstructor | SMF_Destructor);
1489 }
1490
1491 /// Determine whether declaring a const variable with this type is ok
1492 /// per core issue 253.
1493 bool allowConstDefaultInit() const {
1494 return !data().HasUninitializedFields ||
1495 !(data().HasDefaultedDefaultConstructor ||
1496 needsImplicitDefaultConstructor());
1497 }
1498
1499 /// Determine whether this class has a destructor which has no
1500 /// semantic effect.
1501 ///
1502 /// Any such destructor will be trivial, public, defaulted and not deleted,
1503 /// and will call only irrelevant destructors.
1504 bool hasIrrelevantDestructor() const {
1505 return data().HasIrrelevantDestructor;
1506 }
1507
1508 /// Determine whether this class has a non-literal or/ volatile type
1509 /// non-static data member or base class.
1510 bool hasNonLiteralTypeFieldsOrBases() const {
1511 return data().HasNonLiteralTypeFieldsOrBases;
1512 }
1513
1514 /// Determine whether this class has a using-declaration that names
1515 /// a user-declared base class constructor.
1516 bool hasInheritedConstructor() const {
1517 return data().HasInheritedConstructor;
1518 }
1519
1520 /// Determine whether this class has a using-declaration that names
1521 /// a base class assignment operator.
1522 bool hasInheritedAssignment() const {
1523 return data().HasInheritedAssignment;
1524 }
1525
1526 /// Determine whether this class is considered trivially copyable per
1527 /// (C++11 [class]p6).
1528 bool isTriviallyCopyable() const;
1529
1530 /// Determine whether this class is considered trivial.
1531 ///
1532 /// C++11 [class]p6:
1533 /// "A trivial class is a class that has a trivial default constructor and
1534 /// is trivially copiable."
1535 bool isTrivial() const {
1536 return isTriviallyCopyable() && hasTrivialDefaultConstructor();
1537 }
1538
1539 /// Determine whether this class is a literal type.
1540 ///
1541 /// C++11 [basic.types]p10:
1542 /// A class type that has all the following properties:
1543 /// - it has a trivial destructor
1544 /// - every constructor call and full-expression in the
1545 /// brace-or-equal-intializers for non-static data members (if any) is
1546 /// a constant expression.
1547 /// - it is an aggregate type or has at least one constexpr constructor
1548 /// or constructor template that is not a copy or move constructor, and
1549 /// - all of its non-static data members and base classes are of literal
1550 /// types
1551 ///
1552 /// We resolve DR1361 by ignoring the second bullet. We resolve DR1452 by
1553 /// treating types with trivial default constructors as literal types.
1554 ///
1555 /// Only in C++17 and beyond, are lambdas literal types.
1556 bool isLiteral() const {
1557 return hasTrivialDestructor() &&
1558 (!isLambda() || getASTContext().getLangOpts().CPlusPlus17) &&
1559 !hasNonLiteralTypeFieldsOrBases() &&
1560 (isAggregate() || isLambda() ||
1561 hasConstexprNonCopyMoveConstructor() ||
1562 hasTrivialDefaultConstructor());
1563 }
1564
1565 /// If this record is an instantiation of a member class,
1566 /// retrieves the member class from which it was instantiated.
1567 ///
1568 /// This routine will return non-null for (non-templated) member
1569 /// classes of class templates. For example, given:
1570 ///
1571 /// \code
1572 /// template<typename T>
1573 /// struct X {
1574 /// struct A { };
1575 /// };
1576 /// \endcode
1577 ///
1578 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl
1579 /// whose parent is the class template specialization X<int>. For
1580 /// this declaration, getInstantiatedFromMemberClass() will return
1581 /// the CXXRecordDecl X<T>::A. When a complete definition of
1582 /// X<int>::A is required, it will be instantiated from the
1583 /// declaration returned by getInstantiatedFromMemberClass().
1584 CXXRecordDecl *getInstantiatedFromMemberClass() const;
1585
1586 /// If this class is an instantiation of a member class of a
1587 /// class template specialization, retrieves the member specialization
1588 /// information.
1589 MemberSpecializationInfo *getMemberSpecializationInfo() const;
1590
1591 /// Specify that this record is an instantiation of the
1592 /// member class \p RD.
1593 void setInstantiationOfMemberClass(CXXRecordDecl *RD,
1594 TemplateSpecializationKind TSK);
1595
1596 /// Retrieves the class template that is described by this
1597 /// class declaration.
1598 ///
1599 /// Every class template is represented as a ClassTemplateDecl and a
1600 /// CXXRecordDecl. The former contains template properties (such as
1601 /// the template parameter lists) while the latter contains the
1602 /// actual description of the template's
1603 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the
1604 /// CXXRecordDecl that from a ClassTemplateDecl, while
1605 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from
1606 /// a CXXRecordDecl.
1607 ClassTemplateDecl *getDescribedClassTemplate() const;
1608
1609 void setDescribedClassTemplate(ClassTemplateDecl *Template);
1610
1611 /// Determine whether this particular class is a specialization or
1612 /// instantiation of a class template or member class of a class template,
1613 /// and how it was instantiated or specialized.
1614 TemplateSpecializationKind getTemplateSpecializationKind() const;
1615
1616 /// Set the kind of specialization or template instantiation this is.
1617 void setTemplateSpecializationKind(TemplateSpecializationKind TSK);
1618
1619 /// Retrieve the record declaration from which this record could be
1620 /// instantiated. Returns null if this class is not a template instantiation.
1621 const CXXRecordDecl *getTemplateInstantiationPattern() const;
1622
1623 CXXRecordDecl *getTemplateInstantiationPattern() {
1624 return const_cast<CXXRecordDecl *>(const_cast<const CXXRecordDecl *>(this)
1625 ->getTemplateInstantiationPattern());
1626 }
1627
1628 /// Returns the destructor decl for this class.
1629 CXXDestructorDecl *getDestructor() const;
1630
1631 /// Returns true if the class destructor, or any implicitly invoked
1632 /// destructors are marked noreturn.
1633 bool isAnyDestructorNoReturn() const;
1634
1635 /// If the class is a local class [class.local], returns
1636 /// the enclosing function declaration.
1637 const FunctionDecl *isLocalClass() const {
1638 if (const auto *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
1639 return RD->isLocalClass();
1640
1641 return dyn_cast<FunctionDecl>(getDeclContext());
1642 }
1643
1644 FunctionDecl *isLocalClass() {
1645 return const_cast<FunctionDecl*>(
1646 const_cast<const CXXRecordDecl*>(this)->isLocalClass());
1647 }
1648
1649 /// Determine whether this dependent class is a current instantiation,
1650 /// when viewed from within the given context.
1651 bool isCurrentInstantiation(const DeclContext *CurContext) const;
1652
1653 /// Determine whether this class is derived from the class \p Base.
1654 ///
1655 /// This routine only determines whether this class is derived from \p Base,
1656 /// but does not account for factors that may make a Derived -> Base class
1657 /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1658 /// base class subobjects.
1659 ///
1660 /// \param Base the base class we are searching for.
1661 ///
1662 /// \returns true if this class is derived from Base, false otherwise.
1663 bool isDerivedFrom(const CXXRecordDecl *Base) const;
1664
1665 /// Determine whether this class is derived from the type \p Base.
1666 ///
1667 /// This routine only determines whether this class is derived from \p Base,
1668 /// but does not account for factors that may make a Derived -> Base class
1669 /// ill-formed, such as private/protected inheritance or multiple, ambiguous
1670 /// base class subobjects.
1671 ///
1672 /// \param Base the base class we are searching for.
1673 ///
1674 /// \param Paths will contain the paths taken from the current class to the
1675 /// given \p Base class.
1676 ///
1677 /// \returns true if this class is derived from \p Base, false otherwise.
1678 ///
1679 /// \todo add a separate parameter to configure IsDerivedFrom, rather than
1680 /// tangling input and output in \p Paths
1681 bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
1682
1683 /// Determine whether this class is virtually derived from
1684 /// the class \p Base.
1685 ///
1686 /// This routine only determines whether this class is virtually
1687 /// derived from \p Base, but does not account for factors that may
1688 /// make a Derived -> Base class ill-formed, such as
1689 /// private/protected inheritance or multiple, ambiguous base class
1690 /// subobjects.
1691 ///
1692 /// \param Base the base class we are searching for.
1693 ///
1694 /// \returns true if this class is virtually derived from Base,
1695 /// false otherwise.
1696 bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const;
1697
1698 /// Determine whether this class is provably not derived from
1699 /// the type \p Base.
1700 bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const;
1701
1702 /// Function type used by forallBases() as a callback.
1703 ///
1704 /// \param BaseDefinition the definition of the base class
1705 ///
1706 /// \returns true if this base matched the search criteria
1707 using ForallBasesCallback =
1708 llvm::function_ref<bool(const CXXRecordDecl *BaseDefinition)>;
1709
1710 /// Determines if the given callback holds for all the direct
1711 /// or indirect base classes of this type.
1712 ///
1713 /// The class itself does not count as a base class. This routine
1714 /// returns false if the class has non-computable base classes.
1715 ///
1716 /// \param BaseMatches Callback invoked for each (direct or indirect) base
1717 /// class of this type, or if \p AllowShortCircuit is true then until a call
1718 /// returns false.
1719 ///
1720 /// \param AllowShortCircuit if false, forces the callback to be called
1721 /// for every base class, even if a dependent or non-matching base was
1722 /// found.
1723 bool forallBases(ForallBasesCallback BaseMatches,
1724 bool AllowShortCircuit = true) const;
1725
1726 /// Function type used by lookupInBases() to determine whether a
1727 /// specific base class subobject matches the lookup criteria.
1728 ///
1729 /// \param Specifier the base-class specifier that describes the inheritance
1730 /// from the base class we are trying to match.
1731 ///
1732 /// \param Path the current path, from the most-derived class down to the
1733 /// base named by the \p Specifier.
1734 ///
1735 /// \returns true if this base matched the search criteria, false otherwise.
1736 using BaseMatchesCallback =
1737 llvm::function_ref<bool(const CXXBaseSpecifier *Specifier,
1738 CXXBasePath &Path)>;
1739
1740 /// Look for entities within the base classes of this C++ class,
1741 /// transitively searching all base class subobjects.
1742 ///
1743 /// This routine uses the callback function \p BaseMatches to find base
1744 /// classes meeting some search criteria, walking all base class subobjects
1745 /// and populating the given \p Paths structure with the paths through the
1746 /// inheritance hierarchy that resulted in a match. On a successful search,
1747 /// the \p Paths structure can be queried to retrieve the matching paths and
1748 /// to determine if there were any ambiguities.
1749 ///
1750 /// \param BaseMatches callback function used to determine whether a given
1751 /// base matches the user-defined search criteria.
1752 ///
1753 /// \param Paths used to record the paths from this class to its base class
1754 /// subobjects that match the search criteria.
1755 ///
1756 /// \param LookupInDependent can be set to true to extend the search to
1757 /// dependent base classes.
1758 ///
1759 /// \returns true if there exists any path from this class to a base class
1760 /// subobject that matches the search criteria.
1761 bool lookupInBases(BaseMatchesCallback BaseMatches, CXXBasePaths &Paths,
1762 bool LookupInDependent = false) const;
1763
1764 /// Base-class lookup callback that determines whether the given
1765 /// base class specifier refers to a specific class declaration.
1766 ///
1767 /// This callback can be used with \c lookupInBases() to determine whether
1768 /// a given derived class has is a base class subobject of a particular type.
1769 /// The base record pointer should refer to the canonical CXXRecordDecl of the
1770 /// base class that we are searching for.
1771 static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
1772 CXXBasePath &Path, const CXXRecordDecl *BaseRecord);
1773
1774 /// Base-class lookup callback that determines whether the
1775 /// given base class specifier refers to a specific class
1776 /// declaration and describes virtual derivation.
1777 ///
1778 /// This callback can be used with \c lookupInBases() to determine
1779 /// whether a given derived class has is a virtual base class
1780 /// subobject of a particular type. The base record pointer should
1781 /// refer to the canonical CXXRecordDecl of the base class that we
1782 /// are searching for.
1783 static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
1784 CXXBasePath &Path,
1785 const CXXRecordDecl *BaseRecord);
1786
1787 /// Base-class lookup callback that determines whether there exists
1788 /// a tag with the given name.
1789 ///
1790 /// This callback can be used with \c lookupInBases() to find tag members
1791 /// of the given name within a C++ class hierarchy.
1792 static bool FindTagMember(const CXXBaseSpecifier *Specifier,
1793 CXXBasePath &Path, DeclarationName Name);
1794
1795 /// Base-class lookup callback that determines whether there exists
1796 /// a member with the given name.
1797 ///
1798 /// This callback can be used with \c lookupInBases() to find members
1799 /// of the given name within a C++ class hierarchy.
1800 static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
1801 CXXBasePath &Path, DeclarationName Name);
1802
1803 /// Base-class lookup callback that determines whether there exists
1804 /// a member with the given name.
1805 ///
1806 /// This callback can be used with \c lookupInBases() to find members
1807 /// of the given name within a C++ class hierarchy, including dependent
1808 /// classes.
1809 static bool
1810 FindOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier,
1811 CXXBasePath &Path, DeclarationName Name);
1812
1813 /// Base-class lookup callback that determines whether there exists
1814 /// an OpenMP declare reduction member with the given name.
1815 ///
1816 /// This callback can be used with \c lookupInBases() to find members
1817 /// of the given name within a C++ class hierarchy.
1818 static bool FindOMPReductionMember(const CXXBaseSpecifier *Specifier,
1819 CXXBasePath &Path, DeclarationName Name);
1820
1821 /// Base-class lookup callback that determines whether there exists
1822 /// a member with the given name that can be used in a nested-name-specifier.
1823 ///
1824 /// This callback can be used with \c lookupInBases() to find members of
1825 /// the given name within a C++ class hierarchy that can occur within
1826 /// nested-name-specifiers.
1827 static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
1828 CXXBasePath &Path,
1829 DeclarationName Name);
1830
1831 /// Retrieve the final overriders for each virtual member
1832 /// function in the class hierarchy where this class is the
1833 /// most-derived class in the class hierarchy.
1834 void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const;
1835
1836 /// Get the indirect primary bases for this class.
1837 void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const;
1838
1839 /// Performs an imprecise lookup of a dependent name in this class.
1840 ///
1841 /// This function does not follow strict semantic rules and should be used
1842 /// only when lookup rules can be relaxed, e.g. indexing.
1843 std::vector<const NamedDecl *>
1844 lookupDependentName(const DeclarationName &Name,
1845 llvm::function_ref<bool(const NamedDecl *ND)> Filter);
1846
1847 /// Renders and displays an inheritance diagram
1848 /// for this C++ class and all of its base classes (transitively) using
1849 /// GraphViz.
1850 void viewInheritance(ASTContext& Context) const;
1851
1852 /// Calculates the access of a decl that is reached
1853 /// along a path.
1854 static AccessSpecifier MergeAccess(AccessSpecifier PathAccess,
1855 AccessSpecifier DeclAccess) {
1856 assert(DeclAccess != AS_none);
1857 if (DeclAccess == AS_private) return AS_none;
1858 return (PathAccess > DeclAccess ? PathAccess : DeclAccess);
1859 }
1860
1861 /// Indicates that the declaration of a defaulted or deleted special
1862 /// member function is now complete.
1863 void finishedDefaultedOrDeletedMember(CXXMethodDecl *MD);
1864
1865 void setTrivialForCallFlags(CXXMethodDecl *MD);
1866
1867 /// Indicates that the definition of this class is now complete.
1868 void completeDefinition() override;
1869
1870 /// Indicates that the definition of this class is now complete,
1871 /// and provides a final overrider map to help determine
1872 ///
1873 /// \param FinalOverriders The final overrider map for this class, which can
1874 /// be provided as an optimization for abstract-class checking. If NULL,
1875 /// final overriders will be computed if they are needed to complete the
1876 /// definition.
1877 void completeDefinition(CXXFinalOverriderMap *FinalOverriders);
1878
1879 /// Determine whether this class may end up being abstract, even though
1880 /// it is not yet known to be abstract.
1881 ///
1882 /// \returns true if this class is not known to be abstract but has any
1883 /// base classes that are abstract. In this case, \c completeDefinition()
1884 /// will need to compute final overriders to determine whether the class is
1885 /// actually abstract.
1886 bool mayBeAbstract() const;
1887
1888 /// If this is the closure type of a lambda expression, retrieve the
1889 /// number to be used for name mangling in the Itanium C++ ABI.
1890 ///
1891 /// Zero indicates that this closure type has internal linkage, so the
1892 /// mangling number does not matter, while a non-zero value indicates which
1893 /// lambda expression this is in this particular context.
1894 unsigned getLambdaManglingNumber() const {
1895 assert(isLambda() && "Not a lambda closure type!");
1896 return getLambdaData().ManglingNumber;
1897 }
1898
1899 /// Retrieve the declaration that provides additional context for a
1900 /// lambda, when the normal declaration context is not specific enough.
1901 ///
1902 /// Certain contexts (default arguments of in-class function parameters and
1903 /// the initializers of data members) have separate name mangling rules for
1904 /// lambdas within the Itanium C++ ABI. For these cases, this routine provides
1905 /// the declaration in which the lambda occurs, e.g., the function parameter
1906 /// or the non-static data member. Otherwise, it returns NULL to imply that
1907 /// the declaration context suffices.
1908 Decl *getLambdaContextDecl() const;
1909
1910 /// Set the mangling number and context declaration for a lambda
1911 /// class.
1912 void setLambdaMangling(unsigned ManglingNumber, Decl *ContextDecl) {
1913 getLambdaData().ManglingNumber = ManglingNumber;
1914 getLambdaData().ContextDecl = ContextDecl;
1915 }
1916
1917 /// Returns the inheritance model used for this record.
1918 MSInheritanceAttr::Spelling getMSInheritanceModel() const;
1919
1920 /// Calculate what the inheritance model would be for this class.
1921 MSInheritanceAttr::Spelling calculateInheritanceModel() const;
1922
1923 /// In the Microsoft C++ ABI, use zero for the field offset of a null data
1924 /// member pointer if we can guarantee that zero is not a valid field offset,
1925 /// or if the member pointer has multiple fields. Polymorphic classes have a
1926 /// vfptr at offset zero, so we can use zero for null. If there are multiple
1927 /// fields, we can use zero even if it is a valid field offset because
1928 /// null-ness testing will check the other fields.
1929 bool nullFieldOffsetIsZero() const {
1930 return !MSInheritanceAttr::hasOnlyOneField(/*IsMemberFunction=*/false,
1931 getMSInheritanceModel()) ||
1932 (hasDefinition() && isPolymorphic());
1933 }
1934
1935 /// Controls when vtordisps will be emitted if this record is used as a
1936 /// virtual base.
1937 MSVtorDispAttr::Mode getMSVtorDispMode() const;
1938
1939 /// Determine whether this lambda expression was known to be dependent
1940 /// at the time it was created, even if its context does not appear to be
1941 /// dependent.
1942 ///
1943 /// This flag is a workaround for an issue with parsing, where default
1944 /// arguments are parsed before their enclosing function declarations have
1945 /// been created. This means that any lambda expressions within those
1946 /// default arguments will have as their DeclContext the context enclosing
1947 /// the function declaration, which may be non-dependent even when the
1948 /// function declaration itself is dependent. This flag indicates when we
1949 /// know that the lambda is dependent despite that.
1950 bool isDependentLambda() const {
1951 return isLambda() && getLambdaData().Dependent;
1952 }
1953
1954 TypeSourceInfo *getLambdaTypeInfo() const {
1955 return getLambdaData().MethodTyInfo;
1956 }
1957
1958 // Determine whether this type is an Interface Like type for
1959 // __interface inheritance purposes.
1960 bool isInterfaceLike() const;
1961
1962 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1963 static bool classofKind(Kind K) {
1964 return K >= firstCXXRecord && K <= lastCXXRecord;
1965 }
1966};
1967
1968/// Represents a C++ deduction guide declaration.
1969///
1970/// \code
1971/// template<typename T> struct A { A(); A(T); };
1972/// A() -> A<int>;
1973/// \endcode
1974///
1975/// In this example, there will be an explicit deduction guide from the
1976/// second line, and implicit deduction guide templates synthesized from
1977/// the constructors of \c A.
1978class CXXDeductionGuideDecl : public FunctionDecl {
1979 void anchor() override;
1980
1981private:
1982 CXXDeductionGuideDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1983 bool IsExplicit, const DeclarationNameInfo &NameInfo,
1984 QualType T, TypeSourceInfo *TInfo,
1985 SourceLocation EndLocation)
1986 : FunctionDecl(CXXDeductionGuide, C, DC, StartLoc, NameInfo, T, TInfo,
1987 SC_None, false, false) {
1988 if (EndLocation.isValid())
1989 setRangeEnd(EndLocation);
1990 IsExplicitSpecified = IsExplicit;
1991 }
1992
1993public:
1994 friend class ASTDeclReader;
1995 friend class ASTDeclWriter;
1996
1997 static CXXDeductionGuideDecl *Create(ASTContext &C, DeclContext *DC,
1998 SourceLocation StartLoc, bool IsExplicit,
1999 const DeclarationNameInfo &NameInfo,
2000 QualType T, TypeSourceInfo *TInfo,
2001 SourceLocation EndLocation);
2002
2003 static CXXDeductionGuideDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2004
2005 /// Whether this deduction guide is explicit.
2006 bool isExplicit() const { return IsExplicitSpecified; }
2007
2008 /// Whether this deduction guide was declared with the 'explicit' specifier.
2009 bool isExplicitSpecified() const { return IsExplicitSpecified; }
2010
2011 /// Get the template for which this guide performs deduction.
2012 TemplateDecl *getDeducedTemplate() const {
2013 return getDeclName().getCXXDeductionGuideTemplate();
2014 }
2015
2016 void setIsCopyDeductionCandidate() {
2017 IsCopyDeductionCandidate = true;
2018 }
2019
2020 bool isCopyDeductionCandidate() const { return IsCopyDeductionCandidate; }
2021
2022 // Implement isa/cast/dyncast/etc.
2023 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2024 static bool classofKind(Kind K) { return K == CXXDeductionGuide; }
2025};
2026
2027/// Represents a static or instance method of a struct/union/class.
2028///
2029/// In the terminology of the C++ Standard, these are the (static and
2030/// non-static) member functions, whether virtual or not.
2031class CXXMethodDecl : public FunctionDecl {
2032 void anchor() override;
2033
2034protected:
2035 CXXMethodDecl(Kind DK, ASTContext &C, CXXRecordDecl *RD,
2036 SourceLocation StartLoc, const DeclarationNameInfo &NameInfo,
2037 QualType T, TypeSourceInfo *TInfo,
2038 StorageClass SC, bool isInline,
2039 bool isConstexpr, SourceLocation EndLocation)
2040 : FunctionDecl(DK, C, RD, StartLoc, NameInfo, T, TInfo,
2041 SC, isInline, isConstexpr) {
2042 if (EndLocation.isValid())
2043 setRangeEnd(EndLocation);
2044 }
2045
2046public:
2047 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2048 SourceLocation StartLoc,
2049 const DeclarationNameInfo &NameInfo,
2050 QualType T, TypeSourceInfo *TInfo,
2051 StorageClass SC,
2052 bool isInline,
2053 bool isConstexpr,
2054 SourceLocation EndLocation);
2055
2056 static CXXMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2057
2058 bool isStatic() const;
2059 bool isInstance() const { return !isStatic(); }
2060
2061 /// Returns true if the given operator is implicitly static in a record
2062 /// context.
2063 static bool isStaticOverloadedOperator(OverloadedOperatorKind OOK) {
2064 // [class.free]p1:
2065 // Any allocation function for a class T is a static member
2066 // (even if not explicitly declared static).
2067 // [class.free]p6 Any deallocation function for a class X is a static member
2068 // (even if not explicitly declared static).
2069 return OOK == OO_New || OOK == OO_Array_New || OOK == OO_Delete ||
2070 OOK == OO_Array_Delete;
2071 }
2072
2073 bool isConst() const { return getType()->castAs<FunctionType>()->isConst(); }
2074 bool isVolatile() const { return getType()->castAs<FunctionType>()->isVolatile(); }
2075
2076 bool isVirtual() const {
2077 CXXMethodDecl *CD = const_cast<CXXMethodDecl*>(this)->getCanonicalDecl();
2078
2079 // Member function is virtual if it is marked explicitly so, or if it is
2080 // declared in __interface -- then it is automatically pure virtual.
2081 if (CD->isVirtualAsWritten() || CD->isPure())
2082 return true;
2083
2084 return CD->size_overridden_methods() != 0;
2085 }
2086
2087 /// If it's possible to devirtualize a call to this method, return the called
2088 /// function. Otherwise, return null.
2089
2090 /// \param Base The object on which this virtual function is called.
2091 /// \param IsAppleKext True if we are compiling for Apple kext.
2092 CXXMethodDecl *getDevirtualizedMethod(const Expr *Base, bool IsAppleKext);
2093
2094 const CXXMethodDecl *getDevirtualizedMethod(const Expr *Base,
2095 bool IsAppleKext) const {
2096 return const_cast<CXXMethodDecl *>(this)->getDevirtualizedMethod(
2097 Base, IsAppleKext);
2098 }
2099
2100 /// Determine whether this is a usual deallocation function
2101 /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded
2102 /// delete or delete[] operator with a particular signature.
2103 bool isUsualDeallocationFunction() const;
2104
2105 /// Determine whether this is a copy-assignment operator, regardless
2106 /// of whether it was declared implicitly or explicitly.
2107 bool isCopyAssignmentOperator() const;
2108
2109 /// Determine whether this is a move assignment operator.
2110 bool isMoveAssignmentOperator() const;
2111
2112 CXXMethodDecl *getCanonicalDecl() override {
2113 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl());
2114 }
2115 const CXXMethodDecl *getCanonicalDecl() const {
2116 return const_cast<CXXMethodDecl*>(this)->getCanonicalDecl();
2117 }
2118
2119 CXXMethodDecl *getMostRecentDecl() {
2120 return cast<CXXMethodDecl>(
2121 static_cast<FunctionDecl *>(this)->getMostRecentDecl());
2122 }
2123 const CXXMethodDecl *getMostRecentDecl() const {
2124 return const_cast<CXXMethodDecl*>(this)->getMostRecentDecl();
2125 }
2126
2127 /// True if this method is user-declared and was not
2128 /// deleted or defaulted on its first declaration.
2129 bool isUserProvided() const {
2130 auto *DeclAsWritten = this;
2131 if (auto *Pattern = getTemplateInstantiationPattern())
2132 DeclAsWritten = cast<CXXMethodDecl>(Pattern);
2133 return !(DeclAsWritten->isDeleted() ||
2134 DeclAsWritten->getCanonicalDecl()->isDefaulted());
2135 }
2136
2137 void addOverriddenMethod(const CXXMethodDecl *MD);
2138
2139 using method_iterator = const CXXMethodDecl *const *;
2140
2141 method_iterator begin_overridden_methods() const;
2142 method_iterator end_overridden_methods() const;
2143 unsigned size_overridden_methods() const;
2144
2145 using overridden_method_range= ASTContext::overridden_method_range;
2146
2147 overridden_method_range overridden_methods() const;
2148
2149 /// Returns the parent of this method declaration, which
2150 /// is the class in which this method is defined.
2151 const CXXRecordDecl *getParent() const {
2152 return cast<CXXRecordDecl>(FunctionDecl::getParent());
2153 }
2154
2155 /// Returns the parent of this method declaration, which
2156 /// is the class in which this method is defined.
2157 CXXRecordDecl *getParent() {
2158 return const_cast<CXXRecordDecl *>(
2159 cast<CXXRecordDecl>(FunctionDecl::getParent()));
2160 }
2161
2162 /// Returns the type of the \c this pointer.
2163 ///
2164 /// Should only be called for instance (i.e., non-static) methods. Note
2165 /// that for the call operator of a lambda closure type, this returns the
2166 /// desugared 'this' type (a pointer to the closure type), not the captured
2167 /// 'this' type.
2168 QualType getThisType(ASTContext &C) const;
2169
2170 unsigned getTypeQualifiers() const {
2171 return getType()->getAs<FunctionProtoType>()->getTypeQuals();
2172 }
2173
2174 /// Retrieve the ref-qualifier associated with this method.
2175 ///
2176 /// In the following example, \c f() has an lvalue ref-qualifier, \c g()
2177 /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier.
2178 /// @code
2179 /// struct X {
2180 /// void f() &;
2181 /// void g() &&;
2182 /// void h();
2183 /// };
2184 /// @endcode
2185 RefQualifierKind getRefQualifier() const {
2186 return getType()->getAs<FunctionProtoType>()->getRefQualifier();
2187 }
2188
2189 bool hasInlineBody() const;
2190
2191 /// Determine whether this is a lambda closure type's static member
2192 /// function that is used for the result of the lambda's conversion to
2193 /// function pointer (for a lambda with no captures).
2194 ///
2195 /// The function itself, if used, will have a placeholder body that will be
2196 /// supplied by IR generation to either forward to the function call operator
2197 /// or clone the function call operator.
2198 bool isLambdaStaticInvoker() const;
2199
2200 /// Find the method in \p RD that corresponds to this one.
2201 ///
2202 /// Find if \p RD or one of the classes it inherits from override this method.
2203 /// If so, return it. \p RD is assumed to be a subclass of the class defining
2204 /// this method (or be the class itself), unless \p MayBeBase is set to true.
2205 CXXMethodDecl *
2206 getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2207 bool MayBeBase = false);
2208
2209 const CXXMethodDecl *
2210 getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2211 bool MayBeBase = false) const {
2212 return const_cast<CXXMethodDecl *>(this)
2213 ->getCorrespondingMethodInClass(RD, MayBeBase);
2214 }
2215
2216 // Implement isa/cast/dyncast/etc.
2217 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2218 static bool classofKind(Kind K) {
2219 return K >= firstCXXMethod && K <= lastCXXMethod;
2220 }
2221};
2222
2223/// Represents a C++ base or member initializer.
2224///
2225/// This is part of a constructor initializer that
2226/// initializes one non-static member variable or one base class. For
2227/// example, in the following, both 'A(a)' and 'f(3.14159)' are member
2228/// initializers:
2229///
2230/// \code
2231/// class A { };
2232/// class B : public A {
2233/// float f;
2234/// public:
2235/// B(A& a) : A(a), f(3.14159) { }
2236/// };
2237/// \endcode
2238class CXXCtorInitializer final {
2239 /// Either the base class name/delegating constructor type (stored as
2240 /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
2241 /// (IndirectFieldDecl*) being initialized.
2242 llvm::PointerUnion3<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *>
2243 Initializee;
2244
2245 /// The source location for the field name or, for a base initializer
2246 /// pack expansion, the location of the ellipsis.
2247 ///
2248 /// In the case of a delegating
2249 /// constructor, it will still include the type's source location as the
2250 /// Initializee points to the CXXConstructorDecl (to allow loop detection).
2251 SourceLocation MemberOrEllipsisLocation;
2252
2253 /// The argument used to initialize the base or member, which may
2254 /// end up constructing an object (when multiple arguments are involved).
2255 Stmt *Init;
2256
2257 /// Location of the left paren of the ctor-initializer.
2258 SourceLocation LParenLoc;
2259
2260 /// Location of the right paren of the ctor-initializer.
2261 SourceLocation RParenLoc;
2262
2263 /// If the initializee is a type, whether that type makes this
2264 /// a delegating initialization.
2265 unsigned IsDelegating : 1;
2266
2267 /// If the initializer is a base initializer, this keeps track
2268 /// of whether the base is virtual or not.
2269 unsigned IsVirtual : 1;
2270
2271 /// Whether or not the initializer is explicitly written
2272 /// in the sources.
2273 unsigned IsWritten : 1;
2274
2275 /// If IsWritten is true, then this number keeps track of the textual order
2276 /// of this initializer in the original sources, counting from 0.
2277 unsigned SourceOrder : 13;
2278
2279public:
2280 /// Creates a new base-class initializer.
2281 explicit
2282 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual,
2283 SourceLocation L, Expr *Init, SourceLocation R,
2284 SourceLocation EllipsisLoc);
2285
2286 /// Creates a new member initializer.
2287 explicit
2288 CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
2289 SourceLocation MemberLoc, SourceLocation L, Expr *Init,
2290 SourceLocation R);
2291
2292 /// Creates a new anonymous field initializer.
2293 explicit
2294 CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member,
2295 SourceLocation MemberLoc, SourceLocation L, Expr *Init,
2296 SourceLocation R);
2297
2298 /// Creates a new delegating initializer.
2299 explicit
2300 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo,
2301 SourceLocation L, Expr *Init, SourceLocation R);
2302
2303 /// Determine whether this initializer is initializing a base class.
2304 bool isBaseInitializer() const {
2305 return Initializee.is<TypeSourceInfo*>() && !IsDelegating;
2306 }
2307
2308 /// Determine whether this initializer is initializing a non-static
2309 /// data member.
2310 bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); }
2311
2312 bool isAnyMemberInitializer() const {
2313 return isMemberInitializer() || isIndirectMemberInitializer();
2314 }
2315
2316 bool isIndirectMemberInitializer() const {
2317 return Initializee.is<IndirectFieldDecl*>();
2318 }
2319
2320 /// Determine whether this initializer is an implicit initializer
2321 /// generated for a field with an initializer defined on the member
2322 /// declaration.
2323 ///
2324 /// In-class member initializers (also known as "non-static data member
2325 /// initializations", NSDMIs) were introduced in C++11.
2326 bool isInClassMemberInitializer() const {
2327 return Init->getStmtClass() == Stmt::CXXDefaultInitExprClass;
2328 }
2329
2330 /// Determine whether this initializer is creating a delegating
2331 /// constructor.
2332 bool isDelegatingInitializer() const {
2333 return Initializee.is<TypeSourceInfo*>() && IsDelegating;
2334 }
2335
2336 /// Determine whether this initializer is a pack expansion.
2337 bool isPackExpansion() const {
2338 return isBaseInitializer() && MemberOrEllipsisLocation.isValid();
2339 }
2340
2341 // For a pack expansion, returns the location of the ellipsis.
2342 SourceLocation getEllipsisLoc() const {
2343 assert(isPackExpansion() && "Initializer is not a pack expansion");
2344 return MemberOrEllipsisLocation;
2345 }
2346
2347 /// If this is a base class initializer, returns the type of the
2348 /// base class with location information. Otherwise, returns an NULL
2349 /// type location.
2350 TypeLoc getBaseClassLoc() const;
2351
2352 /// If this is a base class initializer, returns the type of the base class.
2353 /// Otherwise, returns null.
2354 const Type *getBaseClass() const;
2355
2356 /// Returns whether the base is virtual or not.
2357 bool isBaseVirtual() const {
2358 assert(isBaseInitializer() && "Must call this on base initializer!");
2359
2360 return IsVirtual;
2361 }
2362
2363 /// Returns the declarator information for a base class or delegating
2364 /// initializer.
2365 TypeSourceInfo *getTypeSourceInfo() const {
2366 return Initializee.dyn_cast<TypeSourceInfo *>();
2367 }
2368
2369 /// If this is a member initializer, returns the declaration of the
2370 /// non-static data member being initialized. Otherwise, returns null.
2371 FieldDecl *getMember() const {
2372 if (isMemberInitializer())
2373 return Initializee.get<FieldDecl*>();
2374 return nullptr;
2375 }
2376
2377 FieldDecl *getAnyMember() const {
2378 if (isMemberInitializer())
2379 return Initializee.get<FieldDecl*>();
2380 if (isIndirectMemberInitializer())
2381 return Initializee.get<IndirectFieldDecl*>()->getAnonField();
2382 return nullptr;
2383 }
2384
2385 IndirectFieldDecl *getIndirectMember() const {
2386 if (isIndirectMemberInitializer())
2387 return Initializee.get<IndirectFieldDecl*>();
2388 return nullptr;
2389 }
2390
2391 SourceLocation getMemberLocation() const {
2392 return MemberOrEllipsisLocation;
2393 }
2394
2395 /// Determine the source location of the initializer.
2396 SourceLocation getSourceLocation() const;
2397
2398 /// Determine the source range covering the entire initializer.
2399 SourceRange getSourceRange() const LLVM_READONLY;
2400
2401 /// Determine whether this initializer is explicitly written
2402 /// in the source code.
2403 bool isWritten() const { return IsWritten; }
2404
2405 /// Return the source position of the initializer, counting from 0.
2406 /// If the initializer was implicit, -1 is returned.
2407 int getSourceOrder() const {
2408 return IsWritten ? static_cast<int>(SourceOrder) : -1;
2409 }
2410
2411 /// Set the source order of this initializer.
2412 ///
2413 /// This can only be called once for each initializer; it cannot be called
2414 /// on an initializer having a positive number of (implicit) array indices.
2415 ///
2416 /// This assumes that the initializer was written in the source code, and
2417 /// ensures that isWritten() returns true.
2418 void setSourceOrder(int Pos) {
2419 assert(!IsWritten &&
2420 "setSourceOrder() used on implicit initializer");
2421 assert(SourceOrder == 0 &&
2422 "calling twice setSourceOrder() on the same initializer");
2423 assert(Pos >= 0 &&
2424 "setSourceOrder() used to make an initializer implicit");
2425 IsWritten = true;
2426 SourceOrder = static_cast<unsigned>(Pos);
2427 }
2428
2429 SourceLocation getLParenLoc() const { return LParenLoc; }
2430 SourceLocation getRParenLoc() const { return RParenLoc; }
2431
2432 /// Get the initializer.
2433 Expr *getInit() const { return static_cast<Expr *>(Init); }
2434};
2435
2436/// Description of a constructor that was inherited from a base class.
2437class InheritedConstructor {
2438 ConstructorUsingShadowDecl *Shadow = nullptr;
2439 CXXConstructorDecl *BaseCtor = nullptr;
2440
2441public:
2442 InheritedConstructor() = default;
2443 InheritedConstructor(ConstructorUsingShadowDecl *Shadow,
2444 CXXConstructorDecl *BaseCtor)
2445 : Shadow(Shadow), BaseCtor(BaseCtor) {}
2446
2447 explicit operator bool() const { return Shadow; }
2448
2449 ConstructorUsingShadowDecl *getShadowDecl() const { return Shadow; }
2450 CXXConstructorDecl *getConstructor() const { return BaseCtor; }
2451};
2452
2453/// Represents a C++ constructor within a class.
2454///
2455/// For example:
2456///
2457/// \code
2458/// class X {
2459/// public:
2460/// explicit X(int); // represented by a CXXConstructorDecl.
2461/// };
2462/// \endcode
2463class CXXConstructorDecl final
2464 : public CXXMethodDecl,
2465 private llvm::TrailingObjects<CXXConstructorDecl, InheritedConstructor> {
2466 /// \name Support for base and member initializers.
2467 /// \{
2468 /// The arguments used to initialize the base or member.
2469 LazyCXXCtorInitializersPtr CtorInitializers;
2470 unsigned NumCtorInitializers : 31;
2471 /// \}
2472
2473 /// Whether this constructor declaration is an implicitly-declared
2474 /// inheriting constructor.
2475 unsigned IsInheritingConstructor : 1;
2476
2477 CXXConstructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2478 const DeclarationNameInfo &NameInfo,
2479 QualType T, TypeSourceInfo *TInfo,
2480 bool isExplicitSpecified, bool isInline,
2481 bool isImplicitlyDeclared, bool isConstexpr,
2482 InheritedConstructor Inherited)
2483 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2484 SC_None, isInline, isConstexpr, SourceLocation()),
2485 NumCtorInitializers(0), IsInheritingConstructor((bool)Inherited) {
2486 setImplicit(isImplicitlyDeclared);
2487 if (Inherited)
2488 *getTrailingObjects<InheritedConstructor>() = Inherited;
2489 IsExplicitSpecified = isExplicitSpecified;
2490 }
2491
2492 void anchor() override;
2493
2494public:
2495 friend class ASTDeclReader;
2496 friend class ASTDeclWriter;
2497 friend TrailingObjects;
2498
2499 static CXXConstructorDecl *CreateDeserialized(ASTContext &C, unsigned ID,
2500 bool InheritsConstructor);
2501 static CXXConstructorDecl *
2502 Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2503 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2504 bool isExplicit, bool isInline, bool isImplicitlyDeclared,
2505 bool isConstexpr,
2506 InheritedConstructor Inherited = InheritedConstructor());
2507
2508 /// Iterates through the member/base initializer list.
2509 using init_iterator = CXXCtorInitializer **;
2510
2511 /// Iterates through the member/base initializer list.
2512 using init_const_iterator = CXXCtorInitializer *const *;
2513
2514 using init_range = llvm::iterator_range<init_iterator>;
2515 using init_const_range = llvm::iterator_range<init_const_iterator>;
2516
2517 init_range inits() { return init_range(init_begin(), init_end()); }
2518 init_const_range inits() const {
2519 return init_const_range(init_begin(), init_end());
2520 }
2521
2522 /// Retrieve an iterator to the first initializer.
2523 init_iterator init_begin() {
2524 const auto *ConstThis = this;
2525 return const_cast<init_iterator>(ConstThis->init_begin());
2526 }
2527
2528 /// Retrieve an iterator to the first initializer.
2529 init_const_iterator init_begin() const;
2530
2531 /// Retrieve an iterator past the last initializer.
2532 init_iterator init_end() {
2533 return init_begin() + NumCtorInitializers;
2534 }
2535
2536 /// Retrieve an iterator past the last initializer.
2537 init_const_iterator init_end() const {
2538 return init_begin() + NumCtorInitializers;
2539 }
2540
2541 using init_reverse_iterator = std::reverse_iterator<init_iterator>;
2542 using init_const_reverse_iterator =
2543 std::reverse_iterator<init_const_iterator>;
2544
2545 init_reverse_iterator init_rbegin() {
2546 return init_reverse_iterator(init_end());
2547 }
2548 init_const_reverse_iterator init_rbegin() const {
2549 return init_const_reverse_iterator(init_end());
2550 }
2551
2552 init_reverse_iterator init_rend() {
2553 return init_reverse_iterator(init_begin());
2554 }
2555 init_const_reverse_iterator init_rend() const {
2556 return init_const_reverse_iterator(init_begin());
2557 }
2558
2559 /// Determine the number of arguments used to initialize the member
2560 /// or base.
2561 unsigned getNumCtorInitializers() const {
2562 return NumCtorInitializers;
2563 }
2564
2565 void setNumCtorInitializers(unsigned numCtorInitializers) {
2566 NumCtorInitializers = numCtorInitializers;
2567 }
2568
2569 void setCtorInitializers(CXXCtorInitializer **Initializers) {
2570 CtorInitializers = Initializers;
2571 }
2572
2573 /// Whether this function is marked as explicit explicitly.
2574 bool isExplicitSpecified() const { return IsExplicitSpecified; }
2575
2576 /// Whether this function is explicit.
2577 bool isExplicit() const {
2578 return getCanonicalDecl()->isExplicitSpecified();
2579 }
2580
2581 /// Determine whether this constructor is a delegating constructor.
2582 bool isDelegatingConstructor() const {
2583 return (getNumCtorInitializers() == 1) &&
2584 init_begin()[0]->isDelegatingInitializer();
2585 }
2586
2587 /// When this constructor delegates to another, retrieve the target.
2588 CXXConstructorDecl *getTargetConstructor() const;
2589
2590 /// Whether this constructor is a default
2591 /// constructor (C++ [class.ctor]p5), which can be used to
2592 /// default-initialize a class of this type.
2593 bool isDefaultConstructor() const;
2594
2595 /// Whether this constructor is a copy constructor (C++ [class.copy]p2,
2596 /// which can be used to copy the class.
2597 ///
2598 /// \p TypeQuals will be set to the qualifiers on the
2599 /// argument type. For example, \p TypeQuals would be set to \c
2600 /// Qualifiers::Const for the following copy constructor:
2601 ///
2602 /// \code
2603 /// class X {
2604 /// public:
2605 /// X(const X&);
2606 /// };
2607 /// \endcode
2608 bool isCopyConstructor(unsigned &TypeQuals) const;
2609
2610 /// Whether this constructor is a copy
2611 /// constructor (C++ [class.copy]p2, which can be used to copy the
2612 /// class.
2613 bool isCopyConstructor() const {
2614 unsigned TypeQuals = 0;
2615 return isCopyConstructor(TypeQuals);
2616 }
2617
2618 /// Determine whether this constructor is a move constructor
2619 /// (C++11 [class.copy]p3), which can be used to move values of the class.
2620 ///
2621 /// \param TypeQuals If this constructor is a move constructor, will be set
2622 /// to the type qualifiers on the referent of the first parameter's type.
2623 bool isMoveConstructor(unsigned &TypeQuals) const;
2624
2625 /// Determine whether this constructor is a move constructor
2626 /// (C++11 [class.copy]p3), which can be used to move values of the class.
2627 bool isMoveConstructor() const {
2628 unsigned TypeQuals = 0;
2629 return isMoveConstructor(TypeQuals);
2630 }
2631
2632 /// Determine whether this is a copy or move constructor.
2633 ///
2634 /// \param TypeQuals Will be set to the type qualifiers on the reference
2635 /// parameter, if in fact this is a copy or move constructor.
2636 bool isCopyOrMoveConstructor(unsigned &TypeQuals) const;
2637
2638 /// Determine whether this a copy or move constructor.
2639 bool isCopyOrMoveConstructor() const {
2640 unsigned Quals;
2641 return isCopyOrMoveConstructor(Quals);
2642 }
2643
2644 /// Whether this constructor is a
2645 /// converting constructor (C++ [class.conv.ctor]), which can be
2646 /// used for user-defined conversions.
2647 bool isConvertingConstructor(bool AllowExplicit) const;
2648
2649 /// Determine whether this is a member template specialization that
2650 /// would copy the object to itself. Such constructors are never used to copy
2651 /// an object.
2652 bool isSpecializationCopyingObject() const;
2653
2654 /// Determine whether this is an implicit constructor synthesized to
2655 /// model a call to a constructor inherited from a base class.
2656 bool isInheritingConstructor() const { return IsInheritingConstructor; }
2657
2658 /// Get the constructor that this inheriting constructor is based on.
2659 InheritedConstructor getInheritedConstructor() const {
2660 return IsInheritingConstructor ? *getTrailingObjects<InheritedConstructor>()
2661 : InheritedConstructor();
2662 }
2663
2664 CXXConstructorDecl *getCanonicalDecl() override {
2665 return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl());
2666 }
2667 const CXXConstructorDecl *getCanonicalDecl() const {
2668 return const_cast<CXXConstructorDecl*>(this)->getCanonicalDecl();
2669 }
2670
2671 // Implement isa/cast/dyncast/etc.
2672 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2673 static bool classofKind(Kind K) { return K == CXXConstructor; }
2674};
2675
2676/// Represents a C++ destructor within a class.
2677///
2678/// For example:
2679///
2680/// \code
2681/// class X {
2682/// public:
2683/// ~X(); // represented by a CXXDestructorDecl.
2684/// };
2685/// \endcode
2686class CXXDestructorDecl : public CXXMethodDecl {
2687 friend class ASTDeclReader;
2688 friend class ASTDeclWriter;
2689
2690 // FIXME: Don't allocate storage for these except in the first declaration
2691 // of a virtual destructor.
2692 FunctionDecl *OperatorDelete = nullptr;
2693 Expr *OperatorDeleteThisArg = nullptr;
2694
2695 CXXDestructorDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2696 const DeclarationNameInfo &NameInfo,
2697 QualType T, TypeSourceInfo *TInfo,
2698 bool isInline, bool isImplicitlyDeclared)
2699 : CXXMethodDecl(CXXDestructor, C, RD, StartLoc, NameInfo, T, TInfo,
2700 SC_None, isInline, /*isConstexpr=*/false, SourceLocation())
2701 {
2702 setImplicit(isImplicitlyDeclared);
2703 }
2704
2705 void anchor() override;
2706
2707public:
2708 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2709 SourceLocation StartLoc,
2710 const DeclarationNameInfo &NameInfo,
2711 QualType T, TypeSourceInfo* TInfo,
2712 bool isInline,
2713 bool isImplicitlyDeclared);
2714 static CXXDestructorDecl *CreateDeserialized(ASTContext & C, unsigned ID);
2715
2716 void setOperatorDelete(FunctionDecl *OD, Expr *ThisArg);
2717
2718 const FunctionDecl *getOperatorDelete() const {
2719 return getCanonicalDecl()->OperatorDelete;
2720 }
2721
2722 Expr *getOperatorDeleteThisArg() const {
2723 return getCanonicalDecl()->OperatorDeleteThisArg;
2724 }
2725
2726 CXXDestructorDecl *getCanonicalDecl() override {
2727 return cast<CXXDestructorDecl>(FunctionDecl::getCanonicalDecl());
2728 }
2729 const CXXDestructorDecl *getCanonicalDecl() const {
2730 return const_cast<CXXDestructorDecl*>(this)->getCanonicalDecl();
2731 }
2732
2733 // Implement isa/cast/dyncast/etc.
2734 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2735 static bool classofKind(Kind K) { return K == CXXDestructor; }
2736};
2737
2738/// Represents a C++ conversion function within a class.
2739///
2740/// For example:
2741///
2742/// \code
2743/// class X {
2744/// public:
2745/// operator bool();
2746/// };
2747/// \endcode
2748class CXXConversionDecl : public CXXMethodDecl {
2749 CXXConversionDecl(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2750 const DeclarationNameInfo &NameInfo, QualType T,
2751 TypeSourceInfo *TInfo, bool isInline,
2752 bool isExplicitSpecified, bool isConstexpr,
2753 SourceLocation EndLocation)
2754 : CXXMethodDecl(CXXConversion, C, RD, StartLoc, NameInfo, T, TInfo,
2755 SC_None, isInline, isConstexpr, EndLocation) {
2756 IsExplicitSpecified = isExplicitSpecified;
2757 }
2758
2759 void anchor() override;
2760
2761public:
2762 friend class ASTDeclReader;
2763 friend class ASTDeclWriter;
2764
2765 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD,
2766 SourceLocation StartLoc,
2767 const DeclarationNameInfo &NameInfo,
2768 QualType T, TypeSourceInfo *TInfo,
2769 bool isInline, bool isExplicit,
2770 bool isConstexpr,
2771 SourceLocation EndLocation);
2772 static CXXConversionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2773
2774 /// Whether this function is marked as explicit explicitly.
2775 bool isExplicitSpecified() const { return IsExplicitSpecified; }
2776
2777 /// Whether this function is explicit.
2778 bool isExplicit() const {
2779 return getCanonicalDecl()->isExplicitSpecified();
2780 }
2781
2782 /// Returns the type that this conversion function is converting to.
2783 QualType getConversionType() const {
2784 return getType()->getAs<FunctionType>()->getReturnType();
2785 }
2786
2787 /// Determine whether this conversion function is a conversion from
2788 /// a lambda closure type to a block pointer.
2789 bool isLambdaToBlockPointerConversion() const;
2790
2791 CXXConversionDecl *getCanonicalDecl() override {
2792 return cast<CXXConversionDecl>(FunctionDecl::getCanonicalDecl());
2793 }
2794 const CXXConversionDecl *getCanonicalDecl() const {
2795 return const_cast<CXXConversionDecl*>(this)->getCanonicalDecl();
2796 }
2797
2798 // Implement isa/cast/dyncast/etc.
2799 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2800 static bool classofKind(Kind K) { return K == CXXConversion; }
2801};
2802
2803/// Represents a linkage specification.
2804///
2805/// For example:
2806/// \code
2807/// extern "C" void foo();
2808/// \endcode
2809class LinkageSpecDecl : public Decl, public DeclContext {
2810 virtual void anchor();
2811
2812public:
2813 /// Represents the language in a linkage specification.
2814 ///
2815 /// The values are part of the serialization ABI for
2816 /// ASTs and cannot be changed without altering that ABI. To help
2817 /// ensure a stable ABI for this, we choose the DW_LANG_ encodings
2818 /// from the dwarf standard.
2819 enum LanguageIDs {
2820 lang_c = /* DW_LANG_C */ 0x0002,
2821 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004
2822 };
2823
2824private:
2825 /// The language for this linkage specification.
2826 unsigned Language : 3;
2827
2828 /// True if this linkage spec has braces.
2829 ///
2830 /// This is needed so that hasBraces() returns the correct result while the
2831 /// linkage spec body is being parsed. Once RBraceLoc has been set this is
2832 /// not used, so it doesn't need to be serialized.
2833 unsigned HasBraces : 1;
2834
2835 /// The source location for the extern keyword.
2836 SourceLocation ExternLoc;
2837
2838 /// The source location for the right brace (if valid).
2839 SourceLocation RBraceLoc;
2840
2841 LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2842 SourceLocation LangLoc, LanguageIDs lang, bool HasBraces)
2843 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2844 Language(lang), HasBraces(HasBraces), ExternLoc(ExternLoc),
2845 RBraceLoc(SourceLocation()) {}
2846
2847public:
2848 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC,
2849 SourceLocation ExternLoc,
2850 SourceLocation LangLoc, LanguageIDs Lang,
2851 bool HasBraces);
2852 static LinkageSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2853
2854 /// Return the language specified by this linkage specification.
2855 LanguageIDs getLanguage() const { return LanguageIDs(Language); }
2856
2857 /// Set the language specified by this linkage specification.
2858 void setLanguage(LanguageIDs L) { Language = L; }
2859
2860 /// Determines whether this linkage specification had braces in
2861 /// its syntactic form.
2862 bool hasBraces() const {
2863 assert(!RBraceLoc.isValid() || HasBraces);
2864 return HasBraces;
2865 }
2866
2867 SourceLocation getExternLoc() const { return ExternLoc; }
2868 SourceLocation getRBraceLoc() const { return RBraceLoc; }
2869 void setExternLoc(SourceLocation L) { ExternLoc = L; }
2870 void setRBraceLoc(SourceLocation L) {
2871 RBraceLoc = L;
2872 HasBraces = RBraceLoc.isValid();
2873 }
2874
2875 SourceLocation getLocEnd() const LLVM_READONLY {
2876 if (hasBraces())
2877 return getRBraceLoc();
2878 // No braces: get the end location of the (only) declaration in context
2879 // (if present).
2880 return decls_empty() ? getLocation() : decls_begin()->getLocEnd();
2881 }
2882
2883 SourceRange getSourceRange() const override LLVM_READONLY {
2884 return SourceRange(ExternLoc, getLocEnd());
2885 }
2886
2887 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2888 static bool classofKind(Kind K) { return K == LinkageSpec; }
2889
2890 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) {
2891 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D));
2892 }
2893
2894 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) {
2895 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC));
2896 }
2897};
2898
2899/// Represents C++ using-directive.
2900///
2901/// For example:
2902/// \code
2903/// using namespace std;
2904/// \endcode
2905///
2906/// \note UsingDirectiveDecl should be Decl not NamedDecl, but we provide
2907/// artificial names for all using-directives in order to store
2908/// them in DeclContext effectively.
2909class UsingDirectiveDecl : public NamedDecl {
2910 /// The location of the \c using keyword.
2911 SourceLocation UsingLoc;
2912
2913 /// The location of the \c namespace keyword.
2914 SourceLocation NamespaceLoc;
2915
2916 /// The nested-name-specifier that precedes the namespace.
2917 NestedNameSpecifierLoc QualifierLoc;
2918
2919 /// The namespace nominated by this using-directive.
2920 NamedDecl *NominatedNamespace;
2921
2922 /// Enclosing context containing both using-directive and nominated
2923 /// namespace.
2924 DeclContext *CommonAncestor;
2925
2926 UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc,
2927 SourceLocation NamespcLoc,
2928 NestedNameSpecifierLoc QualifierLoc,
2929 SourceLocation IdentLoc,
2930 NamedDecl *Nominated,
2931 DeclContext *CommonAncestor)
2932 : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc),
2933 NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc),
2934 NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) {}
2935
2936 /// Returns special DeclarationName used by using-directives.
2937 ///
2938 /// This is only used by DeclContext for storing UsingDirectiveDecls in
2939 /// its lookup structure.
2940 static DeclarationName getName() {
2941 return DeclarationName::getUsingDirectiveName();
2942 }
2943
2944 void anchor() override;
2945
2946public:
2947 friend class ASTDeclReader;
2948
2949 // Friend for getUsingDirectiveName.
2950 friend class DeclContext;
2951
2952 /// Retrieve the nested-name-specifier that qualifies the
2953 /// name of the namespace, with source-location information.
2954 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
2955
2956 /// Retrieve the nested-name-specifier that qualifies the
2957 /// name of the namespace.
2958 NestedNameSpecifier *getQualifier() const {
2959 return QualifierLoc.getNestedNameSpecifier();
2960 }
2961
2962 NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
2963 const NamedDecl *getNominatedNamespaceAsWritten() const {
2964 return NominatedNamespace;
2965 }
2966
2967 /// Returns the namespace nominated by this using-directive.
2968 NamespaceDecl *getNominatedNamespace();
2969
2970 const NamespaceDecl *getNominatedNamespace() const {
2971 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
2972 }
2973
2974 /// Returns the common ancestor context of this using-directive and
2975 /// its nominated namespace.
2976 DeclContext *getCommonAncestor() { return CommonAncestor; }
2977 const DeclContext *getCommonAncestor() const { return CommonAncestor; }
2978
2979 /// Return the location of the \c using keyword.
2980 SourceLocation getUsingLoc() const { return UsingLoc; }
2981
2982 // FIXME: Could omit 'Key' in name.
2983 /// Returns the location of the \c namespace keyword.
2984 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; }
2985
2986 /// Returns the location of this using declaration's identifier.
2987 SourceLocation getIdentLocation() const { return getLocation(); }
2988
2989 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC,
2990 SourceLocation UsingLoc,
2991 SourceLocation NamespaceLoc,
2992 NestedNameSpecifierLoc QualifierLoc,
2993 SourceLocation IdentLoc,
2994 NamedDecl *Nominated,
2995 DeclContext *CommonAncestor);
2996 static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2997
2998 SourceRange getSourceRange() const override LLVM_READONLY {
2999 return SourceRange(UsingLoc, getLocation());
3000 }
3001
3002 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3003 static bool classofKind(Kind K) { return K == UsingDirective; }
3004};
3005
3006/// Represents a C++ namespace alias.
3007///
3008/// For example:
3009///
3010/// \code
3011/// namespace Foo = Bar;
3012/// \endcode
3013class NamespaceAliasDecl : public NamedDecl,
3014 public Redeclarable<NamespaceAliasDecl> {
3015 friend class ASTDeclReader;
3016
3017 /// The location of the \c namespace keyword.
3018 SourceLocation NamespaceLoc;
3019
3020 /// The location of the namespace's identifier.
3021 ///
3022 /// This is accessed by TargetNameLoc.
3023 SourceLocation IdentLoc;
3024
3025 /// The nested-name-specifier that precedes the namespace.
3026 NestedNameSpecifierLoc QualifierLoc;
3027
3028 /// The Decl that this alias points to, either a NamespaceDecl or
3029 /// a NamespaceAliasDecl.
3030 NamedDecl *Namespace;
3031
3032 NamespaceAliasDecl(ASTContext &C, DeclContext *DC,
3033 SourceLocation NamespaceLoc, SourceLocation AliasLoc,
3034 IdentifierInfo *Alias, NestedNameSpecifierLoc QualifierLoc,
3035 SourceLocation IdentLoc, NamedDecl *Namespace)
3036 : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), redeclarable_base(C),
3037 NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc),
3038 QualifierLoc(QualifierLoc), Namespace(Namespace) {}
3039
3040 void anchor() override;
3041
3042 using redeclarable_base = Redeclarable<NamespaceAliasDecl>;
3043
3044 NamespaceAliasDecl *getNextRedeclarationImpl() override;
3045 NamespaceAliasDecl *getPreviousDeclImpl() override;
3046 NamespaceAliasDecl *getMostRecentDeclImpl() override;
3047
3048public:
3049 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC,
3050 SourceLocation NamespaceLoc,
3051 SourceLocation AliasLoc,
3052 IdentifierInfo *Alias,
3053 NestedNameSpecifierLoc QualifierLoc,
3054 SourceLocation IdentLoc,
3055 NamedDecl *Namespace);
3056
3057 static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3058
3059 using redecl_range = redeclarable_base::redecl_range;
3060 using redecl_iterator = redeclarable_base::redecl_iterator;
3061
3062 using redeclarable_base::redecls_begin;
3063 using redeclarable_base::redecls_end;
3064 using redeclarable_base::redecls;
3065 using redeclarable_base::getPreviousDecl;
3066 using redeclarable_base::getMostRecentDecl;
3067
3068 NamespaceAliasDecl *getCanonicalDecl() override {
3069 return getFirstDecl();
3070 }
3071 const NamespaceAliasDecl *getCanonicalDecl() const {
3072 return getFirstDecl();
3073 }
3074
3075 /// Retrieve the nested-name-specifier that qualifies the
3076 /// name of the namespace, with source-location information.
3077 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3078
3079 /// Retrieve the nested-name-specifier that qualifies the
3080 /// name of the namespace.
3081 NestedNameSpecifier *getQualifier() const {
3082 return QualifierLoc.getNestedNameSpecifier();
3083 }
3084
3085 /// Retrieve the namespace declaration aliased by this directive.
3086 NamespaceDecl *getNamespace() {
3087 if (auto *AD = dyn_cast<NamespaceAliasDecl>(Namespace))
3088 return AD->getNamespace();
3089
3090 return cast<NamespaceDecl>(Namespace);
3091 }
3092
3093 const NamespaceDecl *getNamespace() const {
3094 return const_cast<NamespaceAliasDecl *>(this)->getNamespace();
3095 }
3096
3097 /// Returns the location of the alias name, i.e. 'foo' in
3098 /// "namespace foo = ns::bar;".
3099 SourceLocation getAliasLoc() const { return getLocation(); }
3100
3101 /// Returns the location of the \c namespace keyword.
3102 SourceLocation getNamespaceLoc() const { return NamespaceLoc; }
3103
3104 /// Returns the location of the identifier in the named namespace.
3105 SourceLocation getTargetNameLoc() const { return IdentLoc; }
3106
3107 /// Retrieve the namespace that this alias refers to, which
3108 /// may either be a NamespaceDecl or a NamespaceAliasDecl.
3109 NamedDecl *getAliasedNamespace() const { return Namespace; }
3110
3111 SourceRange getSourceRange() const override LLVM_READONLY {
3112 return SourceRange(NamespaceLoc, IdentLoc);
3113 }
3114
3115 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3116 static bool classofKind(Kind K) { return K == NamespaceAlias; }
3117};
3118
3119/// Represents a shadow declaration introduced into a scope by a
3120/// (resolved) using declaration.
3121///
3122/// For example,
3123/// \code
3124/// namespace A {
3125/// void foo();
3126/// }
3127/// namespace B {
3128/// using A::foo; // <- a UsingDecl
3129/// // Also creates a UsingShadowDecl for A::foo() in B
3130/// }
3131/// \endcode
3132class UsingShadowDecl : public NamedDecl, public Redeclarable<UsingShadowDecl> {
3133 friend class UsingDecl;
3134
3135 /// The referenced declaration.
3136 NamedDecl *Underlying = nullptr;
3137
3138 /// The using declaration which introduced this decl or the next using
3139 /// shadow declaration contained in the aforementioned using declaration.
3140 NamedDecl *UsingOrNextShadow = nullptr;
3141
3142 void anchor() override;
3143
3144 using redeclarable_base = Redeclarable<UsingShadowDecl>;
3145
3146 UsingShadowDecl *getNextRedeclarationImpl() override {
3147 return getNextRedeclaration();
3148 }
3149
3150 UsingShadowDecl *getPreviousDeclImpl() override {
3151 return getPreviousDecl();
3152 }
3153
3154 UsingShadowDecl *getMostRecentDeclImpl() override {
3155 return getMostRecentDecl();
3156 }
3157
3158protected:
3159 UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, SourceLocation Loc,
3160 UsingDecl *Using, NamedDecl *Target);
3161 UsingShadowDecl(Kind K, ASTContext &C, EmptyShell);
3162
3163public:
3164 friend class ASTDeclReader;
3165 friend class ASTDeclWriter;
3166
3167 static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
3168 SourceLocation Loc, UsingDecl *Using,
3169 NamedDecl *Target) {
3170 return new (C, DC) UsingShadowDecl(UsingShadow, C, DC, Loc, Using, Target);
3171 }
3172
3173 static UsingShadowDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3174
3175 using redecl_range = redeclarable_base::redecl_range;
3176 using redecl_iterator = redeclarable_base::redecl_iterator;
3177
3178 using redeclarable_base::redecls_begin;
3179 using redeclarable_base::redecls_end;
3180 using redeclarable_base::redecls;
3181 using redeclarable_base::getPreviousDecl;
3182 using redeclarable_base::getMostRecentDecl;
3183 using redeclarable_base::isFirstDecl;
3184
3185 UsingShadowDecl *getCanonicalDecl() override {
3186 return getFirstDecl();
3187 }
3188 const UsingShadowDecl *getCanonicalDecl() const {
3189 return getFirstDecl();
3190 }
3191
3192 /// Gets the underlying declaration which has been brought into the
3193 /// local scope.
3194 NamedDecl *getTargetDecl() const { return Underlying; }
3195
3196 /// Sets the underlying declaration which has been brought into the
3197 /// local scope.
3198 void setTargetDecl(NamedDecl *ND) {
3199 assert(ND && "Target decl is null!");
3200 Underlying = ND;
3201 // A UsingShadowDecl is never a friend or local extern declaration, even
3202 // if it is a shadow declaration for one.
3203 IdentifierNamespace =
3204 ND->getIdentifierNamespace() &
3205 ~(IDNS_OrdinaryFriend | IDNS_TagFriend | IDNS_LocalExtern);
3206 }
3207
3208 /// Gets the using declaration to which this declaration is tied.
3209 UsingDecl *getUsingDecl() const;
3210
3211 /// The next using shadow declaration contained in the shadow decl
3212 /// chain of the using declaration which introduced this decl.
3213 UsingShadowDecl *getNextUsingShadowDecl() const {
3214 return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow);
3215 }
3216
3217 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3218 static bool classofKind(Kind K) {
3219 return K == Decl::UsingShadow || K == Decl::ConstructorUsingShadow;
3220 }
3221};
3222
3223/// Represents a shadow constructor declaration introduced into a
3224/// class by a C++11 using-declaration that names a constructor.
3225///
3226/// For example:
3227/// \code
3228/// struct Base { Base(int); };
3229/// struct Derived {
3230/// using Base::Base; // creates a UsingDecl and a ConstructorUsingShadowDecl
3231/// };
3232/// \endcode
3233class ConstructorUsingShadowDecl final : public UsingShadowDecl {
3234 /// If this constructor using declaration inherted the constructor
3235 /// from an indirect base class, this is the ConstructorUsingShadowDecl
3236 /// in the named direct base class from which the declaration was inherited.
3237 ConstructorUsingShadowDecl *NominatedBaseClassShadowDecl = nullptr;
3238
3239 /// If this constructor using declaration inherted the constructor
3240 /// from an indirect base class, this is the ConstructorUsingShadowDecl
3241 /// that will be used to construct the unique direct or virtual base class
3242 /// that receives the constructor arguments.
3243 ConstructorUsingShadowDecl *ConstructedBaseClassShadowDecl = nullptr;
3244
3245 /// \c true if the constructor ultimately named by this using shadow
3246 /// declaration is within a virtual base class subobject of the class that
3247 /// contains this declaration.
3248 unsigned IsVirtual : 1;
3249
3250 ConstructorUsingShadowDecl(ASTContext &C, DeclContext *DC, SourceLocation Loc,
3251 UsingDecl *Using, NamedDecl *Target,
3252 bool TargetInVirtualBase)
3253 : UsingShadowDecl(ConstructorUsingShadow, C, DC, Loc, Using,
3254 Target->getUnderlyingDecl()),
3255 NominatedBaseClassShadowDecl(
3256 dyn_cast<ConstructorUsingShadowDecl>(Target)),
3257 ConstructedBaseClassShadowDecl(NominatedBaseClassShadowDecl),
3258 IsVirtual(TargetInVirtualBase) {
3259 // If we found a constructor that chains to a constructor for a virtual
3260 // base, we should directly call that virtual base constructor instead.
3261 // FIXME: This logic belongs in Sema.
3262 if (NominatedBaseClassShadowDecl &&
3263 NominatedBaseClassShadowDecl->constructsVirtualBase()) {
3264 ConstructedBaseClassShadowDecl =
3265 NominatedBaseClassShadowDecl->ConstructedBaseClassShadowDecl;
3266 IsVirtual = true;
3267 }
3268 }
3269
3270 ConstructorUsingShadowDecl(ASTContext &C, EmptyShell Empty)
3271 : UsingShadowDecl(ConstructorUsingShadow, C, Empty), IsVirtual(false) {}
3272
3273 void anchor() override;
3274
3275public:
3276 friend class ASTDeclReader;
3277 friend class ASTDeclWriter;
3278
3279 static ConstructorUsingShadowDecl *Create(ASTContext &C, DeclContext *DC,
3280 SourceLocation Loc,
3281 UsingDecl *Using, NamedDecl *Target,
3282 bool IsVirtual);
3283 static ConstructorUsingShadowDecl *CreateDeserialized(ASTContext &C,
3284 unsigned ID);
3285
3286 /// Returns the parent of this using shadow declaration, which
3287 /// is the class in which this is declared.
3288 //@{
3289 const CXXRecordDecl *getParent() const {
3290 return cast<CXXRecordDecl>(getDeclContext());
3291 }
3292 CXXRecordDecl *getParent() {
3293 return cast<CXXRecordDecl>(getDeclContext());
3294 }
3295 //@}
3296
3297 /// Get the inheriting constructor declaration for the direct base
3298 /// class from which this using shadow declaration was inherited, if there is
3299 /// one. This can be different for each redeclaration of the same shadow decl.
3300 ConstructorUsingShadowDecl *getNominatedBaseClassShadowDecl() const {
3301 return NominatedBaseClassShadowDecl;
3302 }
3303
3304 /// Get the inheriting constructor declaration for the base class
3305 /// for which we don't have an explicit initializer, if there is one.
3306 ConstructorUsingShadowDecl *getConstructedBaseClassShadowDecl() const {
3307 return ConstructedBaseClassShadowDecl;
3308 }
3309
3310 /// Get the base class that was named in the using declaration. This
3311 /// can be different for each redeclaration of this same shadow decl.
3312 CXXRecordDecl *getNominatedBaseClass() const;
3313
3314 /// Get the base class whose constructor or constructor shadow
3315 /// declaration is passed the constructor arguments.
3316 CXXRecordDecl *getConstructedBaseClass() const {
3317 return cast<CXXRecordDecl>((ConstructedBaseClassShadowDecl
3318 ? ConstructedBaseClassShadowDecl
3319 : getTargetDecl())
3320 ->getDeclContext());
3321 }
3322
3323 /// Returns \c true if the constructed base class is a virtual base
3324 /// class subobject of this declaration's class.
3325 bool constructsVirtualBase() const {
3326 return IsVirtual;
3327 }
3328
3329 /// Get the constructor or constructor template in the derived class
3330 /// correspnding to this using shadow declaration, if it has been implicitly
3331 /// declared already.
3332 CXXConstructorDecl *getConstructor() const;
3333 void setConstructor(NamedDecl *Ctor);
3334
3335 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3336 static bool classofKind(Kind K) { return K == ConstructorUsingShadow; }
3337};
3338
3339/// Represents a C++ using-declaration.
3340///
3341/// For example:
3342/// \code
3343/// using someNameSpace::someIdentifier;
3344/// \endcode
3345class UsingDecl : public NamedDecl, public Mergeable<UsingDecl> {
3346 /// The source location of the 'using' keyword itself.
3347 SourceLocation UsingLocation;
3348
3349 /// The nested-name-specifier that precedes the name.
3350 NestedNameSpecifierLoc QualifierLoc;
3351
3352 /// Provides source/type location info for the declaration name
3353 /// embedded in the ValueDecl base class.
3354 DeclarationNameLoc DNLoc;
3355
3356 /// The first shadow declaration of the shadow decl chain associated
3357 /// with this using declaration.
3358 ///
3359 /// The bool member of the pair store whether this decl has the \c typename
3360 /// keyword.
3361 llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow;
3362
3363 UsingDecl(DeclContext *DC, SourceLocation UL,
3364 NestedNameSpecifierLoc QualifierLoc,
3365 const DeclarationNameInfo &NameInfo, bool HasTypenameKeyword)
3366 : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()),
3367 UsingLocation(UL), QualifierLoc(QualifierLoc),
3368 DNLoc(NameInfo.getInfo()), FirstUsingShadow(nullptr, HasTypenameKeyword) {
3369 }
3370
3371 void anchor() override;
3372
3373public:
3374 friend class ASTDeclReader;
3375 friend class ASTDeclWriter;
3376
3377 /// Return the source location of the 'using' keyword.
3378 SourceLocation getUsingLoc() const { return UsingLocation; }
3379
3380 /// Set the source location of the 'using' keyword.
3381 void setUsingLoc(SourceLocation L) { UsingLocation = L; }
3382
3383 /// Retrieve the nested-name-specifier that qualifies the name,
3384 /// with source-location information.
3385 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3386
3387 /// Retrieve the nested-name-specifier that qualifies the name.
3388 NestedNameSpecifier *getQualifier() const {
3389 return QualifierLoc.getNestedNameSpecifier();
3390 }
3391
3392 DeclarationNameInfo getNameInfo() const {
3393 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
3394 }
3395
3396 /// Return true if it is a C++03 access declaration (no 'using').
3397 bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
3398
3399 /// Return true if the using declaration has 'typename'.
3400 bool hasTypename() const { return FirstUsingShadow.getInt(); }
3401
3402 /// Sets whether the using declaration has 'typename'.
3403 void setTypename(bool TN) { FirstUsingShadow.setInt(TN); }
3404
3405 /// Iterates through the using shadow declarations associated with
3406 /// this using declaration.
3407 class shadow_iterator {
3408 /// The current using shadow declaration.
3409 UsingShadowDecl *Current = nullptr;
3410
3411 public:
3412 using value_type = UsingShadowDecl *;
3413 using reference = UsingShadowDecl *;
3414 using pointer = UsingShadowDecl *;
3415 using iterator_category = std::forward_iterator_tag;
3416 using difference_type = std::ptrdiff_t;
3417
3418 shadow_iterator() = default;
3419 explicit shadow_iterator(UsingShadowDecl *C) : Current(C) {}
3420
3421 reference operator*() const { return Current; }
3422 pointer operator->() const { return Current; }
3423
3424 shadow_iterator& operator++() {
3425 Current = Current->getNextUsingShadowDecl();
3426 return *this;
3427 }
3428
3429 shadow_iterator operator++(int) {
3430 shadow_iterator tmp(*this);
3431 ++(*this);
3432 return tmp;
3433 }
3434
3435 friend bool operator==(shadow_iterator x, shadow_iterator y) {
3436 return x.Current == y.Current;
3437 }
3438 friend bool operator!=(shadow_iterator x, shadow_iterator y) {
3439 return x.Current != y.Current;
3440 }
3441 };
3442
3443 using shadow_range = llvm::iterator_range<shadow_iterator>;
3444
3445 shadow_range shadows() const {
3446 return shadow_range(shadow_begin(), shadow_end());
3447 }
3448
3449 shadow_iterator shadow_begin() const {
3450 return shadow_iterator(FirstUsingShadow.getPointer());
3451 }
3452
3453 shadow_iterator shadow_end() const { return shadow_iterator(); }
3454
3455 /// Return the number of shadowed declarations associated with this
3456 /// using declaration.
3457 unsigned shadow_size() const {
3458 return std::distance(shadow_begin(), shadow_end());
3459 }
3460
3461 void addShadowDecl(UsingShadowDecl *S);
3462 void removeShadowDecl(UsingShadowDecl *S);
3463
3464 static UsingDecl *Create(ASTContext &C, DeclContext *DC,
3465 SourceLocation UsingL,
3466 NestedNameSpecifierLoc QualifierLoc,
3467 const DeclarationNameInfo &NameInfo,
3468 bool HasTypenameKeyword);
3469
3470 static UsingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3471
3472 SourceRange getSourceRange() const override LLVM_READONLY;
3473
3474 /// Retrieves the canonical declaration of this declaration.
3475 UsingDecl *getCanonicalDecl() override { return getFirstDecl(); }
3476 const UsingDecl *getCanonicalDecl() const { return getFirstDecl(); }
3477
3478 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3479 static bool classofKind(Kind K) { return K == Using; }
3480};
3481
3482/// Represents a pack of using declarations that a single
3483/// using-declarator pack-expanded into.
3484///
3485/// \code
3486/// template<typename ...T> struct X : T... {
3487/// using T::operator()...;
3488/// using T::operator T...;
3489/// };
3490/// \endcode
3491///
3492/// In the second case above, the UsingPackDecl will have the name
3493/// 'operator T' (which contains an unexpanded pack), but the individual
3494/// UsingDecls and UsingShadowDecls will have more reasonable names.
3495class UsingPackDecl final
3496 : public NamedDecl, public Mergeable<UsingPackDecl>,
3497 private llvm::TrailingObjects<UsingPackDecl, NamedDecl *> {
3498 /// The UnresolvedUsingValueDecl or UnresolvedUsingTypenameDecl from
3499 /// which this waas instantiated.
3500 NamedDecl *InstantiatedFrom;
3501
3502 /// The number of using-declarations created by this pack expansion.
3503 unsigned NumExpansions;
3504
3505 UsingPackDecl(DeclContext *DC, NamedDecl *InstantiatedFrom,
3506 ArrayRef<NamedDecl *> UsingDecls)
3507 : NamedDecl(UsingPack, DC,
3508 InstantiatedFrom ? InstantiatedFrom->getLocation()
3509 : SourceLocation(),
3510 InstantiatedFrom ? InstantiatedFrom->getDeclName()
3511 : DeclarationName()),
3512 InstantiatedFrom(InstantiatedFrom), NumExpansions(UsingDecls.size()) {
3513 std::uninitialized_copy(UsingDecls.begin(), UsingDecls.end(),
3514 getTrailingObjects<NamedDecl *>());
3515 }
3516
3517 void anchor() override;
3518
3519public:
3520 friend class ASTDeclReader;
3521 friend class ASTDeclWriter;
3522 friend TrailingObjects;
3523
3524 /// Get the using declaration from which this was instantiated. This will
3525 /// always be an UnresolvedUsingValueDecl or an UnresolvedUsingTypenameDecl
3526 /// that is a pack expansion.
3527 NamedDecl *getInstantiatedFromUsingDecl() const { return InstantiatedFrom; }
3528
3529 /// Get the set of using declarations that this pack expanded into. Note that
3530 /// some of these may still be unresolved.
3531 ArrayRef<NamedDecl *> expansions() const {
3532 return llvm::makeArrayRef(getTrailingObjects<NamedDecl *>(), NumExpansions);
3533 }
3534
3535 static UsingPackDecl *Create(ASTContext &C, DeclContext *DC,
3536 NamedDecl *InstantiatedFrom,
3537 ArrayRef<NamedDecl *> UsingDecls);
3538
3539 static UsingPackDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3540 unsigned NumExpansions);
3541
3542 SourceRange getSourceRange() const override LLVM_READONLY {
3543 return InstantiatedFrom->getSourceRange();
3544 }
3545
3546 UsingPackDecl *getCanonicalDecl() override { return getFirstDecl(); }
3547 const UsingPackDecl *getCanonicalDecl() const { return getFirstDecl(); }
3548
3549 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3550 static bool classofKind(Kind K) { return K == UsingPack; }
3551};
3552
3553/// Represents a dependent using declaration which was not marked with
3554/// \c typename.
3555///
3556/// Unlike non-dependent using declarations, these *only* bring through
3557/// non-types; otherwise they would break two-phase lookup.
3558///
3559/// \code
3560/// template \<class T> class A : public Base<T> {
3561/// using Base<T>::foo;
3562/// };
3563/// \endcode
3564class UnresolvedUsingValueDecl : public ValueDecl,
3565 public Mergeable<UnresolvedUsingValueDecl> {
3566 /// The source location of the 'using' keyword
3567 SourceLocation UsingLocation;
3568
3569 /// If this is a pack expansion, the location of the '...'.
3570 SourceLocation EllipsisLoc;
3571
3572 /// The nested-name-specifier that precedes the name.
3573 NestedNameSpecifierLoc QualifierLoc;
3574
3575 /// Provides source/type location info for the declaration name
3576 /// embedded in the ValueDecl base class.
3577 DeclarationNameLoc DNLoc;
3578
3579 UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty,
3580 SourceLocation UsingLoc,
3581 NestedNameSpecifierLoc QualifierLoc,
3582 const DeclarationNameInfo &NameInfo,
3583 SourceLocation EllipsisLoc)
3584 : ValueDecl(UnresolvedUsingValue, DC,
3585 NameInfo.getLoc(), NameInfo.getName(), Ty),
3586 UsingLocation(UsingLoc), EllipsisLoc(EllipsisLoc),
3587 QualifierLoc(QualifierLoc), DNLoc(NameInfo.getInfo()) {}
3588
3589 void anchor() override;
3590
3591public:
3592 friend class ASTDeclReader;
3593 friend class ASTDeclWriter;
3594
3595 /// Returns the source location of the 'using' keyword.
3596 SourceLocation getUsingLoc() const { return UsingLocation; }
3597
3598 /// Set the source location of the 'using' keyword.
3599 void setUsingLoc(SourceLocation L) { UsingLocation = L; }
3600
3601 /// Return true if it is a C++03 access declaration (no 'using').
3602 bool isAccessDeclaration() const { return UsingLocation.isInvalid(); }
3603
3604 /// Retrieve the nested-name-specifier that qualifies the name,
3605 /// with source-location information.
3606 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3607
3608 /// Retrieve the nested-name-specifier that qualifies the name.
3609 NestedNameSpecifier *getQualifier() const {
3610 return QualifierLoc.getNestedNameSpecifier();
3611 }
3612
3613 DeclarationNameInfo getNameInfo() const {
3614 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
3615 }
3616
3617 /// Determine whether this is a pack expansion.
3618 bool isPackExpansion() const {
3619 return EllipsisLoc.isValid();
3620 }
3621
3622 /// Get the location of the ellipsis if this is a pack expansion.
3623 SourceLocation getEllipsisLoc() const {
3624 return EllipsisLoc;
3625 }
3626
3627 static UnresolvedUsingValueDecl *
3628 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
3629 NestedNameSpecifierLoc QualifierLoc,
3630 const DeclarationNameInfo &NameInfo, SourceLocation EllipsisLoc);
3631
3632 static UnresolvedUsingValueDecl *
3633 CreateDeserialized(ASTContext &C, unsigned ID);
3634
3635 SourceRange getSourceRange() const override LLVM_READONLY;
3636
3637 /// Retrieves the canonical declaration of this declaration.
3638 UnresolvedUsingValueDecl *getCanonicalDecl() override {
3639 return getFirstDecl();
3640 }
3641 const UnresolvedUsingValueDecl *getCanonicalDecl() const {
3642 return getFirstDecl();
3643 }
3644
3645 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3646 static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
3647};
3648
3649/// Represents a dependent using declaration which was marked with
3650/// \c typename.
3651///
3652/// \code
3653/// template \<class T> class A : public Base<T> {
3654/// using typename Base<T>::foo;
3655/// };
3656/// \endcode
3657///
3658/// The type associated with an unresolved using typename decl is
3659/// currently always a typename type.
3660class UnresolvedUsingTypenameDecl
3661 : public TypeDecl,
3662 public Mergeable<UnresolvedUsingTypenameDecl> {
3663 friend class ASTDeclReader;
3664
3665 /// The source location of the 'typename' keyword
3666 SourceLocation TypenameLocation;
3667
3668 /// If this is a pack expansion, the location of the '...'.
3669 SourceLocation EllipsisLoc;
3670
3671 /// The nested-name-specifier that precedes the name.
3672 NestedNameSpecifierLoc QualifierLoc;
3673
3674 UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc,
3675 SourceLocation TypenameLoc,
3676 NestedNameSpecifierLoc QualifierLoc,
3677 SourceLocation TargetNameLoc,
3678 IdentifierInfo *TargetName,
3679 SourceLocation EllipsisLoc)
3680 : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName,
3681 UsingLoc),
3682 TypenameLocation(TypenameLoc), EllipsisLoc(EllipsisLoc),
3683 QualifierLoc(QualifierLoc) {}
3684
3685 void anchor() override;
3686
3687public:
3688 /// Returns the source location of the 'using' keyword.
3689 SourceLocation getUsingLoc() const { return getLocStart(); }
3690
3691 /// Returns the source location of the 'typename' keyword.
3692 SourceLocation getTypenameLoc() const { return TypenameLocation; }
3693
3694 /// Retrieve the nested-name-specifier that qualifies the name,
3695 /// with source-location information.
3696 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
3697
3698 /// Retrieve the nested-name-specifier that qualifies the name.
3699 NestedNameSpecifier *getQualifier() const {
3700 return QualifierLoc.getNestedNameSpecifier();
3701 }
3702
3703 DeclarationNameInfo getNameInfo() const {
3704 return DeclarationNameInfo(getDeclName(), getLocation());
3705 }
3706
3707 /// Determine whether this is a pack expansion.
3708 bool isPackExpansion() const {
3709 return EllipsisLoc.isValid();
3710 }
3711
3712 /// Get the location of the ellipsis if this is a pack expansion.
3713 SourceLocation getEllipsisLoc() const {
3714 return EllipsisLoc;
3715 }
3716
3717 static UnresolvedUsingTypenameDecl *
3718 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
3719 SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc,
3720 SourceLocation TargetNameLoc, DeclarationName TargetName,
3721 SourceLocation EllipsisLoc);
3722
3723 static UnresolvedUsingTypenameDecl *
3724 CreateDeserialized(ASTContext &C, unsigned ID);
3725
3726 /// Retrieves the canonical declaration of this declaration.
3727 UnresolvedUsingTypenameDecl *getCanonicalDecl() override {
3728 return getFirstDecl();
3729 }
3730 const UnresolvedUsingTypenameDecl *getCanonicalDecl() const {
3731 return getFirstDecl();
3732 }
3733
3734 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3735 static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
3736};
3737
3738/// Represents a C++11 static_assert declaration.
3739class StaticAssertDecl : public Decl {
3740 llvm::PointerIntPair<Expr *, 1, bool> AssertExprAndFailed;
3741 StringLiteral *Message;
3742 SourceLocation RParenLoc;
3743
3744 StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc,
3745 Expr *AssertExpr, StringLiteral *Message,
3746 SourceLocation RParenLoc, bool Failed)
3747 : Decl(StaticAssert, DC, StaticAssertLoc),
3748 AssertExprAndFailed(AssertExpr, Failed), Message(Message),
3749 RParenLoc(RParenLoc) {}
3750
3751 virtual void anchor();
3752
3753public:
3754 friend class ASTDeclReader;
3755
3756 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC,
3757 SourceLocation StaticAssertLoc,
3758 Expr *AssertExpr, StringLiteral *Message,
3759 SourceLocation RParenLoc, bool Failed);
3760 static StaticAssertDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3761
3762 Expr *getAssertExpr() { return AssertExprAndFailed.getPointer(); }
3763 const Expr *getAssertExpr() const { return AssertExprAndFailed.getPointer(); }
3764
3765 StringLiteral *getMessage() { return Message; }
3766 const StringLiteral *getMessage() const { return Message; }
3767
3768 bool isFailed() const { return AssertExprAndFailed.getInt(); }
3769
3770 SourceLocation getRParenLoc() const { return RParenLoc; }
3771
3772 SourceRange getSourceRange() const override LLVM_READONLY {
3773 return SourceRange(getLocation(), getRParenLoc());
3774 }
3775
3776 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3777 static bool classofKind(Kind K) { return K == StaticAssert; }
3778};
3779
3780/// A binding in a decomposition declaration. For instance, given:
3781///
3782/// int n[3];
3783/// auto &[a, b, c] = n;
3784///
3785/// a, b, and c are BindingDecls, whose bindings are the expressions
3786/// x[0], x[1], and x[2] respectively, where x is the implicit
3787/// DecompositionDecl of type 'int (&)[3]'.
3788class BindingDecl : public ValueDecl {
3789 /// The binding represented by this declaration. References to this
3790 /// declaration are effectively equivalent to this expression (except
3791 /// that it is only evaluated once at the point of declaration of the
3792 /// binding).
3793 Expr *Binding = nullptr;
3794
3795 BindingDecl(DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id)
3796 : ValueDecl(Decl::Binding, DC, IdLoc, Id, QualType()) {}
3797
3798 void anchor() override;
3799
3800public:
3801 friend class ASTDeclReader;
3802
3803 static BindingDecl *Create(ASTContext &C, DeclContext *DC,
3804 SourceLocation IdLoc, IdentifierInfo *Id);
3805 static BindingDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3806
3807 /// Get the expression to which this declaration is bound. This may be null
3808 /// in two different cases: while parsing the initializer for the
3809 /// decomposition declaration, and when the initializer is type-dependent.
3810 Expr *getBinding() const { return Binding; }
3811
3812 /// Get the variable (if any) that holds the value of evaluating the binding.
3813 /// Only present for user-defined bindings for tuple-like types.
3814 VarDecl *getHoldingVar() const;
3815
3816 /// Set the binding for this BindingDecl, along with its declared type (which
3817 /// should be a possibly-cv-qualified form of the type of the binding, or a
3818 /// reference to such a type).
3819 void setBinding(QualType DeclaredType, Expr *Binding) {
3820 setType(DeclaredType);
3821 this->Binding = Binding;
3822 }
3823
3824 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3825 static bool classofKind(Kind K) { return K == Decl::Binding; }
3826};
3827
3828/// A decomposition declaration. For instance, given:
3829///
3830/// int n[3];
3831/// auto &[a, b, c] = n;
3832///
3833/// the second line declares a DecompositionDecl of type 'int (&)[3]', and
3834/// three BindingDecls (named a, b, and c). An instance of this class is always
3835/// unnamed, but behaves in almost all other respects like a VarDecl.
3836class DecompositionDecl final
3837 : public VarDecl,
3838 private llvm::TrailingObjects<DecompositionDecl, BindingDecl *> {
3839 /// The number of BindingDecl*s following this object.
3840 unsigned NumBindings;
3841
3842 DecompositionDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
3843 SourceLocation LSquareLoc, QualType T,
3844 TypeSourceInfo *TInfo, StorageClass SC,
3845 ArrayRef<BindingDecl *> Bindings)
3846 : VarDecl(Decomposition, C, DC, StartLoc, LSquareLoc, nullptr, T, TInfo,
3847 SC),
3848 NumBindings(Bindings.size()) {
3849 std::uninitialized_copy(Bindings.begin(), Bindings.end(),
3850 getTrailingObjects<BindingDecl *>());
3851 }
3852
3853 void anchor() override;
3854
3855public:
3856 friend class ASTDeclReader;
3857 friend TrailingObjects;
3858
3859 static DecompositionDecl *Create(ASTContext &C, DeclContext *DC,
3860 SourceLocation StartLoc,
3861 SourceLocation LSquareLoc,
3862 QualType T, TypeSourceInfo *TInfo,
3863 StorageClass S,
3864 ArrayRef<BindingDecl *> Bindings);
3865 static DecompositionDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3866 unsigned NumBindings);
3867
3868 ArrayRef<BindingDecl *> bindings() const {
3869 return llvm::makeArrayRef(getTrailingObjects<BindingDecl *>(), NumBindings);
3870 }
3871
3872 void printName(raw_ostream &os) const override;
3873
3874 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3875 static bool classofKind(Kind K) { return K == Decomposition; }
3876};
3877
3878/// An instance of this class represents the declaration of a property
3879/// member. This is a Microsoft extension to C++, first introduced in
3880/// Visual Studio .NET 2003 as a parallel to similar features in C#
3881/// and Managed C++.
3882///
3883/// A property must always be a non-static class member.
3884///
3885/// A property member superficially resembles a non-static data
3886/// member, except preceded by a property attribute:
3887/// __declspec(property(get=GetX, put=PutX)) int x;
3888/// Either (but not both) of the 'get' and 'put' names may be omitted.
3889///
3890/// A reference to a property is always an lvalue. If the lvalue
3891/// undergoes lvalue-to-rvalue conversion, then a getter name is
3892/// required, and that member is called with no arguments.
3893/// If the lvalue is assigned into, then a setter name is required,
3894/// and that member is called with one argument, the value assigned.
3895/// Both operations are potentially overloaded. Compound assignments
3896/// are permitted, as are the increment and decrement operators.
3897///
3898/// The getter and putter methods are permitted to be overloaded,
3899/// although their return and parameter types are subject to certain
3900/// restrictions according to the type of the property.
3901///
3902/// A property declared using an incomplete array type may
3903/// additionally be subscripted, adding extra parameters to the getter
3904/// and putter methods.
3905class MSPropertyDecl : public DeclaratorDecl {
3906 IdentifierInfo *GetterId, *SetterId;
3907
3908 MSPropertyDecl(DeclContext *DC, SourceLocation L, DeclarationName N,
3909 QualType T, TypeSourceInfo *TInfo, SourceLocation StartL,
3910 IdentifierInfo *Getter, IdentifierInfo *Setter)
3911 : DeclaratorDecl(MSProperty, DC, L, N, T, TInfo, StartL),
3912 GetterId(Getter), SetterId(Setter) {}
3913
3914public:
3915 friend class ASTDeclReader;
3916
3917 static MSPropertyDecl *Create(ASTContext &C, DeclContext *DC,
3918 SourceLocation L, DeclarationName N, QualType T,
3919 TypeSourceInfo *TInfo, SourceLocation StartL,
3920 IdentifierInfo *Getter, IdentifierInfo *Setter);
3921 static MSPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3922
3923 static bool classof(const Decl *D) { return D->getKind() == MSProperty; }
3924
3925 bool hasGetter() const { return GetterId != nullptr; }
3926 IdentifierInfo* getGetterId() const { return GetterId; }
3927 bool hasSetter() const { return SetterId != nullptr; }
3928 IdentifierInfo* getSetterId() const { return SetterId; }
3929};
3930
3931/// Insertion operator for diagnostics. This allows sending an AccessSpecifier
3932/// into a diagnostic with <<.
3933const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
3934 AccessSpecifier AS);
3935
3936const PartialDiagnostic &operator<<(const PartialDiagnostic &DB,
3937 AccessSpecifier AS);
3938
3939} // namespace clang
3940
3941#endif // LLVM_CLANG_AST_DECLCXX_H
3942