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