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