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