DeclCXX.h revision ccc1b5eebc6ca8a904c58c0468b9a71483b7c7cf
1//===-- DeclCXX.h - Classes for representing C++ declarations -*- C++ -*-=====// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the C++ Decl subclasses, other than those for 11// templates (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/Expr.h" 19#include "clang/AST/ExprCXX.h" 20#include "clang/AST/Decl.h" 21#include "clang/AST/TypeLoc.h" 22#include "clang/AST/UnresolvedSet.h" 23#include "llvm/ADT/DenseMap.h" 24#include "llvm/ADT/PointerIntPair.h" 25#include "llvm/ADT/SmallPtrSet.h" 26 27namespace clang { 28 29class ClassTemplateDecl; 30class ClassTemplateSpecializationDecl; 31class CXXBasePath; 32class CXXBasePaths; 33class CXXConstructorDecl; 34class CXXConversionDecl; 35class CXXDestructorDecl; 36class CXXMethodDecl; 37class CXXRecordDecl; 38class CXXMemberLookupCriteria; 39class CXXFinalOverriderMap; 40class CXXIndirectPrimaryBaseSet; 41class FriendDecl; 42class LambdaExpr; 43 44/// \brief Represents any kind of function declaration, whether it is a 45/// concrete function or a function template. 46class AnyFunctionDecl { 47 NamedDecl *Function; 48 49 AnyFunctionDecl(NamedDecl *ND) : Function(ND) { } 50 51public: 52 AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { } 53 AnyFunctionDecl(FunctionTemplateDecl *FTD); 54 55 /// \brief Implicily converts any function or function template into a 56 /// named declaration. 57 operator NamedDecl *() const { return Function; } 58 59 /// \brief Retrieve the underlying function or function template. 60 NamedDecl *get() const { return Function; } 61 62 static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) { 63 return AnyFunctionDecl(ND); 64 } 65}; 66 67} // end namespace clang 68 69namespace llvm { 70 /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from 71 /// AnyFunctionDecl to any function or function template declaration. 72 template<> struct simplify_type<const ::clang::AnyFunctionDecl> { 73 typedef ::clang::NamedDecl* SimpleType; 74 static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) { 75 return Val; 76 } 77 }; 78 template<> struct simplify_type< ::clang::AnyFunctionDecl> 79 : public simplify_type<const ::clang::AnyFunctionDecl> {}; 80 81 // Provide PointerLikeTypeTraits for non-cvr pointers. 82 template<> 83 class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> { 84 public: 85 static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) { 86 return F.get(); 87 } 88 static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) { 89 return ::clang::AnyFunctionDecl::getFromNamedDecl( 90 static_cast< ::clang::NamedDecl*>(P)); 91 } 92 93 enum { NumLowBitsAvailable = 2 }; 94 }; 95 96} // end namespace llvm 97 98namespace clang { 99 100/// AccessSpecDecl - An access specifier followed by colon ':'. 101/// 102/// An objects of this class represents sugar for the syntactic occurrence 103/// of an access specifier followed by a colon in the list of member 104/// specifiers of a C++ class definition. 105/// 106/// Note that they do not represent other uses of access specifiers, 107/// such as those occurring in a list of base specifiers. 108/// Also note that this class has nothing to do with so-called 109/// "access declarations" (C++98 11.3 [class.access.dcl]). 110class AccessSpecDecl : public Decl { 111 virtual void anchor(); 112 /// ColonLoc - The location of the ':'. 113 SourceLocation ColonLoc; 114 115 AccessSpecDecl(AccessSpecifier AS, DeclContext *DC, 116 SourceLocation ASLoc, SourceLocation ColonLoc) 117 : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) { 118 setAccess(AS); 119 } 120 AccessSpecDecl(EmptyShell Empty) 121 : Decl(AccessSpec, Empty) { } 122public: 123 /// getAccessSpecifierLoc - The location of the access specifier. 124 SourceLocation getAccessSpecifierLoc() const { return getLocation(); } 125 /// setAccessSpecifierLoc - Sets the location of the access specifier. 126 void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); } 127 128 /// getColonLoc - The location of the colon following the access specifier. 129 SourceLocation getColonLoc() const { return ColonLoc; } 130 /// setColonLoc - Sets the location of the colon. 131 void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; } 132 133 SourceRange getSourceRange() const { 134 return SourceRange(getAccessSpecifierLoc(), getColonLoc()); 135 } 136 137 static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS, 138 DeclContext *DC, SourceLocation ASLoc, 139 SourceLocation ColonLoc) { 140 return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc); 141 } 142 static AccessSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID); 143 144 // Implement isa/cast/dyncast/etc. 145 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 146 static bool classof(const AccessSpecDecl *D) { return true; } 147 static bool classofKind(Kind K) { return K == AccessSpec; } 148}; 149 150 151/// CXXBaseSpecifier - A base class of a C++ class. 152/// 153/// Each CXXBaseSpecifier represents a single, direct base class (or 154/// struct) of a C++ class (or struct). It specifies the type of that 155/// base class, whether it is a virtual or non-virtual base, and what 156/// level of access (public, protected, private) is used for the 157/// derivation. For example: 158/// 159/// @code 160/// class A { }; 161/// class B { }; 162/// class C : public virtual A, protected B { }; 163/// @endcode 164/// 165/// In this code, C will have two CXXBaseSpecifiers, one for "public 166/// virtual A" and the other for "protected B". 167class CXXBaseSpecifier { 168 /// Range - The source code range that covers the full base 169 /// specifier, including the "virtual" (if present) and access 170 /// specifier (if present). 171 SourceRange Range; 172 173 /// \brief The source location of the ellipsis, if this is a pack 174 /// expansion. 175 SourceLocation EllipsisLoc; 176 177 /// Virtual - Whether this is a virtual base class or not. 178 bool Virtual : 1; 179 180 /// BaseOfClass - Whether this is the base of a class (true) or of a 181 /// struct (false). This determines the mapping from the access 182 /// specifier as written in the source code to the access specifier 183 /// used for semantic analysis. 184 bool BaseOfClass : 1; 185 186 /// Access - Access specifier as written in the source code (which 187 /// may be AS_none). The actual type of data stored here is an 188 /// AccessSpecifier, but we use "unsigned" here to work around a 189 /// VC++ bug. 190 unsigned Access : 2; 191 192 /// InheritConstructors - Whether the class contains a using declaration 193 /// to inherit the named class's constructors. 194 bool InheritConstructors : 1; 195 196 /// BaseTypeInfo - The type of the base class. This will be a class or struct 197 /// (or a typedef of such). The source code range does not include the 198 /// "virtual" or access specifier. 199 TypeSourceInfo *BaseTypeInfo; 200 201public: 202 CXXBaseSpecifier() { } 203 204 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, 205 TypeSourceInfo *TInfo, SourceLocation EllipsisLoc) 206 : Range(R), EllipsisLoc(EllipsisLoc), Virtual(V), BaseOfClass(BC), 207 Access(A), InheritConstructors(false), BaseTypeInfo(TInfo) { } 208 209 /// getSourceRange - Retrieves the source range that contains the 210 /// entire base specifier. 211 SourceRange getSourceRange() const { return Range; } 212 213 /// isVirtual - Determines whether the base class is a virtual base 214 /// class (or not). 215 bool isVirtual() const { return Virtual; } 216 217 /// \brief Determine whether this base class is a base of a class declared 218 /// with the 'class' keyword (vs. one declared with the 'struct' keyword). 219 bool isBaseOfClass() const { return BaseOfClass; } 220 221 /// \brief Determine whether this base specifier is a pack expansion. 222 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 223 224 /// \brief Determine whether this base class's constructors get inherited. 225 bool getInheritConstructors() const { return InheritConstructors; } 226 227 /// \brief Set that this base class's constructors should be inherited. 228 void setInheritConstructors(bool Inherit = true) { 229 InheritConstructors = Inherit; 230 } 231 232 /// \brief For a pack expansion, determine the location of the ellipsis. 233 SourceLocation getEllipsisLoc() const { 234 return EllipsisLoc; 235 } 236 237 /// getAccessSpecifier - Returns the access specifier for this base 238 /// specifier. This is the actual base specifier as used for 239 /// semantic analysis, so the result can never be AS_none. To 240 /// retrieve the access specifier as written in the source code, use 241 /// getAccessSpecifierAsWritten(). 242 AccessSpecifier getAccessSpecifier() const { 243 if ((AccessSpecifier)Access == AS_none) 244 return BaseOfClass? AS_private : AS_public; 245 else 246 return (AccessSpecifier)Access; 247 } 248 249 /// getAccessSpecifierAsWritten - Retrieves the access specifier as 250 /// written in the source code (which may mean that no access 251 /// specifier was explicitly written). Use getAccessSpecifier() to 252 /// retrieve the access specifier for use in semantic analysis. 253 AccessSpecifier getAccessSpecifierAsWritten() const { 254 return (AccessSpecifier)Access; 255 } 256 257 /// getType - Retrieves the type of the base class. This type will 258 /// always be an unqualified class type. 259 QualType getType() const { return BaseTypeInfo->getType(); } 260 261 /// getTypeLoc - Retrieves the type and source location of the base class. 262 TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; } 263}; 264 265/// CXXRecordDecl - Represents a C++ struct/union/class. 266/// FIXME: This class will disappear once we've properly taught RecordDecl 267/// to deal with C++-specific things. 268class CXXRecordDecl : public RecordDecl { 269 270 friend void TagDecl::startDefinition(); 271 272 struct DefinitionData { 273 DefinitionData(CXXRecordDecl *D); 274 275 /// UserDeclaredConstructor - True when this class has a 276 /// user-declared constructor. 277 bool UserDeclaredConstructor : 1; 278 279 /// UserDeclaredCopyConstructor - True when this class has a 280 /// user-declared copy constructor. 281 bool UserDeclaredCopyConstructor : 1; 282 283 /// UserDeclareMoveConstructor - True when this class has a 284 /// user-declared move constructor. 285 bool UserDeclaredMoveConstructor : 1; 286 287 /// UserDeclaredCopyAssignment - True when this class has a 288 /// user-declared copy assignment operator. 289 bool UserDeclaredCopyAssignment : 1; 290 291 /// UserDeclareMoveAssignment - True when this class has a 292 /// user-declared move assignment. 293 bool UserDeclaredMoveAssignment : 1; 294 295 /// UserDeclaredDestructor - True when this class has a 296 /// user-declared destructor. 297 bool UserDeclaredDestructor : 1; 298 299 /// Aggregate - True when this class is an aggregate. 300 bool Aggregate : 1; 301 302 /// PlainOldData - True when this class is a POD-type. 303 bool PlainOldData : 1; 304 305 /// Empty - true when this class is empty for traits purposes, 306 /// i.e. has no data members other than 0-width bit-fields, has no 307 /// virtual function/base, and doesn't inherit from a non-empty 308 /// class. Doesn't take union-ness into account. 309 bool Empty : 1; 310 311 /// Polymorphic - True when this class is polymorphic, i.e. has at 312 /// least one virtual member or derives from a polymorphic class. 313 bool Polymorphic : 1; 314 315 /// Abstract - True when this class is abstract, i.e. has at least 316 /// one pure virtual function, (that can come from a base class). 317 bool Abstract : 1; 318 319 /// IsStandardLayout - True when this class has standard layout. 320 /// 321 /// C++0x [class]p7. A standard-layout class is a class that: 322 /// * has no non-static data members of type non-standard-layout class (or 323 /// array of such types) or reference, 324 /// * has no virtual functions (10.3) and no virtual base classes (10.1), 325 /// * has the same access control (Clause 11) for all non-static data 326 /// members 327 /// * has no non-standard-layout base classes, 328 /// * either has no non-static data members in the most derived class and at 329 /// most one base class with non-static data members, or has no base 330 /// classes with non-static data members, and 331 /// * has no base classes of the same type as the first non-static data 332 /// member. 333 bool IsStandardLayout : 1; 334 335 /// HasNoNonEmptyBases - True when there are no non-empty base classes. 336 /// 337 /// This is a helper bit of state used to implement IsStandardLayout more 338 /// efficiently. 339 bool HasNoNonEmptyBases : 1; 340 341 /// HasPrivateFields - True when there are private non-static data members. 342 bool HasPrivateFields : 1; 343 344 /// HasProtectedFields - True when there are protected non-static data 345 /// members. 346 bool HasProtectedFields : 1; 347 348 /// HasPublicFields - True when there are private non-static data members. 349 bool HasPublicFields : 1; 350 351 /// \brief True if this class (or any subobject) has mutable fields. 352 bool HasMutableFields : 1; 353 354 /// \brief True if there no non-field members declared by the user. 355 bool HasOnlyCMembers : 1; 356 357 /// HasTrivialDefaultConstructor - True when, if this class has a default 358 /// constructor, this default constructor is trivial. 359 /// 360 /// C++0x [class.ctor]p5 361 /// A default constructor is trivial if it is not user-provided and if 362 /// -- its class has no virtual functions and no virtual base classes, 363 /// and 364 /// -- no non-static data member of its class has a 365 /// brace-or-equal-initializer, and 366 /// -- all the direct base classes of its class have trivial 367 /// default constructors, and 368 /// -- for all the nonstatic data members of its class that are of class 369 /// type (or array thereof), each such class has a trivial 370 /// default constructor. 371 bool HasTrivialDefaultConstructor : 1; 372 373 /// HasConstexprNonCopyMoveConstructor - True when this class has at least 374 /// one user-declared constexpr constructor which is neither the copy nor 375 /// move constructor. 376 bool HasConstexprNonCopyMoveConstructor : 1; 377 378 /// DefaultedDefaultConstructorIsConstexpr - True if a defaulted default 379 /// constructor for this class would be constexpr. 380 bool DefaultedDefaultConstructorIsConstexpr : 1; 381 382 /// DefaultedCopyConstructorIsConstexpr - True if a defaulted copy 383 /// constructor for this class would be constexpr. 384 bool DefaultedCopyConstructorIsConstexpr : 1; 385 386 /// DefaultedMoveConstructorIsConstexpr - True if a defaulted move 387 /// constructor for this class would be constexpr. 388 bool DefaultedMoveConstructorIsConstexpr : 1; 389 390 /// HasConstexprDefaultConstructor - True if this class has a constexpr 391 /// default constructor (either user-declared or implicitly declared). 392 bool HasConstexprDefaultConstructor : 1; 393 394 /// HasConstexprCopyConstructor - True if this class has a constexpr copy 395 /// constructor (either user-declared or implicitly declared). 396 bool HasConstexprCopyConstructor : 1; 397 398 /// HasConstexprMoveConstructor - True if this class has a constexpr move 399 /// constructor (either user-declared or implicitly declared). 400 bool HasConstexprMoveConstructor : 1; 401 402 /// HasTrivialCopyConstructor - True when this class has a trivial copy 403 /// constructor. 404 /// 405 /// C++0x [class.copy]p13: 406 /// A copy/move constructor for class X is trivial if it is neither 407 /// user-provided and if 408 /// -- class X has no virtual functions and no virtual base classes, and 409 /// -- the constructor selected to copy/move each direct base class 410 /// subobject is trivial, and 411 /// -- for each non-static data member of X that is of class type (or an 412 /// array thereof), the constructor selected to copy/move that member 413 /// is trivial; 414 /// otherwise the copy/move constructor is non-trivial. 415 bool HasTrivialCopyConstructor : 1; 416 417 /// HasTrivialMoveConstructor - True when this class has a trivial move 418 /// constructor. 419 /// 420 /// C++0x [class.copy]p13: 421 /// A copy/move constructor for class X is trivial if it is neither 422 /// user-provided and if 423 /// -- class X has no virtual functions and no virtual base classes, and 424 /// -- the constructor selected to copy/move each direct base class 425 /// subobject is trivial, and 426 /// -- for each non-static data member of X that is of class type (or an 427 /// array thereof), the constructor selected to copy/move that member 428 /// is trivial; 429 /// otherwise the copy/move constructor is non-trivial. 430 bool HasTrivialMoveConstructor : 1; 431 432 /// HasTrivialCopyAssignment - True when this class has a trivial copy 433 /// assignment operator. 434 /// 435 /// C++0x [class.copy]p27: 436 /// A copy/move assignment operator for class X is trivial if it is 437 /// neither user-provided nor deleted and if 438 /// -- class X has no virtual functions and no virtual base classes, and 439 /// -- the assignment operator selected to copy/move each direct base 440 /// class subobject is trivial, and 441 /// -- for each non-static data member of X that is of class type (or an 442 /// array thereof), the assignment operator selected to copy/move 443 /// that member is trivial; 444 /// otherwise the copy/move assignment operator is non-trivial. 445 bool HasTrivialCopyAssignment : 1; 446 447 /// HasTrivialMoveAssignment - True when this class has a trivial move 448 /// assignment operator. 449 /// 450 /// C++0x [class.copy]p27: 451 /// A copy/move assignment operator for class X is trivial if it is 452 /// neither user-provided nor deleted and if 453 /// -- class X has no virtual functions and no virtual base classes, and 454 /// -- the assignment operator selected to copy/move each direct base 455 /// class subobject is trivial, and 456 /// -- for each non-static data member of X that is of class type (or an 457 /// array thereof), the assignment operator selected to copy/move 458 /// that member is trivial; 459 /// otherwise the copy/move assignment operator is non-trivial. 460 bool HasTrivialMoveAssignment : 1; 461 462 /// HasTrivialDestructor - True when this class has a trivial destructor. 463 /// 464 /// C++ [class.dtor]p3. A destructor is trivial if it is an 465 /// implicitly-declared destructor and if: 466 /// * all of the direct base classes of its class have trivial destructors 467 /// and 468 /// * for all of the non-static data members of its class that are of class 469 /// type (or array thereof), each such class has a trivial destructor. 470 bool HasTrivialDestructor : 1; 471 472 /// HasNonLiteralTypeFieldsOrBases - True when this class contains at least 473 /// one non-static data member or base class of non-literal or volatile 474 /// type. 475 bool HasNonLiteralTypeFieldsOrBases : 1; 476 477 /// ComputedVisibleConversions - True when visible conversion functions are 478 /// already computed and are available. 479 bool ComputedVisibleConversions : 1; 480 481 /// \brief Whether we have a C++0x user-provided default constructor (not 482 /// explicitly deleted or defaulted). 483 bool UserProvidedDefaultConstructor : 1; 484 485 /// \brief Whether we have already declared the default constructor. 486 bool DeclaredDefaultConstructor : 1; 487 488 /// \brief Whether we have already declared the copy constructor. 489 bool DeclaredCopyConstructor : 1; 490 491 /// \brief Whether we have already declared the move constructor. 492 bool DeclaredMoveConstructor : 1; 493 494 /// \brief Whether we have already declared the copy-assignment operator. 495 bool DeclaredCopyAssignment : 1; 496 497 /// \brief Whether we have already declared the move-assignment operator. 498 bool DeclaredMoveAssignment : 1; 499 500 /// \brief Whether we have already declared a destructor within the class. 501 bool DeclaredDestructor : 1; 502 503 /// \brief Whether an implicit move constructor was attempted to be declared 504 /// but would have been deleted. 505 bool FailedImplicitMoveConstructor : 1; 506 507 /// \brief Whether an implicit move assignment operator was attempted to be 508 /// declared but would have been deleted. 509 bool FailedImplicitMoveAssignment : 1; 510 511 /// \brief Whether this class describes a C++ lambda. 512 bool IsLambda : 1; 513 514 /// NumBases - The number of base class specifiers in Bases. 515 unsigned NumBases; 516 517 /// NumVBases - The number of virtual base class specifiers in VBases. 518 unsigned NumVBases; 519 520 /// Bases - Base classes of this class. 521 /// FIXME: This is wasted space for a union. 522 LazyCXXBaseSpecifiersPtr Bases; 523 524 /// VBases - direct and indirect virtual base classes of this class. 525 LazyCXXBaseSpecifiersPtr VBases; 526 527 /// Conversions - Overload set containing the conversion functions 528 /// of this C++ class (but not its inherited conversion 529 /// functions). Each of the entries in this overload set is a 530 /// CXXConversionDecl. 531 UnresolvedSet<4> Conversions; 532 533 /// VisibleConversions - Overload set containing the conversion 534 /// functions of this C++ class and all those inherited conversion 535 /// functions that are visible in this class. Each of the entries 536 /// in this overload set is a CXXConversionDecl or a 537 /// FunctionTemplateDecl. 538 UnresolvedSet<4> VisibleConversions; 539 540 /// Definition - The declaration which defines this record. 541 CXXRecordDecl *Definition; 542 543 /// FirstFriend - The first friend declaration in this class, or 544 /// null if there aren't any. This is actually currently stored 545 /// in reverse order. 546 FriendDecl *FirstFriend; 547 548 /// \brief Retrieve the set of direct base classes. 549 CXXBaseSpecifier *getBases() const { 550 return Bases.get(Definition->getASTContext().getExternalSource()); 551 } 552 553 /// \brief Retrieve the set of virtual base classes. 554 CXXBaseSpecifier *getVBases() const { 555 return VBases.get(Definition->getASTContext().getExternalSource()); 556 } 557 } *DefinitionData; 558 559 /// \brief Describes a C++ closure type (generated by a lambda expression). 560 struct LambdaDefinitionData : public DefinitionData { 561 typedef LambdaExpr::Capture Capture; 562 563 LambdaDefinitionData(CXXRecordDecl *D) 564 : DefinitionData(D), NumCaptures(0), NumExplicitCaptures(0), 565 ContextDecl(0), Captures(0) 566 { 567 IsLambda = true; 568 } 569 570 /// \brief The number of captures in this lambda. 571 unsigned NumCaptures : 16; 572 573 /// \brief The number of explicit captures in this lambda. 574 unsigned NumExplicitCaptures : 16; 575 576 /// \brief The number used to indicate this lambda expression for name 577 /// mangling in the Itanium C++ ABI. 578 unsigned ManglingNumber; 579 580 /// \brief The declaration that provides context for this lambda, if the 581 /// actual DeclContext does not suffice. This is used for lambdas that 582 /// occur within default arguments of function parameters within the class 583 /// or within a data member initializer. 584 Decl *ContextDecl; 585 586 /// \brief The list of captures, both explicit and implicit, for this 587 /// lambda. 588 Capture *Captures; 589 }; 590 591 struct DefinitionData &data() { 592 assert(DefinitionData && "queried property of class with no definition"); 593 return *DefinitionData; 594 } 595 596 const struct DefinitionData &data() const { 597 assert(DefinitionData && "queried property of class with no definition"); 598 return *DefinitionData; 599 } 600 601 struct LambdaDefinitionData &getLambdaData() const { 602 assert(DefinitionData && "queried property of lambda with no definition"); 603 assert(DefinitionData->IsLambda && 604 "queried lambda property of non-lambda class"); 605 return static_cast<LambdaDefinitionData &>(*DefinitionData); 606 } 607 608 /// \brief The template or declaration that this declaration 609 /// describes or was instantiated from, respectively. 610 /// 611 /// For non-templates, this value will be NULL. For record 612 /// declarations that describe a class template, this will be a 613 /// pointer to a ClassTemplateDecl. For member 614 /// classes of class template specializations, this will be the 615 /// MemberSpecializationInfo referring to the member class that was 616 /// instantiated or specialized. 617 llvm::PointerUnion<ClassTemplateDecl*, MemberSpecializationInfo*> 618 TemplateOrInstantiation; 619 620 friend class DeclContext; 621 friend class LambdaExpr; 622 623 /// \brief Notify the class that member has been added. 624 /// 625 /// This routine helps maintain information about the class based on which 626 /// members have been added. It will be invoked by DeclContext::addDecl() 627 /// whenever a member is added to this record. 628 void addedMember(Decl *D); 629 630 void markedVirtualFunctionPure(); 631 friend void FunctionDecl::setPure(bool); 632 633 friend class ASTNodeImporter; 634 635protected: 636 CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 637 SourceLocation StartLoc, SourceLocation IdLoc, 638 IdentifierInfo *Id, CXXRecordDecl *PrevDecl); 639 640public: 641 /// base_class_iterator - Iterator that traverses the base classes 642 /// of a class. 643 typedef CXXBaseSpecifier* base_class_iterator; 644 645 /// base_class_const_iterator - Iterator that traverses the base 646 /// classes of a class. 647 typedef const CXXBaseSpecifier* base_class_const_iterator; 648 649 /// reverse_base_class_iterator = Iterator that traverses the base classes 650 /// of a class in reverse order. 651 typedef std::reverse_iterator<base_class_iterator> 652 reverse_base_class_iterator; 653 654 /// reverse_base_class_iterator = Iterator that traverses the base classes 655 /// of a class in reverse order. 656 typedef std::reverse_iterator<base_class_const_iterator> 657 reverse_base_class_const_iterator; 658 659 virtual CXXRecordDecl *getCanonicalDecl() { 660 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl()); 661 } 662 virtual const CXXRecordDecl *getCanonicalDecl() const { 663 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl()); 664 } 665 666 const CXXRecordDecl *getPreviousDecl() const { 667 return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDecl()); 668 } 669 CXXRecordDecl *getPreviousDecl() { 670 return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDecl()); 671 } 672 673 const CXXRecordDecl *getMostRecentDecl() const { 674 return cast_or_null<CXXRecordDecl>(RecordDecl::getMostRecentDecl()); 675 } 676 CXXRecordDecl *getMostRecentDecl() { 677 return cast_or_null<CXXRecordDecl>(RecordDecl::getMostRecentDecl()); 678 } 679 680 CXXRecordDecl *getDefinition() const { 681 if (!DefinitionData) return 0; 682 return data().Definition; 683 } 684 685 bool hasDefinition() const { return DefinitionData != 0; } 686 687 static CXXRecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, 688 SourceLocation StartLoc, SourceLocation IdLoc, 689 IdentifierInfo *Id, CXXRecordDecl* PrevDecl=0, 690 bool DelayTypeCreation = false); 691 static CXXRecordDecl *CreateLambda(const ASTContext &C, DeclContext *DC, 692 SourceLocation Loc); 693 static CXXRecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); 694 695 bool isDynamicClass() const { 696 return data().Polymorphic || data().NumVBases != 0; 697 } 698 699 /// setBases - Sets the base classes of this struct or class. 700 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases); 701 702 /// getNumBases - Retrieves the number of base classes of this 703 /// class. 704 unsigned getNumBases() const { return data().NumBases; } 705 706 base_class_iterator bases_begin() { return data().getBases(); } 707 base_class_const_iterator bases_begin() const { return data().getBases(); } 708 base_class_iterator bases_end() { return bases_begin() + data().NumBases; } 709 base_class_const_iterator bases_end() const { 710 return bases_begin() + data().NumBases; 711 } 712 reverse_base_class_iterator bases_rbegin() { 713 return reverse_base_class_iterator(bases_end()); 714 } 715 reverse_base_class_const_iterator bases_rbegin() const { 716 return reverse_base_class_const_iterator(bases_end()); 717 } 718 reverse_base_class_iterator bases_rend() { 719 return reverse_base_class_iterator(bases_begin()); 720 } 721 reverse_base_class_const_iterator bases_rend() const { 722 return reverse_base_class_const_iterator(bases_begin()); 723 } 724 725 /// getNumVBases - Retrieves the number of virtual base classes of this 726 /// class. 727 unsigned getNumVBases() const { return data().NumVBases; } 728 729 base_class_iterator vbases_begin() { return data().getVBases(); } 730 base_class_const_iterator vbases_begin() const { return data().getVBases(); } 731 base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; } 732 base_class_const_iterator vbases_end() const { 733 return vbases_begin() + data().NumVBases; 734 } 735 reverse_base_class_iterator vbases_rbegin() { 736 return reverse_base_class_iterator(vbases_end()); 737 } 738 reverse_base_class_const_iterator vbases_rbegin() const { 739 return reverse_base_class_const_iterator(vbases_end()); 740 } 741 reverse_base_class_iterator vbases_rend() { 742 return reverse_base_class_iterator(vbases_begin()); 743 } 744 reverse_base_class_const_iterator vbases_rend() const { 745 return reverse_base_class_const_iterator(vbases_begin()); 746 } 747 748 /// \brief Determine whether this class has any dependent base classes. 749 bool hasAnyDependentBases() const; 750 751 /// Iterator access to method members. The method iterator visits 752 /// all method members of the class, including non-instance methods, 753 /// special methods, etc. 754 typedef specific_decl_iterator<CXXMethodDecl> method_iterator; 755 756 /// method_begin - Method begin iterator. Iterates in the order the methods 757 /// were declared. 758 method_iterator method_begin() const { 759 return method_iterator(decls_begin()); 760 } 761 /// method_end - Method end iterator. 762 method_iterator method_end() const { 763 return method_iterator(decls_end()); 764 } 765 766 /// Iterator access to constructor members. 767 typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator; 768 769 ctor_iterator ctor_begin() const { 770 return ctor_iterator(decls_begin()); 771 } 772 ctor_iterator ctor_end() const { 773 return ctor_iterator(decls_end()); 774 } 775 776 /// An iterator over friend declarations. All of these are defined 777 /// in DeclFriend.h. 778 class friend_iterator; 779 friend_iterator friend_begin() const; 780 friend_iterator friend_end() const; 781 void pushFriendDecl(FriendDecl *FD); 782 783 /// Determines whether this record has any friends. 784 bool hasFriends() const { 785 return data().FirstFriend != 0; 786 } 787 788 /// \brief Determine if we need to declare a default constructor for 789 /// this class. 790 /// 791 /// This value is used for lazy creation of default constructors. 792 bool needsImplicitDefaultConstructor() const { 793 return !data().UserDeclaredConstructor && 794 !data().DeclaredDefaultConstructor; 795 } 796 797 /// hasDeclaredDefaultConstructor - Whether this class's default constructor 798 /// has been declared (either explicitly or implicitly). 799 bool hasDeclaredDefaultConstructor() const { 800 return data().DeclaredDefaultConstructor; 801 } 802 803 /// hasConstCopyConstructor - Determines whether this class has a 804 /// copy constructor that accepts a const-qualified argument. 805 bool hasConstCopyConstructor() const; 806 807 /// getCopyConstructor - Returns the copy constructor for this class 808 CXXConstructorDecl *getCopyConstructor(unsigned TypeQuals) const; 809 810 /// getMoveConstructor - Returns the move constructor for this class 811 CXXConstructorDecl *getMoveConstructor() const; 812 813 /// \brief Retrieve the copy-assignment operator for this class, if available. 814 /// 815 /// This routine attempts to find the copy-assignment operator for this 816 /// class, using a simplistic form of overload resolution. 817 /// 818 /// \param ArgIsConst Whether the argument to the copy-assignment operator 819 /// is const-qualified. 820 /// 821 /// \returns The copy-assignment operator that can be invoked, or NULL if 822 /// a unique copy-assignment operator could not be found. 823 CXXMethodDecl *getCopyAssignmentOperator(bool ArgIsConst) const; 824 825 /// getMoveAssignmentOperator - Returns the move assignment operator for this 826 /// class 827 CXXMethodDecl *getMoveAssignmentOperator() const; 828 829 /// hasUserDeclaredConstructor - Whether this class has any 830 /// user-declared constructors. When true, a default constructor 831 /// will not be implicitly declared. 832 bool hasUserDeclaredConstructor() const { 833 return data().UserDeclaredConstructor; 834 } 835 836 /// hasUserProvidedDefaultconstructor - Whether this class has a 837 /// user-provided default constructor per C++0x. 838 bool hasUserProvidedDefaultConstructor() const { 839 return data().UserProvidedDefaultConstructor; 840 } 841 842 /// hasUserDeclaredCopyConstructor - Whether this class has a 843 /// user-declared copy constructor. When false, a copy constructor 844 /// will be implicitly declared. 845 bool hasUserDeclaredCopyConstructor() const { 846 return data().UserDeclaredCopyConstructor; 847 } 848 849 /// \brief Determine whether this class has had its copy constructor 850 /// declared, either via the user or via an implicit declaration. 851 /// 852 /// This value is used for lazy creation of copy constructors. 853 bool hasDeclaredCopyConstructor() const { 854 return data().DeclaredCopyConstructor; 855 } 856 857 /// hasUserDeclaredMoveOperation - Whether this class has a user- 858 /// declared move constructor or assignment operator. When false, a 859 /// move constructor and assignment operator may be implicitly declared. 860 bool hasUserDeclaredMoveOperation() const { 861 return data().UserDeclaredMoveConstructor || 862 data().UserDeclaredMoveAssignment; 863 } 864 865 /// \brief Determine whether this class has had a move constructor 866 /// declared by the user. 867 bool hasUserDeclaredMoveConstructor() const { 868 return data().UserDeclaredMoveConstructor; 869 } 870 871 /// \brief Determine whether this class has had a move constructor 872 /// declared. 873 bool hasDeclaredMoveConstructor() const { 874 return data().DeclaredMoveConstructor; 875 } 876 877 /// \brief Determine whether implicit move constructor generation for this 878 /// class has failed before. 879 bool hasFailedImplicitMoveConstructor() const { 880 return data().FailedImplicitMoveConstructor; 881 } 882 883 /// \brief Set whether implicit move constructor generation for this class 884 /// has failed before. 885 void setFailedImplicitMoveConstructor(bool Failed = true) { 886 data().FailedImplicitMoveConstructor = Failed; 887 } 888 889 /// \brief Determine whether this class should get an implicit move 890 /// constructor or if any existing special member function inhibits this. 891 /// 892 /// Covers all bullets of C++0x [class.copy]p9 except the last, that the 893 /// constructor wouldn't be deleted, which is only looked up from a cached 894 /// result. 895 bool needsImplicitMoveConstructor() const { 896 return !hasFailedImplicitMoveConstructor() && 897 !hasDeclaredMoveConstructor() && 898 !hasUserDeclaredCopyConstructor() && 899 !hasUserDeclaredCopyAssignment() && 900 !hasUserDeclaredMoveAssignment() && 901 !hasUserDeclaredDestructor(); 902 } 903 904 /// hasUserDeclaredCopyAssignment - Whether this class has a 905 /// user-declared copy assignment operator. When false, a copy 906 /// assigment operator will be implicitly declared. 907 bool hasUserDeclaredCopyAssignment() const { 908 return data().UserDeclaredCopyAssignment; 909 } 910 911 /// \brief Determine whether this class has had its copy assignment operator 912 /// declared, either via the user or via an implicit declaration. 913 /// 914 /// This value is used for lazy creation of copy assignment operators. 915 bool hasDeclaredCopyAssignment() const { 916 return data().DeclaredCopyAssignment; 917 } 918 919 /// \brief Determine whether this class has had a move assignment 920 /// declared by the user. 921 bool hasUserDeclaredMoveAssignment() const { 922 return data().UserDeclaredMoveAssignment; 923 } 924 925 /// hasDeclaredMoveAssignment - Whether this class has a 926 /// declared move assignment operator. 927 bool hasDeclaredMoveAssignment() const { 928 return data().DeclaredMoveAssignment; 929 } 930 931 /// \brief Determine whether implicit move assignment generation for this 932 /// class has failed before. 933 bool hasFailedImplicitMoveAssignment() const { 934 return data().FailedImplicitMoveAssignment; 935 } 936 937 /// \brief Set whether implicit move assignment generation for this class 938 /// has failed before. 939 void setFailedImplicitMoveAssignment(bool Failed = true) { 940 data().FailedImplicitMoveAssignment = Failed; 941 } 942 943 /// \brief Determine whether this class should get an implicit move 944 /// assignment operator or if any existing special member function inhibits 945 /// this. 946 /// 947 /// Covers all bullets of C++0x [class.copy]p20 except the last, that the 948 /// constructor wouldn't be deleted. 949 bool needsImplicitMoveAssignment() const { 950 return !hasFailedImplicitMoveAssignment() && 951 !hasDeclaredMoveAssignment() && 952 !hasUserDeclaredCopyConstructor() && 953 !hasUserDeclaredCopyAssignment() && 954 !hasUserDeclaredMoveConstructor() && 955 !hasUserDeclaredDestructor(); 956 } 957 958 /// hasUserDeclaredDestructor - Whether this class has a 959 /// user-declared destructor. When false, a destructor will be 960 /// implicitly declared. 961 bool hasUserDeclaredDestructor() const { 962 return data().UserDeclaredDestructor; 963 } 964 965 /// \brief Determine whether this class has had its destructor declared, 966 /// either via the user or via an implicit declaration. 967 /// 968 /// This value is used for lazy creation of destructors. 969 bool hasDeclaredDestructor() const { return data().DeclaredDestructor; } 970 971 /// \brief Determine whether this class describes a lambda function object. 972 bool isLambda() const { return hasDefinition() && data().IsLambda; } 973 974 /// \brief For a closure type, retrieve the mapping from captured 975 /// variables and this to the non-static data members that store the 976 /// values or references of the captures. 977 /// 978 /// \param Captures Will be populated with the mapping from captured 979 /// variables to the corresponding fields. 980 /// 981 /// \param ThisCapture Will be set to the field declaration for the 982 /// 'this' capture. 983 void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures, 984 FieldDecl *&ThisCapture) const; 985 986 /// getConversions - Retrieve the overload set containing all of the 987 /// conversion functions in this class. 988 UnresolvedSetImpl *getConversionFunctions() { 989 return &data().Conversions; 990 } 991 const UnresolvedSetImpl *getConversionFunctions() const { 992 return &data().Conversions; 993 } 994 995 typedef UnresolvedSetImpl::iterator conversion_iterator; 996 conversion_iterator conversion_begin() const { 997 return getConversionFunctions()->begin(); 998 } 999 conversion_iterator conversion_end() const { 1000 return getConversionFunctions()->end(); 1001 } 1002 1003 /// Removes a conversion function from this class. The conversion 1004 /// function must currently be a member of this class. Furthermore, 1005 /// this class must currently be in the process of being defined. 1006 void removeConversion(const NamedDecl *Old); 1007 1008 /// getVisibleConversionFunctions - get all conversion functions visible 1009 /// in current class; including conversion function templates. 1010 const UnresolvedSetImpl *getVisibleConversionFunctions(); 1011 1012 /// isAggregate - Whether this class is an aggregate (C++ 1013 /// [dcl.init.aggr]), which is a class with no user-declared 1014 /// constructors, no private or protected non-static data members, 1015 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 1016 bool isAggregate() const { return data().Aggregate; } 1017 1018 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 1019 /// that is an aggregate that has no non-static non-POD data members, no 1020 /// reference data members, no user-defined copy assignment operator and no 1021 /// user-defined destructor. 1022 bool isPOD() const { return data().PlainOldData; } 1023 1024 /// \brief True if this class is C-like, without C++-specific features, e.g. 1025 /// it contains only public fields, no bases, tag kind is not 'class', etc. 1026 bool isCLike() const; 1027 1028 /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which 1029 /// means it has a virtual function, virtual base, data member (other than 1030 /// 0-width bit-field) or inherits from a non-empty class. Does NOT include 1031 /// a check for union-ness. 1032 bool isEmpty() const { return data().Empty; } 1033 1034 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 1035 /// which means that the class contains or inherits a virtual function. 1036 bool isPolymorphic() const { return data().Polymorphic; } 1037 1038 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 1039 /// which means that the class contains or inherits a pure virtual function. 1040 bool isAbstract() const { return data().Abstract; } 1041 1042 /// isStandardLayout - Whether this class has standard layout 1043 /// (C++ [class]p7) 1044 bool isStandardLayout() const { return data().IsStandardLayout; } 1045 1046 /// \brief Whether this class, or any of its class subobjects, contains a 1047 /// mutable field. 1048 bool hasMutableFields() const { return data().HasMutableFields; } 1049 1050 /// hasTrivialDefaultConstructor - Whether this class has a trivial default 1051 /// constructor (C++11 [class.ctor]p5). 1052 bool hasTrivialDefaultConstructor() const { 1053 return data().HasTrivialDefaultConstructor && 1054 (!data().UserDeclaredConstructor || 1055 data().DeclaredDefaultConstructor); 1056 } 1057 1058 /// hasConstexprNonCopyMoveConstructor - Whether this class has at least one 1059 /// constexpr constructor other than the copy or move constructors. 1060 bool hasConstexprNonCopyMoveConstructor() const { 1061 return data().HasConstexprNonCopyMoveConstructor || 1062 (!hasUserDeclaredConstructor() && 1063 defaultedDefaultConstructorIsConstexpr()); 1064 } 1065 1066 /// defaultedDefaultConstructorIsConstexpr - Whether a defaulted default 1067 /// constructor for this class would be constexpr. 1068 bool defaultedDefaultConstructorIsConstexpr() const { 1069 return data().DefaultedDefaultConstructorIsConstexpr; 1070 } 1071 1072 /// defaultedCopyConstructorIsConstexpr - Whether a defaulted copy 1073 /// constructor for this class would be constexpr. 1074 bool defaultedCopyConstructorIsConstexpr() const { 1075 return data().DefaultedCopyConstructorIsConstexpr; 1076 } 1077 1078 /// defaultedMoveConstructorIsConstexpr - Whether a defaulted move 1079 /// constructor for this class would be constexpr. 1080 bool defaultedMoveConstructorIsConstexpr() const { 1081 return data().DefaultedMoveConstructorIsConstexpr; 1082 } 1083 1084 /// hasConstexprDefaultConstructor - Whether this class has a constexpr 1085 /// default constructor. 1086 bool hasConstexprDefaultConstructor() const { 1087 return data().HasConstexprDefaultConstructor || 1088 (!data().UserDeclaredConstructor && 1089 data().DefaultedDefaultConstructorIsConstexpr && isLiteral()); 1090 } 1091 1092 /// hasConstexprCopyConstructor - Whether this class has a constexpr copy 1093 /// constructor. 1094 bool hasConstexprCopyConstructor() const { 1095 return data().HasConstexprCopyConstructor || 1096 (!data().DeclaredCopyConstructor && 1097 data().DefaultedCopyConstructorIsConstexpr && isLiteral()); 1098 } 1099 1100 /// hasConstexprMoveConstructor - Whether this class has a constexpr move 1101 /// constructor. 1102 bool hasConstexprMoveConstructor() const { 1103 return data().HasConstexprMoveConstructor || 1104 (needsImplicitMoveConstructor() && 1105 data().DefaultedMoveConstructorIsConstexpr && isLiteral()); 1106 } 1107 1108 // hasTrivialCopyConstructor - Whether this class has a trivial copy 1109 // constructor (C++ [class.copy]p6, C++0x [class.copy]p13) 1110 bool hasTrivialCopyConstructor() const { 1111 return data().HasTrivialCopyConstructor; 1112 } 1113 1114 // hasTrivialMoveConstructor - Whether this class has a trivial move 1115 // constructor (C++0x [class.copy]p13) 1116 bool hasTrivialMoveConstructor() const { 1117 return data().HasTrivialMoveConstructor; 1118 } 1119 1120 // hasTrivialCopyAssignment - Whether this class has a trivial copy 1121 // assignment operator (C++ [class.copy]p11, C++0x [class.copy]p27) 1122 bool hasTrivialCopyAssignment() const { 1123 return data().HasTrivialCopyAssignment; 1124 } 1125 1126 // hasTrivialMoveAssignment - Whether this class has a trivial move 1127 // assignment operator (C++0x [class.copy]p27) 1128 bool hasTrivialMoveAssignment() const { 1129 return data().HasTrivialMoveAssignment; 1130 } 1131 1132 // hasTrivialDestructor - Whether this class has a trivial destructor 1133 // (C++ [class.dtor]p3) 1134 bool hasTrivialDestructor() const { return data().HasTrivialDestructor; } 1135 1136 // hasIrrelevantDestructor - Whether this class has a destructor which has no 1137 // semantic effect. Any such destructor will be trivial, public, defaulted 1138 // and not deleted. 1139 bool hasIrrelevantDestructor() const { 1140 return hasTrivialDestructor() && !hasUserDeclaredDestructor(); 1141 } 1142 1143 // hasNonLiteralTypeFieldsOrBases - Whether this class has a non-literal or 1144 // volatile type non-static data member or base class. 1145 bool hasNonLiteralTypeFieldsOrBases() const { 1146 return data().HasNonLiteralTypeFieldsOrBases; 1147 } 1148 1149 // isTriviallyCopyable - Whether this class is considered trivially copyable 1150 // (C++0x [class]p6). 1151 bool isTriviallyCopyable() const; 1152 1153 // isTrivial - Whether this class is considered trivial 1154 // 1155 // C++0x [class]p6 1156 // A trivial class is a class that has a trivial default constructor and 1157 // is trivially copiable. 1158 bool isTrivial() const { 1159 return isTriviallyCopyable() && hasTrivialDefaultConstructor(); 1160 } 1161 1162 // isLiteral - Whether this class is a literal type. 1163 // 1164 // C++11 [basic.types]p10 1165 // A class type that has all the following properties: 1166 // -- it has a trivial destructor 1167 // -- every constructor call and full-expression in the 1168 // brace-or-equal-intializers for non-static data members (if any) is 1169 // a constant expression. 1170 // -- it is an aggregate type or has at least one constexpr constructor or 1171 // constructor template that is not a copy or move constructor, and 1172 // -- all of its non-static data members and base classes are of literal 1173 // types 1174 // 1175 // We resolve DR1361 by ignoring the second bullet. We resolve DR1452 by 1176 // treating types with trivial default constructors as literal types. 1177 bool isLiteral() const { 1178 return hasTrivialDestructor() && 1179 (isAggregate() || hasConstexprNonCopyMoveConstructor() || 1180 hasTrivialDefaultConstructor()) && 1181 !hasNonLiteralTypeFieldsOrBases(); 1182 } 1183 1184 /// \brief If this record is an instantiation of a member class, 1185 /// retrieves the member class from which it was instantiated. 1186 /// 1187 /// This routine will return non-NULL for (non-templated) member 1188 /// classes of class templates. For example, given: 1189 /// 1190 /// \code 1191 /// template<typename T> 1192 /// struct X { 1193 /// struct A { }; 1194 /// }; 1195 /// \endcode 1196 /// 1197 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 1198 /// whose parent is the class template specialization X<int>. For 1199 /// this declaration, getInstantiatedFromMemberClass() will return 1200 /// the CXXRecordDecl X<T>::A. When a complete definition of 1201 /// X<int>::A is required, it will be instantiated from the 1202 /// declaration returned by getInstantiatedFromMemberClass(). 1203 CXXRecordDecl *getInstantiatedFromMemberClass() const; 1204 1205 /// \brief If this class is an instantiation of a member class of a 1206 /// class template specialization, retrieves the member specialization 1207 /// information. 1208 MemberSpecializationInfo *getMemberSpecializationInfo() const; 1209 1210 /// \brief Specify that this record is an instantiation of the 1211 /// member class RD. 1212 void setInstantiationOfMemberClass(CXXRecordDecl *RD, 1213 TemplateSpecializationKind TSK); 1214 1215 /// \brief Retrieves the class template that is described by this 1216 /// class declaration. 1217 /// 1218 /// Every class template is represented as a ClassTemplateDecl and a 1219 /// CXXRecordDecl. The former contains template properties (such as 1220 /// the template parameter lists) while the latter contains the 1221 /// actual description of the template's 1222 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 1223 /// CXXRecordDecl that from a ClassTemplateDecl, while 1224 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 1225 /// a CXXRecordDecl. 1226 ClassTemplateDecl *getDescribedClassTemplate() const { 1227 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 1228 } 1229 1230 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 1231 TemplateOrInstantiation = Template; 1232 } 1233 1234 /// \brief Determine whether this particular class is a specialization or 1235 /// instantiation of a class template or member class of a class template, 1236 /// and how it was instantiated or specialized. 1237 TemplateSpecializationKind getTemplateSpecializationKind() const; 1238 1239 /// \brief Set the kind of specialization or template instantiation this is. 1240 void setTemplateSpecializationKind(TemplateSpecializationKind TSK); 1241 1242 /// getDestructor - Returns the destructor decl for this class. 1243 CXXDestructorDecl *getDestructor() const; 1244 1245 /// isLocalClass - If the class is a local class [class.local], returns 1246 /// the enclosing function declaration. 1247 const FunctionDecl *isLocalClass() const { 1248 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) 1249 return RD->isLocalClass(); 1250 1251 return dyn_cast<FunctionDecl>(getDeclContext()); 1252 } 1253 1254 /// \brief Determine whether this class is derived from the class \p Base. 1255 /// 1256 /// This routine only determines whether this class is derived from \p Base, 1257 /// but does not account for factors that may make a Derived -> Base class 1258 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 1259 /// base class subobjects. 1260 /// 1261 /// \param Base the base class we are searching for. 1262 /// 1263 /// \returns true if this class is derived from Base, false otherwise. 1264 bool isDerivedFrom(const CXXRecordDecl *Base) const; 1265 1266 /// \brief Determine whether this class is derived from the type \p Base. 1267 /// 1268 /// This routine only determines whether this class is derived from \p Base, 1269 /// but does not account for factors that may make a Derived -> Base class 1270 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 1271 /// base class subobjects. 1272 /// 1273 /// \param Base the base class we are searching for. 1274 /// 1275 /// \param Paths will contain the paths taken from the current class to the 1276 /// given \p Base class. 1277 /// 1278 /// \returns true if this class is derived from Base, false otherwise. 1279 /// 1280 /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than 1281 /// tangling input and output in \p Paths 1282 bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const; 1283 1284 /// \brief Determine whether this class is virtually derived from 1285 /// the class \p Base. 1286 /// 1287 /// This routine only determines whether this class is virtually 1288 /// derived from \p Base, but does not account for factors that may 1289 /// make a Derived -> Base class ill-formed, such as 1290 /// private/protected inheritance or multiple, ambiguous base class 1291 /// subobjects. 1292 /// 1293 /// \param Base the base class we are searching for. 1294 /// 1295 /// \returns true if this class is virtually derived from Base, 1296 /// false otherwise. 1297 bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const; 1298 1299 /// \brief Determine whether this class is provably not derived from 1300 /// the type \p Base. 1301 bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const; 1302 1303 /// \brief Function type used by forallBases() as a callback. 1304 /// 1305 /// \param Base the definition of the base class 1306 /// 1307 /// \returns true if this base matched the search criteria 1308 typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition, 1309 void *UserData); 1310 1311 /// \brief Determines if the given callback holds for all the direct 1312 /// or indirect base classes of this type. 1313 /// 1314 /// The class itself does not count as a base class. This routine 1315 /// returns false if the class has non-computable base classes. 1316 /// 1317 /// \param AllowShortCircuit if false, forces the callback to be called 1318 /// for every base class, even if a dependent or non-matching base was 1319 /// found. 1320 bool forallBases(ForallBasesCallback *BaseMatches, void *UserData, 1321 bool AllowShortCircuit = true) const; 1322 1323 /// \brief Function type used by lookupInBases() to determine whether a 1324 /// specific base class subobject matches the lookup criteria. 1325 /// 1326 /// \param Specifier the base-class specifier that describes the inheritance 1327 /// from the base class we are trying to match. 1328 /// 1329 /// \param Path the current path, from the most-derived class down to the 1330 /// base named by the \p Specifier. 1331 /// 1332 /// \param UserData a single pointer to user-specified data, provided to 1333 /// lookupInBases(). 1334 /// 1335 /// \returns true if this base matched the search criteria, false otherwise. 1336 typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier, 1337 CXXBasePath &Path, 1338 void *UserData); 1339 1340 /// \brief Look for entities within the base classes of this C++ class, 1341 /// transitively searching all base class subobjects. 1342 /// 1343 /// This routine uses the callback function \p BaseMatches to find base 1344 /// classes meeting some search criteria, walking all base class subobjects 1345 /// and populating the given \p Paths structure with the paths through the 1346 /// inheritance hierarchy that resulted in a match. On a successful search, 1347 /// the \p Paths structure can be queried to retrieve the matching paths and 1348 /// to determine if there were any ambiguities. 1349 /// 1350 /// \param BaseMatches callback function used to determine whether a given 1351 /// base matches the user-defined search criteria. 1352 /// 1353 /// \param UserData user data pointer that will be provided to \p BaseMatches. 1354 /// 1355 /// \param Paths used to record the paths from this class to its base class 1356 /// subobjects that match the search criteria. 1357 /// 1358 /// \returns true if there exists any path from this class to a base class 1359 /// subobject that matches the search criteria. 1360 bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData, 1361 CXXBasePaths &Paths) const; 1362 1363 /// \brief Base-class lookup callback that determines whether the given 1364 /// base class specifier refers to a specific class declaration. 1365 /// 1366 /// This callback can be used with \c lookupInBases() to determine whether 1367 /// a given derived class has is a base class subobject of a particular type. 1368 /// The user data pointer should refer to the canonical CXXRecordDecl of the 1369 /// base class that we are searching for. 1370 static bool FindBaseClass(const CXXBaseSpecifier *Specifier, 1371 CXXBasePath &Path, void *BaseRecord); 1372 1373 /// \brief Base-class lookup callback that determines whether the 1374 /// given base class specifier refers to a specific class 1375 /// declaration and describes virtual derivation. 1376 /// 1377 /// This callback can be used with \c lookupInBases() to determine 1378 /// whether a given derived class has is a virtual base class 1379 /// subobject of a particular type. The user data pointer should 1380 /// refer to the canonical CXXRecordDecl of the base class that we 1381 /// are searching for. 1382 static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, 1383 CXXBasePath &Path, void *BaseRecord); 1384 1385 /// \brief Base-class lookup callback that determines whether there exists 1386 /// a tag with the given name. 1387 /// 1388 /// This callback can be used with \c lookupInBases() to find tag members 1389 /// of the given name within a C++ class hierarchy. The user data pointer 1390 /// is an opaque \c DeclarationName pointer. 1391 static bool FindTagMember(const CXXBaseSpecifier *Specifier, 1392 CXXBasePath &Path, void *Name); 1393 1394 /// \brief Base-class lookup callback that determines whether there exists 1395 /// a member with the given name. 1396 /// 1397 /// This callback can be used with \c lookupInBases() to find members 1398 /// of the given name within a C++ class hierarchy. The user data pointer 1399 /// is an opaque \c DeclarationName pointer. 1400 static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier, 1401 CXXBasePath &Path, void *Name); 1402 1403 /// \brief Base-class lookup callback that determines whether there exists 1404 /// a member with the given name that can be used in a nested-name-specifier. 1405 /// 1406 /// This callback can be used with \c lookupInBases() to find membes of 1407 /// the given name within a C++ class hierarchy that can occur within 1408 /// nested-name-specifiers. 1409 static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, 1410 CXXBasePath &Path, 1411 void *UserData); 1412 1413 /// \brief Retrieve the final overriders for each virtual member 1414 /// function in the class hierarchy where this class is the 1415 /// most-derived class in the class hierarchy. 1416 void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const; 1417 1418 /// \brief Get the indirect primary bases for this class. 1419 void getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const; 1420 1421 /// viewInheritance - Renders and displays an inheritance diagram 1422 /// for this C++ class and all of its base classes (transitively) using 1423 /// GraphViz. 1424 void viewInheritance(ASTContext& Context) const; 1425 1426 /// MergeAccess - Calculates the access of a decl that is reached 1427 /// along a path. 1428 static AccessSpecifier MergeAccess(AccessSpecifier PathAccess, 1429 AccessSpecifier DeclAccess) { 1430 assert(DeclAccess != AS_none); 1431 if (DeclAccess == AS_private) return AS_none; 1432 return (PathAccess > DeclAccess ? PathAccess : DeclAccess); 1433 } 1434 1435 /// \brief Indicates that the definition of this class is now complete. 1436 virtual void completeDefinition(); 1437 1438 /// \brief Indicates that the definition of this class is now complete, 1439 /// and provides a final overrider map to help determine 1440 /// 1441 /// \param FinalOverriders The final overrider map for this class, which can 1442 /// be provided as an optimization for abstract-class checking. If NULL, 1443 /// final overriders will be computed if they are needed to complete the 1444 /// definition. 1445 void completeDefinition(CXXFinalOverriderMap *FinalOverriders); 1446 1447 /// \brief Determine whether this class may end up being abstract, even though 1448 /// it is not yet known to be abstract. 1449 /// 1450 /// \returns true if this class is not known to be abstract but has any 1451 /// base classes that are abstract. In this case, \c completeDefinition() 1452 /// will need to compute final overriders to determine whether the class is 1453 /// actually abstract. 1454 bool mayBeAbstract() const; 1455 1456 /// \brief If this is the closure type of a lambda expression, retrieve the 1457 /// number to be used for name mangling in the Itanium C++ ABI. 1458 /// 1459 /// Zero indicates that this closure type has internal linkage, so the 1460 /// mangling number does not matter, while a non-zero value indicates which 1461 /// lambda expression this is in this particular context. 1462 unsigned getLambdaManglingNumber() const { 1463 assert(isLambda() && "Not a lambda closure type!"); 1464 return getLambdaData().ManglingNumber; 1465 } 1466 1467 /// \brief Retrieve the declaration that provides additional context for a 1468 /// lambda, when the normal declaration context is not specific enough. 1469 /// 1470 /// Certain contexts (default arguments of in-class function parameters and 1471 /// the initializers of data members) have separate name mangling rules for 1472 /// lambdas within the Itanium C++ ABI. For these cases, this routine provides 1473 /// the declaration in which the lambda occurs, e.g., the function parameter 1474 /// or the non-static data member. Otherwise, it returns NULL to imply that 1475 /// the declaration context suffices. 1476 Decl *getLambdaContextDecl() const { 1477 assert(isLambda() && "Not a lambda closure type!"); 1478 return getLambdaData().ContextDecl; 1479 } 1480 1481 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1482 static bool classofKind(Kind K) { 1483 return K >= firstCXXRecord && K <= lastCXXRecord; 1484 } 1485 static bool classof(const CXXRecordDecl *D) { return true; } 1486 static bool classof(const ClassTemplateSpecializationDecl *D) { 1487 return true; 1488 } 1489 1490 friend class ASTDeclReader; 1491 friend class ASTDeclWriter; 1492 friend class ASTReader; 1493 friend class ASTWriter; 1494}; 1495 1496/// CXXMethodDecl - Represents a static or instance method of a 1497/// struct/union/class. 1498class CXXMethodDecl : public FunctionDecl { 1499 virtual void anchor(); 1500protected: 1501 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation StartLoc, 1502 const DeclarationNameInfo &NameInfo, 1503 QualType T, TypeSourceInfo *TInfo, 1504 bool isStatic, StorageClass SCAsWritten, bool isInline, 1505 bool isConstexpr, SourceLocation EndLocation) 1506 : FunctionDecl(DK, RD, StartLoc, NameInfo, T, TInfo, 1507 (isStatic ? SC_Static : SC_None), 1508 SCAsWritten, isInline, isConstexpr) { 1509 if (EndLocation.isValid()) 1510 setRangeEnd(EndLocation); 1511 } 1512 1513public: 1514 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1515 SourceLocation StartLoc, 1516 const DeclarationNameInfo &NameInfo, 1517 QualType T, TypeSourceInfo *TInfo, 1518 bool isStatic, 1519 StorageClass SCAsWritten, 1520 bool isInline, 1521 bool isConstexpr, 1522 SourceLocation EndLocation); 1523 1524 static CXXMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1525 1526 bool isStatic() const { return getStorageClass() == SC_Static; } 1527 bool isInstance() const { return !isStatic(); } 1528 1529 bool isVirtual() const { 1530 CXXMethodDecl *CD = 1531 cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl()); 1532 1533 if (CD->isVirtualAsWritten()) 1534 return true; 1535 1536 return (CD->begin_overridden_methods() != CD->end_overridden_methods()); 1537 } 1538 1539 /// \brief Determine whether this is a usual deallocation function 1540 /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded 1541 /// delete or delete[] operator with a particular signature. 1542 bool isUsualDeallocationFunction() const; 1543 1544 /// \brief Determine whether this is a copy-assignment operator, regardless 1545 /// of whether it was declared implicitly or explicitly. 1546 bool isCopyAssignmentOperator() const; 1547 1548 /// \brief Determine whether this is a move assignment operator. 1549 bool isMoveAssignmentOperator() const; 1550 1551 const CXXMethodDecl *getCanonicalDecl() const { 1552 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1553 } 1554 CXXMethodDecl *getCanonicalDecl() { 1555 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1556 } 1557 1558 /// isUserProvided - True if it is either an implicit constructor or 1559 /// if it was defaulted or deleted on first declaration. 1560 bool isUserProvided() const { 1561 return !(isDeleted() || getCanonicalDecl()->isDefaulted()); 1562 } 1563 1564 /// 1565 void addOverriddenMethod(const CXXMethodDecl *MD); 1566 1567 typedef const CXXMethodDecl ** method_iterator; 1568 1569 method_iterator begin_overridden_methods() const; 1570 method_iterator end_overridden_methods() const; 1571 unsigned size_overridden_methods() const; 1572 1573 /// getParent - Returns the parent of this method declaration, which 1574 /// is the class in which this method is defined. 1575 const CXXRecordDecl *getParent() const { 1576 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 1577 } 1578 1579 /// getParent - Returns the parent of this method declaration, which 1580 /// is the class in which this method is defined. 1581 CXXRecordDecl *getParent() { 1582 return const_cast<CXXRecordDecl *>( 1583 cast<CXXRecordDecl>(FunctionDecl::getParent())); 1584 } 1585 1586 /// getThisType - Returns the type of 'this' pointer. 1587 /// Should only be called for instance methods. 1588 QualType getThisType(ASTContext &C) const; 1589 1590 unsigned getTypeQualifiers() const { 1591 return getType()->getAs<FunctionProtoType>()->getTypeQuals(); 1592 } 1593 1594 /// \brief Retrieve the ref-qualifier associated with this method. 1595 /// 1596 /// In the following example, \c f() has an lvalue ref-qualifier, \c g() 1597 /// has an rvalue ref-qualifier, and \c h() has no ref-qualifier. 1598 /// \code 1599 /// struct X { 1600 /// void f() &; 1601 /// void g() &&; 1602 /// void h(); 1603 /// }; 1604 /// \endcode 1605 RefQualifierKind getRefQualifier() const { 1606 return getType()->getAs<FunctionProtoType>()->getRefQualifier(); 1607 } 1608 1609 bool hasInlineBody() const; 1610 1611 /// \brief Determine whether this is a lambda closure type's static member 1612 /// function that is used for the result of the lambda's conversion to 1613 /// function pointer (for a lambda with no captures). 1614 /// 1615 /// The function itself, if used, will have a placeholder body that will be 1616 /// supplied by IR generation to either forward to the function call operator 1617 /// or clone the function call operator. 1618 bool isLambdaStaticInvoker() const; 1619 1620 // Implement isa/cast/dyncast/etc. 1621 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1622 static bool classof(const CXXMethodDecl *D) { return true; } 1623 static bool classofKind(Kind K) { 1624 return K >= firstCXXMethod && K <= lastCXXMethod; 1625 } 1626}; 1627 1628/// CXXCtorInitializer - Represents a C++ base or member 1629/// initializer, which is part of a constructor initializer that 1630/// initializes one non-static member variable or one base class. For 1631/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 1632/// initializers: 1633/// 1634/// @code 1635/// class A { }; 1636/// class B : public A { 1637/// float f; 1638/// public: 1639/// B(A& a) : A(a), f(3.14159) { } 1640/// }; 1641/// @endcode 1642class CXXCtorInitializer { 1643 /// \brief Either the base class name/delegating constructor type (stored as 1644 /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field 1645 /// (IndirectFieldDecl*) being initialized. 1646 llvm::PointerUnion3<TypeSourceInfo *, FieldDecl *, IndirectFieldDecl *> 1647 Initializee; 1648 1649 /// \brief The source location for the field name or, for a base initializer 1650 /// pack expansion, the location of the ellipsis. In the case of a delegating 1651 /// constructor, it will still include the type's source location as the 1652 /// Initializee points to the CXXConstructorDecl (to allow loop detection). 1653 SourceLocation MemberOrEllipsisLocation; 1654 1655 /// \brief The argument used to initialize the base or member, which may 1656 /// end up constructing an object (when multiple arguments are involved). 1657 /// If 0, this is a field initializer, and the in-class member initializer 1658 /// will be used. 1659 Stmt *Init; 1660 1661 /// LParenLoc - Location of the left paren of the ctor-initializer. 1662 SourceLocation LParenLoc; 1663 1664 /// RParenLoc - Location of the right paren of the ctor-initializer. 1665 SourceLocation RParenLoc; 1666 1667 /// \brief If the initializee is a type, whether that type makes this 1668 /// a delegating initialization. 1669 bool IsDelegating : 1; 1670 1671 /// IsVirtual - If the initializer is a base initializer, this keeps track 1672 /// of whether the base is virtual or not. 1673 bool IsVirtual : 1; 1674 1675 /// IsWritten - Whether or not the initializer is explicitly written 1676 /// in the sources. 1677 bool IsWritten : 1; 1678 1679 /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this 1680 /// number keeps track of the textual order of this initializer in the 1681 /// original sources, counting from 0; otherwise, if IsWritten is false, 1682 /// it stores the number of array index variables stored after this 1683 /// object in memory. 1684 unsigned SourceOrderOrNumArrayIndices : 13; 1685 1686 CXXCtorInitializer(ASTContext &Context, FieldDecl *Member, 1687 SourceLocation MemberLoc, SourceLocation L, Expr *Init, 1688 SourceLocation R, VarDecl **Indices, unsigned NumIndices); 1689 1690public: 1691 /// CXXCtorInitializer - Creates a new base-class initializer. 1692 explicit 1693 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, bool IsVirtual, 1694 SourceLocation L, Expr *Init, SourceLocation R, 1695 SourceLocation EllipsisLoc); 1696 1697 /// CXXCtorInitializer - Creates a new member initializer. 1698 explicit 1699 CXXCtorInitializer(ASTContext &Context, FieldDecl *Member, 1700 SourceLocation MemberLoc, SourceLocation L, Expr *Init, 1701 SourceLocation R); 1702 1703 /// CXXCtorInitializer - Creates a new anonymous field initializer. 1704 explicit 1705 CXXCtorInitializer(ASTContext &Context, IndirectFieldDecl *Member, 1706 SourceLocation MemberLoc, SourceLocation L, Expr *Init, 1707 SourceLocation R); 1708 1709 /// CXXCtorInitializer - Creates a new delegating Initializer. 1710 explicit 1711 CXXCtorInitializer(ASTContext &Context, TypeSourceInfo *TInfo, 1712 SourceLocation L, Expr *Init, SourceLocation R); 1713 1714 /// \brief Creates a new member initializer that optionally contains 1715 /// array indices used to describe an elementwise initialization. 1716 static CXXCtorInitializer *Create(ASTContext &Context, FieldDecl *Member, 1717 SourceLocation MemberLoc, SourceLocation L, 1718 Expr *Init, SourceLocation R, 1719 VarDecl **Indices, unsigned NumIndices); 1720 1721 /// isBaseInitializer - Returns true when this initializer is 1722 /// initializing a base class. 1723 bool isBaseInitializer() const { 1724 return Initializee.is<TypeSourceInfo*>() && !IsDelegating; 1725 } 1726 1727 /// isMemberInitializer - Returns true when this initializer is 1728 /// initializing a non-static data member. 1729 bool isMemberInitializer() const { return Initializee.is<FieldDecl*>(); } 1730 1731 bool isAnyMemberInitializer() const { 1732 return isMemberInitializer() || isIndirectMemberInitializer(); 1733 } 1734 1735 bool isIndirectMemberInitializer() const { 1736 return Initializee.is<IndirectFieldDecl*>(); 1737 } 1738 1739 /// isInClassMemberInitializer - Returns true when this initializer is an 1740 /// implicit ctor initializer generated for a field with an initializer 1741 /// defined on the member declaration. 1742 bool isInClassMemberInitializer() const { 1743 return !Init; 1744 } 1745 1746 /// isDelegatingInitializer - Returns true when this initializer is creating 1747 /// a delegating constructor. 1748 bool isDelegatingInitializer() const { 1749 return Initializee.is<TypeSourceInfo*>() && IsDelegating; 1750 } 1751 1752 /// \brief Determine whether this initializer is a pack expansion. 1753 bool isPackExpansion() const { 1754 return isBaseInitializer() && MemberOrEllipsisLocation.isValid(); 1755 } 1756 1757 // \brief For a pack expansion, returns the location of the ellipsis. 1758 SourceLocation getEllipsisLoc() const { 1759 assert(isPackExpansion() && "Initializer is not a pack expansion"); 1760 return MemberOrEllipsisLocation; 1761 } 1762 1763 /// If this is a base class initializer, returns the type of the 1764 /// base class with location information. Otherwise, returns an NULL 1765 /// type location. 1766 TypeLoc getBaseClassLoc() const; 1767 1768 /// If this is a base class initializer, returns the type of the base class. 1769 /// Otherwise, returns NULL. 1770 const Type *getBaseClass() const; 1771 1772 /// Returns whether the base is virtual or not. 1773 bool isBaseVirtual() const { 1774 assert(isBaseInitializer() && "Must call this on base initializer!"); 1775 1776 return IsVirtual; 1777 } 1778 1779 /// \brief Returns the declarator information for a base class or delegating 1780 /// initializer. 1781 TypeSourceInfo *getTypeSourceInfo() const { 1782 return Initializee.dyn_cast<TypeSourceInfo *>(); 1783 } 1784 1785 /// getMember - If this is a member initializer, returns the 1786 /// declaration of the non-static data member being 1787 /// initialized. Otherwise, returns NULL. 1788 FieldDecl *getMember() const { 1789 if (isMemberInitializer()) 1790 return Initializee.get<FieldDecl*>(); 1791 return 0; 1792 } 1793 FieldDecl *getAnyMember() const { 1794 if (isMemberInitializer()) 1795 return Initializee.get<FieldDecl*>(); 1796 if (isIndirectMemberInitializer()) 1797 return Initializee.get<IndirectFieldDecl*>()->getAnonField(); 1798 return 0; 1799 } 1800 1801 IndirectFieldDecl *getIndirectMember() const { 1802 if (isIndirectMemberInitializer()) 1803 return Initializee.get<IndirectFieldDecl*>(); 1804 return 0; 1805 } 1806 1807 SourceLocation getMemberLocation() const { 1808 return MemberOrEllipsisLocation; 1809 } 1810 1811 /// \brief Determine the source location of the initializer. 1812 SourceLocation getSourceLocation() const; 1813 1814 /// \brief Determine the source range covering the entire initializer. 1815 SourceRange getSourceRange() const; 1816 1817 /// isWritten - Returns true if this initializer is explicitly written 1818 /// in the source code. 1819 bool isWritten() const { return IsWritten; } 1820 1821 /// \brief Return the source position of the initializer, counting from 0. 1822 /// If the initializer was implicit, -1 is returned. 1823 int getSourceOrder() const { 1824 return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1; 1825 } 1826 1827 /// \brief Set the source order of this initializer. This method can only 1828 /// be called once for each initializer; it cannot be called on an 1829 /// initializer having a positive number of (implicit) array indices. 1830 void setSourceOrder(int pos) { 1831 assert(!IsWritten && 1832 "calling twice setSourceOrder() on the same initializer"); 1833 assert(SourceOrderOrNumArrayIndices == 0 && 1834 "setSourceOrder() used when there are implicit array indices"); 1835 assert(pos >= 0 && 1836 "setSourceOrder() used to make an initializer implicit"); 1837 IsWritten = true; 1838 SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos); 1839 } 1840 1841 SourceLocation getLParenLoc() const { return LParenLoc; } 1842 SourceLocation getRParenLoc() const { return RParenLoc; } 1843 1844 /// \brief Determine the number of implicit array indices used while 1845 /// described an array member initialization. 1846 unsigned getNumArrayIndices() const { 1847 return IsWritten ? 0 : SourceOrderOrNumArrayIndices; 1848 } 1849 1850 /// \brief Retrieve a particular array index variable used to 1851 /// describe an array member initialization. 1852 VarDecl *getArrayIndex(unsigned I) { 1853 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1854 return reinterpret_cast<VarDecl **>(this + 1)[I]; 1855 } 1856 const VarDecl *getArrayIndex(unsigned I) const { 1857 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1858 return reinterpret_cast<const VarDecl * const *>(this + 1)[I]; 1859 } 1860 void setArrayIndex(unsigned I, VarDecl *Index) { 1861 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1862 reinterpret_cast<VarDecl **>(this + 1)[I] = Index; 1863 } 1864 ArrayRef<VarDecl *> getArrayIndexes() { 1865 assert(getNumArrayIndices() != 0 && "Getting indexes for non-array init"); 1866 return ArrayRef<VarDecl *>(reinterpret_cast<VarDecl **>(this + 1), 1867 getNumArrayIndices()); 1868 } 1869 1870 /// \brief Get the initializer. This is 0 if this is an in-class initializer 1871 /// for a non-static data member which has not yet been parsed. 1872 Expr *getInit() const { 1873 if (!Init) 1874 return getAnyMember()->getInClassInitializer(); 1875 1876 return static_cast<Expr*>(Init); 1877 } 1878}; 1879 1880/// CXXConstructorDecl - Represents a C++ constructor within a 1881/// class. For example: 1882/// 1883/// @code 1884/// class X { 1885/// public: 1886/// explicit X(int); // represented by a CXXConstructorDecl. 1887/// }; 1888/// @endcode 1889class CXXConstructorDecl : public CXXMethodDecl { 1890 virtual void anchor(); 1891 /// IsExplicitSpecified - Whether this constructor declaration has the 1892 /// 'explicit' keyword specified. 1893 bool IsExplicitSpecified : 1; 1894 1895 /// ImplicitlyDefined - Whether this constructor was implicitly 1896 /// defined by the compiler. When false, the constructor was defined 1897 /// by the user. In C++03, this flag will have the same value as 1898 /// Implicit. In C++0x, however, a constructor that is 1899 /// explicitly defaulted (i.e., defined with " = default") will have 1900 /// @c !Implicit && ImplicitlyDefined. 1901 bool ImplicitlyDefined : 1; 1902 1903 /// Support for base and member initializers. 1904 /// CtorInitializers - The arguments used to initialize the base 1905 /// or member. 1906 CXXCtorInitializer **CtorInitializers; 1907 unsigned NumCtorInitializers; 1908 1909 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation StartLoc, 1910 const DeclarationNameInfo &NameInfo, 1911 QualType T, TypeSourceInfo *TInfo, 1912 bool isExplicitSpecified, bool isInline, 1913 bool isImplicitlyDeclared, bool isConstexpr) 1914 : CXXMethodDecl(CXXConstructor, RD, StartLoc, NameInfo, T, TInfo, false, 1915 SC_None, isInline, isConstexpr, SourceLocation()), 1916 IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false), 1917 CtorInitializers(0), NumCtorInitializers(0) { 1918 setImplicit(isImplicitlyDeclared); 1919 } 1920 1921public: 1922 static CXXConstructorDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1923 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1924 SourceLocation StartLoc, 1925 const DeclarationNameInfo &NameInfo, 1926 QualType T, TypeSourceInfo *TInfo, 1927 bool isExplicit, 1928 bool isInline, bool isImplicitlyDeclared, 1929 bool isConstexpr); 1930 1931 /// isExplicitSpecified - Whether this constructor declaration has the 1932 /// 'explicit' keyword specified. 1933 bool isExplicitSpecified() const { return IsExplicitSpecified; } 1934 1935 /// isExplicit - Whether this constructor was marked "explicit" or not. 1936 bool isExplicit() const { 1937 return cast<CXXConstructorDecl>(getFirstDeclaration()) 1938 ->isExplicitSpecified(); 1939 } 1940 1941 /// isImplicitlyDefined - Whether this constructor was implicitly 1942 /// defined. If false, then this constructor was defined by the 1943 /// user. This operation can only be invoked if the constructor has 1944 /// already been defined. 1945 bool isImplicitlyDefined() const { 1946 assert(isThisDeclarationADefinition() && 1947 "Can only get the implicit-definition flag once the " 1948 "constructor has been defined"); 1949 return ImplicitlyDefined; 1950 } 1951 1952 /// setImplicitlyDefined - Set whether this constructor was 1953 /// implicitly defined or not. 1954 void setImplicitlyDefined(bool ID) { 1955 assert(isThisDeclarationADefinition() && 1956 "Can only set the implicit-definition flag once the constructor " 1957 "has been defined"); 1958 ImplicitlyDefined = ID; 1959 } 1960 1961 /// init_iterator - Iterates through the member/base initializer list. 1962 typedef CXXCtorInitializer **init_iterator; 1963 1964 /// init_const_iterator - Iterates through the memberbase initializer list. 1965 typedef CXXCtorInitializer * const * init_const_iterator; 1966 1967 /// init_begin() - Retrieve an iterator to the first initializer. 1968 init_iterator init_begin() { return CtorInitializers; } 1969 /// begin() - Retrieve an iterator to the first initializer. 1970 init_const_iterator init_begin() const { return CtorInitializers; } 1971 1972 /// init_end() - Retrieve an iterator past the last initializer. 1973 init_iterator init_end() { 1974 return CtorInitializers + NumCtorInitializers; 1975 } 1976 /// end() - Retrieve an iterator past the last initializer. 1977 init_const_iterator init_end() const { 1978 return CtorInitializers + NumCtorInitializers; 1979 } 1980 1981 typedef std::reverse_iterator<init_iterator> init_reverse_iterator; 1982 typedef std::reverse_iterator<init_const_iterator> 1983 init_const_reverse_iterator; 1984 1985 init_reverse_iterator init_rbegin() { 1986 return init_reverse_iterator(init_end()); 1987 } 1988 init_const_reverse_iterator init_rbegin() const { 1989 return init_const_reverse_iterator(init_end()); 1990 } 1991 1992 init_reverse_iterator init_rend() { 1993 return init_reverse_iterator(init_begin()); 1994 } 1995 init_const_reverse_iterator init_rend() const { 1996 return init_const_reverse_iterator(init_begin()); 1997 } 1998 1999 /// getNumArgs - Determine the number of arguments used to 2000 /// initialize the member or base. 2001 unsigned getNumCtorInitializers() const { 2002 return NumCtorInitializers; 2003 } 2004 2005 void setNumCtorInitializers(unsigned numCtorInitializers) { 2006 NumCtorInitializers = numCtorInitializers; 2007 } 2008 2009 void setCtorInitializers(CXXCtorInitializer ** initializers) { 2010 CtorInitializers = initializers; 2011 } 2012 2013 /// isDelegatingConstructor - Whether this constructor is a 2014 /// delegating constructor 2015 bool isDelegatingConstructor() const { 2016 return (getNumCtorInitializers() == 1) && 2017 CtorInitializers[0]->isDelegatingInitializer(); 2018 } 2019 2020 /// getTargetConstructor - When this constructor delegates to 2021 /// another, retrieve the target 2022 CXXConstructorDecl *getTargetConstructor() const; 2023 2024 /// isDefaultConstructor - Whether this constructor is a default 2025 /// constructor (C++ [class.ctor]p5), which can be used to 2026 /// default-initialize a class of this type. 2027 bool isDefaultConstructor() const; 2028 2029 /// isCopyConstructor - Whether this constructor is a copy 2030 /// constructor (C++ [class.copy]p2, which can be used to copy the 2031 /// class. @p TypeQuals will be set to the qualifiers on the 2032 /// argument type. For example, @p TypeQuals would be set to @c 2033 /// QualType::Const for the following copy constructor: 2034 /// 2035 /// @code 2036 /// class X { 2037 /// public: 2038 /// X(const X&); 2039 /// }; 2040 /// @endcode 2041 bool isCopyConstructor(unsigned &TypeQuals) const; 2042 2043 /// isCopyConstructor - Whether this constructor is a copy 2044 /// constructor (C++ [class.copy]p2, which can be used to copy the 2045 /// class. 2046 bool isCopyConstructor() const { 2047 unsigned TypeQuals = 0; 2048 return isCopyConstructor(TypeQuals); 2049 } 2050 2051 /// \brief Determine whether this constructor is a move constructor 2052 /// (C++0x [class.copy]p3), which can be used to move values of the class. 2053 /// 2054 /// \param TypeQuals If this constructor is a move constructor, will be set 2055 /// to the type qualifiers on the referent of the first parameter's type. 2056 bool isMoveConstructor(unsigned &TypeQuals) const; 2057 2058 /// \brief Determine whether this constructor is a move constructor 2059 /// (C++0x [class.copy]p3), which can be used to move values of the class. 2060 bool isMoveConstructor() const { 2061 unsigned TypeQuals = 0; 2062 return isMoveConstructor(TypeQuals); 2063 } 2064 2065 /// \brief Determine whether this is a copy or move constructor. 2066 /// 2067 /// \param TypeQuals Will be set to the type qualifiers on the reference 2068 /// parameter, if in fact this is a copy or move constructor. 2069 bool isCopyOrMoveConstructor(unsigned &TypeQuals) const; 2070 2071 /// \brief Determine whether this a copy or move constructor. 2072 bool isCopyOrMoveConstructor() const { 2073 unsigned Quals; 2074 return isCopyOrMoveConstructor(Quals); 2075 } 2076 2077 /// isConvertingConstructor - Whether this constructor is a 2078 /// converting constructor (C++ [class.conv.ctor]), which can be 2079 /// used for user-defined conversions. 2080 bool isConvertingConstructor(bool AllowExplicit) const; 2081 2082 /// \brief Determine whether this is a member template specialization that 2083 /// would copy the object to itself. Such constructors are never used to copy 2084 /// an object. 2085 bool isSpecializationCopyingObject() const; 2086 2087 /// \brief Get the constructor that this inheriting constructor is based on. 2088 const CXXConstructorDecl *getInheritedConstructor() const; 2089 2090 /// \brief Set the constructor that this inheriting constructor is based on. 2091 void setInheritedConstructor(const CXXConstructorDecl *BaseCtor); 2092 2093 const CXXConstructorDecl *getCanonicalDecl() const { 2094 return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl()); 2095 } 2096 CXXConstructorDecl *getCanonicalDecl() { 2097 return cast<CXXConstructorDecl>(FunctionDecl::getCanonicalDecl()); 2098 } 2099 2100 // Implement isa/cast/dyncast/etc. 2101 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2102 static bool classof(const CXXConstructorDecl *D) { return true; } 2103 static bool classofKind(Kind K) { return K == CXXConstructor; } 2104 2105 friend class ASTDeclReader; 2106 friend class ASTDeclWriter; 2107}; 2108 2109/// CXXDestructorDecl - Represents a C++ destructor within a 2110/// class. For example: 2111/// 2112/// @code 2113/// class X { 2114/// public: 2115/// ~X(); // represented by a CXXDestructorDecl. 2116/// }; 2117/// @endcode 2118class CXXDestructorDecl : public CXXMethodDecl { 2119 virtual void anchor(); 2120 /// ImplicitlyDefined - Whether this destructor was implicitly 2121 /// defined by the compiler. When false, the destructor was defined 2122 /// by the user. In C++03, this flag will have the same value as 2123 /// Implicit. In C++0x, however, a destructor that is 2124 /// explicitly defaulted (i.e., defined with " = default") will have 2125 /// @c !Implicit && ImplicitlyDefined. 2126 bool ImplicitlyDefined : 1; 2127 2128 FunctionDecl *OperatorDelete; 2129 2130 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation StartLoc, 2131 const DeclarationNameInfo &NameInfo, 2132 QualType T, TypeSourceInfo *TInfo, 2133 bool isInline, bool isImplicitlyDeclared) 2134 : CXXMethodDecl(CXXDestructor, RD, StartLoc, NameInfo, T, TInfo, false, 2135 SC_None, isInline, /*isConstexpr=*/false, SourceLocation()), 2136 ImplicitlyDefined(false), OperatorDelete(0) { 2137 setImplicit(isImplicitlyDeclared); 2138 } 2139 2140public: 2141 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 2142 SourceLocation StartLoc, 2143 const DeclarationNameInfo &NameInfo, 2144 QualType T, TypeSourceInfo* TInfo, 2145 bool isInline, 2146 bool isImplicitlyDeclared); 2147 static CXXDestructorDecl *CreateDeserialized(ASTContext & C, unsigned ID); 2148 2149 /// isImplicitlyDefined - Whether this destructor was implicitly 2150 /// defined. If false, then this destructor was defined by the 2151 /// user. This operation can only be invoked if the destructor has 2152 /// already been defined. 2153 bool isImplicitlyDefined() const { 2154 assert(isThisDeclarationADefinition() && 2155 "Can only get the implicit-definition flag once the destructor has " 2156 "been defined"); 2157 return ImplicitlyDefined; 2158 } 2159 2160 /// setImplicitlyDefined - Set whether this destructor was 2161 /// implicitly defined or not. 2162 void setImplicitlyDefined(bool ID) { 2163 assert(isThisDeclarationADefinition() && 2164 "Can only set the implicit-definition flag once the destructor has " 2165 "been defined"); 2166 ImplicitlyDefined = ID; 2167 } 2168 2169 void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; } 2170 const FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 2171 2172 // Implement isa/cast/dyncast/etc. 2173 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2174 static bool classof(const CXXDestructorDecl *D) { return true; } 2175 static bool classofKind(Kind K) { return K == CXXDestructor; } 2176 2177 friend class ASTDeclReader; 2178 friend class ASTDeclWriter; 2179}; 2180 2181/// CXXConversionDecl - Represents a C++ conversion function within a 2182/// class. For example: 2183/// 2184/// @code 2185/// class X { 2186/// public: 2187/// operator bool(); 2188/// }; 2189/// @endcode 2190class CXXConversionDecl : public CXXMethodDecl { 2191 virtual void anchor(); 2192 /// IsExplicitSpecified - Whether this conversion function declaration is 2193 /// marked "explicit", meaning that it can only be applied when the user 2194 /// explicitly wrote a cast. This is a C++0x feature. 2195 bool IsExplicitSpecified : 1; 2196 2197 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation StartLoc, 2198 const DeclarationNameInfo &NameInfo, 2199 QualType T, TypeSourceInfo *TInfo, 2200 bool isInline, bool isExplicitSpecified, 2201 bool isConstexpr, SourceLocation EndLocation) 2202 : CXXMethodDecl(CXXConversion, RD, StartLoc, NameInfo, T, TInfo, false, 2203 SC_None, isInline, isConstexpr, EndLocation), 2204 IsExplicitSpecified(isExplicitSpecified) { } 2205 2206public: 2207 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 2208 SourceLocation StartLoc, 2209 const DeclarationNameInfo &NameInfo, 2210 QualType T, TypeSourceInfo *TInfo, 2211 bool isInline, bool isExplicit, 2212 bool isConstexpr, 2213 SourceLocation EndLocation); 2214 static CXXConversionDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2215 2216 /// IsExplicitSpecified - Whether this conversion function declaration is 2217 /// marked "explicit", meaning that it can only be applied when the user 2218 /// explicitly wrote a cast. This is a C++0x feature. 2219 bool isExplicitSpecified() const { return IsExplicitSpecified; } 2220 2221 /// isExplicit - Whether this is an explicit conversion operator 2222 /// (C++0x only). Explicit conversion operators are only considered 2223 /// when the user has explicitly written a cast. 2224 bool isExplicit() const { 2225 return cast<CXXConversionDecl>(getFirstDeclaration()) 2226 ->isExplicitSpecified(); 2227 } 2228 2229 /// getConversionType - Returns the type that this conversion 2230 /// function is converting to. 2231 QualType getConversionType() const { 2232 return getType()->getAs<FunctionType>()->getResultType(); 2233 } 2234 2235 /// \brief Determine whether this conversion function is a conversion from 2236 /// a lambda closure type to a block pointer. 2237 bool isLambdaToBlockPointerConversion() const; 2238 2239 /// \brief For an implicit conversion function that converts a lambda 2240 /// closure type to a block pointer, retrieve the expression used to 2241 /// copy the closure object into the block. 2242 Expr *getLambdaToBlockPointerCopyInit() const; 2243 2244 /// \brief Set the copy-initialization expression to be used when converting 2245 /// a lambda object to a block pointer. 2246 void setLambdaToBlockPointerCopyInit(Expr *Init); 2247 2248 // Implement isa/cast/dyncast/etc. 2249 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2250 static bool classof(const CXXConversionDecl *D) { return true; } 2251 static bool classofKind(Kind K) { return K == CXXConversion; } 2252 2253 friend class ASTDeclReader; 2254 friend class ASTDeclWriter; 2255}; 2256 2257/// LinkageSpecDecl - This represents a linkage specification. For example: 2258/// extern "C" void foo(); 2259/// 2260class LinkageSpecDecl : public Decl, public DeclContext { 2261 virtual void anchor(); 2262public: 2263 /// LanguageIDs - Used to represent the language in a linkage 2264 /// specification. The values are part of the serialization abi for 2265 /// ASTs and cannot be changed without altering that abi. To help 2266 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 2267 /// from the dwarf standard. 2268 enum LanguageIDs { 2269 lang_c = /* DW_LANG_C */ 0x0002, 2270 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 2271 }; 2272private: 2273 /// Language - The language for this linkage specification. 2274 LanguageIDs Language; 2275 /// ExternLoc - The source location for the extern keyword. 2276 SourceLocation ExternLoc; 2277 /// RBraceLoc - The source location for the right brace (if valid). 2278 SourceLocation RBraceLoc; 2279 2280 LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc, 2281 SourceLocation LangLoc, LanguageIDs lang, 2282 SourceLocation RBLoc) 2283 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec), 2284 Language(lang), ExternLoc(ExternLoc), RBraceLoc(RBLoc) { } 2285 2286public: 2287 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 2288 SourceLocation ExternLoc, 2289 SourceLocation LangLoc, LanguageIDs Lang, 2290 SourceLocation RBraceLoc = SourceLocation()); 2291 static LinkageSpecDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2292 2293 /// \brief Return the language specified by this linkage specification. 2294 LanguageIDs getLanguage() const { return Language; } 2295 /// \brief Set the language specified by this linkage specification. 2296 void setLanguage(LanguageIDs L) { Language = L; } 2297 2298 /// \brief Determines whether this linkage specification had braces in 2299 /// its syntactic form. 2300 bool hasBraces() const { return RBraceLoc.isValid(); } 2301 2302 SourceLocation getExternLoc() const { return ExternLoc; } 2303 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2304 void setExternLoc(SourceLocation L) { ExternLoc = L; } 2305 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } 2306 2307 SourceLocation getLocEnd() const { 2308 if (hasBraces()) 2309 return getRBraceLoc(); 2310 // No braces: get the end location of the (only) declaration in context 2311 // (if present). 2312 return decls_empty() ? getLocation() : decls_begin()->getLocEnd(); 2313 } 2314 2315 SourceRange getSourceRange() const { 2316 return SourceRange(ExternLoc, getLocEnd()); 2317 } 2318 2319 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2320 static bool classof(const LinkageSpecDecl *D) { return true; } 2321 static bool classofKind(Kind K) { return K == LinkageSpec; } 2322 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 2323 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 2324 } 2325 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 2326 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 2327 } 2328}; 2329 2330/// UsingDirectiveDecl - Represents C++ using-directive. For example: 2331/// 2332/// using namespace std; 2333/// 2334// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 2335// artificial names for all using-directives in order to store 2336// them in DeclContext effectively. 2337class UsingDirectiveDecl : public NamedDecl { 2338 virtual void anchor(); 2339 /// \brief The location of the "using" keyword. 2340 SourceLocation UsingLoc; 2341 2342 /// SourceLocation - Location of 'namespace' token. 2343 SourceLocation NamespaceLoc; 2344 2345 /// \brief The nested-name-specifier that precedes the namespace. 2346 NestedNameSpecifierLoc QualifierLoc; 2347 2348 /// NominatedNamespace - Namespace nominated by using-directive. 2349 NamedDecl *NominatedNamespace; 2350 2351 /// Enclosing context containing both using-directive and nominated 2352 /// namespace. 2353 DeclContext *CommonAncestor; 2354 2355 /// getUsingDirectiveName - Returns special DeclarationName used by 2356 /// using-directives. This is only used by DeclContext for storing 2357 /// UsingDirectiveDecls in its lookup structure. 2358 static DeclarationName getName() { 2359 return DeclarationName::getUsingDirectiveName(); 2360 } 2361 2362 UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc, 2363 SourceLocation NamespcLoc, 2364 NestedNameSpecifierLoc QualifierLoc, 2365 SourceLocation IdentLoc, 2366 NamedDecl *Nominated, 2367 DeclContext *CommonAncestor) 2368 : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc), 2369 NamespaceLoc(NamespcLoc), QualifierLoc(QualifierLoc), 2370 NominatedNamespace(Nominated), CommonAncestor(CommonAncestor) { } 2371 2372public: 2373 /// \brief Retrieve the nested-name-specifier that qualifies the 2374 /// name of the namespace, with source-location information. 2375 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2376 2377 /// \brief Retrieve the nested-name-specifier that qualifies the 2378 /// name of the namespace. 2379 NestedNameSpecifier *getQualifier() const { 2380 return QualifierLoc.getNestedNameSpecifier(); 2381 } 2382 2383 NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; } 2384 const NamedDecl *getNominatedNamespaceAsWritten() const { 2385 return NominatedNamespace; 2386 } 2387 2388 /// getNominatedNamespace - Returns namespace nominated by using-directive. 2389 NamespaceDecl *getNominatedNamespace(); 2390 2391 const NamespaceDecl *getNominatedNamespace() const { 2392 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 2393 } 2394 2395 /// \brief Returns the common ancestor context of this using-directive and 2396 /// its nominated namespace. 2397 DeclContext *getCommonAncestor() { return CommonAncestor; } 2398 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 2399 2400 /// \brief Return the location of the "using" keyword. 2401 SourceLocation getUsingLoc() const { return UsingLoc; } 2402 2403 // FIXME: Could omit 'Key' in name. 2404 /// getNamespaceKeyLocation - Returns location of namespace keyword. 2405 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 2406 2407 /// getIdentLocation - Returns location of identifier. 2408 SourceLocation getIdentLocation() const { return getLocation(); } 2409 2410 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 2411 SourceLocation UsingLoc, 2412 SourceLocation NamespaceLoc, 2413 NestedNameSpecifierLoc QualifierLoc, 2414 SourceLocation IdentLoc, 2415 NamedDecl *Nominated, 2416 DeclContext *CommonAncestor); 2417 static UsingDirectiveDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2418 2419 SourceRange getSourceRange() const { 2420 return SourceRange(UsingLoc, getLocation()); 2421 } 2422 2423 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2424 static bool classof(const UsingDirectiveDecl *D) { return true; } 2425 static bool classofKind(Kind K) { return K == UsingDirective; } 2426 2427 // Friend for getUsingDirectiveName. 2428 friend class DeclContext; 2429 2430 friend class ASTDeclReader; 2431}; 2432 2433/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 2434/// 2435/// @code 2436/// namespace Foo = Bar; 2437/// @endcode 2438class NamespaceAliasDecl : public NamedDecl { 2439 virtual void anchor(); 2440 2441 /// \brief The location of the "namespace" keyword. 2442 SourceLocation NamespaceLoc; 2443 2444 /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc. 2445 SourceLocation IdentLoc; 2446 2447 /// \brief The nested-name-specifier that precedes the namespace. 2448 NestedNameSpecifierLoc QualifierLoc; 2449 2450 /// Namespace - The Decl that this alias points to. Can either be a 2451 /// NamespaceDecl or a NamespaceAliasDecl. 2452 NamedDecl *Namespace; 2453 2454 NamespaceAliasDecl(DeclContext *DC, SourceLocation NamespaceLoc, 2455 SourceLocation AliasLoc, IdentifierInfo *Alias, 2456 NestedNameSpecifierLoc QualifierLoc, 2457 SourceLocation IdentLoc, NamedDecl *Namespace) 2458 : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), 2459 NamespaceLoc(NamespaceLoc), IdentLoc(IdentLoc), 2460 QualifierLoc(QualifierLoc), Namespace(Namespace) { } 2461 2462 friend class ASTDeclReader; 2463 2464public: 2465 /// \brief Retrieve the nested-name-specifier that qualifies the 2466 /// name of the namespace, with source-location information. 2467 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2468 2469 /// \brief Retrieve the nested-name-specifier that qualifies the 2470 /// name of the namespace. 2471 NestedNameSpecifier *getQualifier() const { 2472 return QualifierLoc.getNestedNameSpecifier(); 2473 } 2474 2475 /// \brief Retrieve the namespace declaration aliased by this directive. 2476 NamespaceDecl *getNamespace() { 2477 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 2478 return AD->getNamespace(); 2479 2480 return cast<NamespaceDecl>(Namespace); 2481 } 2482 2483 const NamespaceDecl *getNamespace() const { 2484 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 2485 } 2486 2487 /// Returns the location of the alias name, i.e. 'foo' in 2488 /// "namespace foo = ns::bar;". 2489 SourceLocation getAliasLoc() const { return getLocation(); } 2490 2491 /// Returns the location of the 'namespace' keyword. 2492 SourceLocation getNamespaceLoc() const { return NamespaceLoc; } 2493 2494 /// Returns the location of the identifier in the named namespace. 2495 SourceLocation getTargetNameLoc() const { return IdentLoc; } 2496 2497 /// \brief Retrieve the namespace that this alias refers to, which 2498 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 2499 NamedDecl *getAliasedNamespace() const { return Namespace; } 2500 2501 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 2502 SourceLocation NamespaceLoc, 2503 SourceLocation AliasLoc, 2504 IdentifierInfo *Alias, 2505 NestedNameSpecifierLoc QualifierLoc, 2506 SourceLocation IdentLoc, 2507 NamedDecl *Namespace); 2508 2509 static NamespaceAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2510 2511 virtual SourceRange getSourceRange() const { 2512 return SourceRange(NamespaceLoc, IdentLoc); 2513 } 2514 2515 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2516 static bool classof(const NamespaceAliasDecl *D) { return true; } 2517 static bool classofKind(Kind K) { return K == NamespaceAlias; } 2518}; 2519 2520/// UsingShadowDecl - Represents a shadow declaration introduced into 2521/// a scope by a (resolved) using declaration. For example, 2522/// 2523/// namespace A { 2524/// void foo(); 2525/// } 2526/// namespace B { 2527/// using A::foo(); // <- a UsingDecl 2528/// // Also creates a UsingShadowDecl for A::foo in B 2529/// } 2530/// 2531class UsingShadowDecl : public NamedDecl { 2532 virtual void anchor(); 2533 2534 /// The referenced declaration. 2535 NamedDecl *Underlying; 2536 2537 /// \brief The using declaration which introduced this decl or the next using 2538 /// shadow declaration contained in the aforementioned using declaration. 2539 NamedDecl *UsingOrNextShadow; 2540 friend class UsingDecl; 2541 2542 UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using, 2543 NamedDecl *Target) 2544 : NamedDecl(UsingShadow, DC, Loc, DeclarationName()), 2545 Underlying(Target), 2546 UsingOrNextShadow(reinterpret_cast<NamedDecl *>(Using)) { 2547 if (Target) { 2548 setDeclName(Target->getDeclName()); 2549 IdentifierNamespace = Target->getIdentifierNamespace(); 2550 } 2551 setImplicit(); 2552 } 2553 2554public: 2555 static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC, 2556 SourceLocation Loc, UsingDecl *Using, 2557 NamedDecl *Target) { 2558 return new (C) UsingShadowDecl(DC, Loc, Using, Target); 2559 } 2560 2561 static UsingShadowDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2562 2563 /// \brief Gets the underlying declaration which has been brought into the 2564 /// local scope. 2565 NamedDecl *getTargetDecl() const { return Underlying; } 2566 2567 /// \brief Sets the underlying declaration which has been brought into the 2568 /// local scope. 2569 void setTargetDecl(NamedDecl* ND) { 2570 assert(ND && "Target decl is null!"); 2571 Underlying = ND; 2572 IdentifierNamespace = ND->getIdentifierNamespace(); 2573 } 2574 2575 /// \brief Gets the using declaration to which this declaration is tied. 2576 UsingDecl *getUsingDecl() const; 2577 2578 /// \brief The next using shadow declaration contained in the shadow decl 2579 /// chain of the using declaration which introduced this decl. 2580 UsingShadowDecl *getNextUsingShadowDecl() const { 2581 return dyn_cast_or_null<UsingShadowDecl>(UsingOrNextShadow); 2582 } 2583 2584 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2585 static bool classof(const UsingShadowDecl *D) { return true; } 2586 static bool classofKind(Kind K) { return K == Decl::UsingShadow; } 2587 2588 friend class ASTDeclReader; 2589 friend class ASTDeclWriter; 2590}; 2591 2592/// UsingDecl - Represents a C++ using-declaration. For example: 2593/// using someNameSpace::someIdentifier; 2594class UsingDecl : public NamedDecl { 2595 virtual void anchor(); 2596 2597 /// \brief The source location of the "using" location itself. 2598 SourceLocation UsingLocation; 2599 2600 /// \brief The nested-name-specifier that precedes the name. 2601 NestedNameSpecifierLoc QualifierLoc; 2602 2603 /// DNLoc - Provides source/type location info for the 2604 /// declaration name embedded in the ValueDecl base class. 2605 DeclarationNameLoc DNLoc; 2606 2607 /// \brief The first shadow declaration of the shadow decl chain associated 2608 /// with this using declaration. The bool member of the pair store whether 2609 /// this decl has the 'typename' keyword. 2610 llvm::PointerIntPair<UsingShadowDecl *, 1, bool> FirstUsingShadow; 2611 2612 UsingDecl(DeclContext *DC, SourceLocation UL, 2613 NestedNameSpecifierLoc QualifierLoc, 2614 const DeclarationNameInfo &NameInfo, bool IsTypeNameArg) 2615 : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()), 2616 UsingLocation(UL), QualifierLoc(QualifierLoc), 2617 DNLoc(NameInfo.getInfo()), FirstUsingShadow(0, IsTypeNameArg) { 2618 } 2619 2620public: 2621 /// \brief Returns the source location of the "using" keyword. 2622 SourceLocation getUsingLocation() const { return UsingLocation; } 2623 2624 /// \brief Set the source location of the 'using' keyword. 2625 void setUsingLocation(SourceLocation L) { UsingLocation = L; } 2626 2627 /// \brief Retrieve the nested-name-specifier that qualifies the name, 2628 /// with source-location information. 2629 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2630 2631 /// \brief Retrieve the nested-name-specifier that qualifies the name. 2632 NestedNameSpecifier *getQualifier() const { 2633 return QualifierLoc.getNestedNameSpecifier(); 2634 } 2635 2636 DeclarationNameInfo getNameInfo() const { 2637 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2638 } 2639 2640 /// \brief Return true if the using declaration has 'typename'. 2641 bool isTypeName() const { return FirstUsingShadow.getInt(); } 2642 2643 /// \brief Sets whether the using declaration has 'typename'. 2644 void setTypeName(bool TN) { FirstUsingShadow.setInt(TN); } 2645 2646 /// \brief Iterates through the using shadow declarations assosiated with 2647 /// this using declaration. 2648 class shadow_iterator { 2649 /// \brief The current using shadow declaration. 2650 UsingShadowDecl *Current; 2651 2652 public: 2653 typedef UsingShadowDecl* value_type; 2654 typedef UsingShadowDecl* reference; 2655 typedef UsingShadowDecl* pointer; 2656 typedef std::forward_iterator_tag iterator_category; 2657 typedef std::ptrdiff_t difference_type; 2658 2659 shadow_iterator() : Current(0) { } 2660 explicit shadow_iterator(UsingShadowDecl *C) : Current(C) { } 2661 2662 reference operator*() const { return Current; } 2663 pointer operator->() const { return Current; } 2664 2665 shadow_iterator& operator++() { 2666 Current = Current->getNextUsingShadowDecl(); 2667 return *this; 2668 } 2669 2670 shadow_iterator operator++(int) { 2671 shadow_iterator tmp(*this); 2672 ++(*this); 2673 return tmp; 2674 } 2675 2676 friend bool operator==(shadow_iterator x, shadow_iterator y) { 2677 return x.Current == y.Current; 2678 } 2679 friend bool operator!=(shadow_iterator x, shadow_iterator y) { 2680 return x.Current != y.Current; 2681 } 2682 }; 2683 2684 shadow_iterator shadow_begin() const { 2685 return shadow_iterator(FirstUsingShadow.getPointer()); 2686 } 2687 shadow_iterator shadow_end() const { return shadow_iterator(); } 2688 2689 /// \brief Return the number of shadowed declarations associated with this 2690 /// using declaration. 2691 unsigned shadow_size() const { 2692 return std::distance(shadow_begin(), shadow_end()); 2693 } 2694 2695 void addShadowDecl(UsingShadowDecl *S); 2696 void removeShadowDecl(UsingShadowDecl *S); 2697 2698 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 2699 SourceLocation UsingL, 2700 NestedNameSpecifierLoc QualifierLoc, 2701 const DeclarationNameInfo &NameInfo, 2702 bool IsTypeNameArg); 2703 2704 static UsingDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2705 2706 SourceRange getSourceRange() const { 2707 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2708 } 2709 2710 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2711 static bool classof(const UsingDecl *D) { return true; } 2712 static bool classofKind(Kind K) { return K == Using; } 2713 2714 friend class ASTDeclReader; 2715 friend class ASTDeclWriter; 2716}; 2717 2718/// UnresolvedUsingValueDecl - Represents a dependent using 2719/// declaration which was not marked with 'typename'. Unlike 2720/// non-dependent using declarations, these *only* bring through 2721/// non-types; otherwise they would break two-phase lookup. 2722/// 2723/// template <class T> class A : public Base<T> { 2724/// using Base<T>::foo; 2725/// }; 2726class UnresolvedUsingValueDecl : public ValueDecl { 2727 virtual void anchor(); 2728 2729 /// \brief The source location of the 'using' keyword 2730 SourceLocation UsingLocation; 2731 2732 /// \brief The nested-name-specifier that precedes the name. 2733 NestedNameSpecifierLoc QualifierLoc; 2734 2735 /// DNLoc - Provides source/type location info for the 2736 /// declaration name embedded in the ValueDecl base class. 2737 DeclarationNameLoc DNLoc; 2738 2739 UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty, 2740 SourceLocation UsingLoc, 2741 NestedNameSpecifierLoc QualifierLoc, 2742 const DeclarationNameInfo &NameInfo) 2743 : ValueDecl(UnresolvedUsingValue, DC, 2744 NameInfo.getLoc(), NameInfo.getName(), Ty), 2745 UsingLocation(UsingLoc), QualifierLoc(QualifierLoc), 2746 DNLoc(NameInfo.getInfo()) 2747 { } 2748 2749public: 2750 /// \brief Returns the source location of the 'using' keyword. 2751 SourceLocation getUsingLoc() const { return UsingLocation; } 2752 2753 /// \brief Set the source location of the 'using' keyword. 2754 void setUsingLoc(SourceLocation L) { UsingLocation = L; } 2755 2756 /// \brief Retrieve the nested-name-specifier that qualifies the name, 2757 /// with source-location information. 2758 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2759 2760 /// \brief Retrieve the nested-name-specifier that qualifies the name. 2761 NestedNameSpecifier *getQualifier() const { 2762 return QualifierLoc.getNestedNameSpecifier(); 2763 } 2764 2765 DeclarationNameInfo getNameInfo() const { 2766 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2767 } 2768 2769 static UnresolvedUsingValueDecl * 2770 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2771 NestedNameSpecifierLoc QualifierLoc, 2772 const DeclarationNameInfo &NameInfo); 2773 2774 static UnresolvedUsingValueDecl * 2775 CreateDeserialized(ASTContext &C, unsigned ID); 2776 2777 SourceRange getSourceRange() const { 2778 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2779 } 2780 2781 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2782 static bool classof(const UnresolvedUsingValueDecl *D) { return true; } 2783 static bool classofKind(Kind K) { return K == UnresolvedUsingValue; } 2784 2785 friend class ASTDeclReader; 2786 friend class ASTDeclWriter; 2787}; 2788 2789/// UnresolvedUsingTypenameDecl - Represents a dependent using 2790/// declaration which was marked with 'typename'. 2791/// 2792/// template <class T> class A : public Base<T> { 2793/// using typename Base<T>::foo; 2794/// }; 2795/// 2796/// The type associated with a unresolved using typename decl is 2797/// currently always a typename type. 2798class UnresolvedUsingTypenameDecl : public TypeDecl { 2799 virtual void anchor(); 2800 2801 /// \brief The source location of the 'using' keyword 2802 SourceLocation UsingLocation; 2803 2804 /// \brief The source location of the 'typename' keyword 2805 SourceLocation TypenameLocation; 2806 2807 /// \brief The nested-name-specifier that precedes the name. 2808 NestedNameSpecifierLoc QualifierLoc; 2809 2810 UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc, 2811 SourceLocation TypenameLoc, 2812 NestedNameSpecifierLoc QualifierLoc, 2813 SourceLocation TargetNameLoc, 2814 IdentifierInfo *TargetName) 2815 : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName, 2816 UsingLoc), 2817 TypenameLocation(TypenameLoc), QualifierLoc(QualifierLoc) { } 2818 2819 friend class ASTDeclReader; 2820 2821public: 2822 /// \brief Returns the source location of the 'using' keyword. 2823 SourceLocation getUsingLoc() const { return getLocStart(); } 2824 2825 /// \brief Returns the source location of the 'typename' keyword. 2826 SourceLocation getTypenameLoc() const { return TypenameLocation; } 2827 2828 /// \brief Retrieve the nested-name-specifier that qualifies the name, 2829 /// with source-location information. 2830 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2831 2832 /// \brief Retrieve the nested-name-specifier that qualifies the name. 2833 NestedNameSpecifier *getQualifier() const { 2834 return QualifierLoc.getNestedNameSpecifier(); 2835 } 2836 2837 static UnresolvedUsingTypenameDecl * 2838 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2839 SourceLocation TypenameLoc, NestedNameSpecifierLoc QualifierLoc, 2840 SourceLocation TargetNameLoc, DeclarationName TargetName); 2841 2842 static UnresolvedUsingTypenameDecl * 2843 CreateDeserialized(ASTContext &C, unsigned ID); 2844 2845 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2846 static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; } 2847 static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; } 2848}; 2849 2850/// StaticAssertDecl - Represents a C++0x static_assert declaration. 2851class StaticAssertDecl : public Decl { 2852 virtual void anchor(); 2853 Expr *AssertExpr; 2854 StringLiteral *Message; 2855 SourceLocation RParenLoc; 2856 2857 StaticAssertDecl(DeclContext *DC, SourceLocation StaticAssertLoc, 2858 Expr *assertexpr, StringLiteral *message, 2859 SourceLocation RParenLoc) 2860 : Decl(StaticAssert, DC, StaticAssertLoc), AssertExpr(assertexpr), 2861 Message(message), RParenLoc(RParenLoc) { } 2862 2863public: 2864 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 2865 SourceLocation StaticAssertLoc, 2866 Expr *AssertExpr, StringLiteral *Message, 2867 SourceLocation RParenLoc); 2868 static StaticAssertDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2869 2870 Expr *getAssertExpr() { return AssertExpr; } 2871 const Expr *getAssertExpr() const { return AssertExpr; } 2872 2873 StringLiteral *getMessage() { return Message; } 2874 const StringLiteral *getMessage() const { return Message; } 2875 2876 SourceLocation getRParenLoc() const { return RParenLoc; } 2877 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2878 2879 SourceRange getSourceRange() const { 2880 return SourceRange(getLocation(), getRParenLoc()); 2881 } 2882 2883 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2884 static bool classof(StaticAssertDecl *D) { return true; } 2885 static bool classofKind(Kind K) { return K == StaticAssert; } 2886 2887 friend class ASTDeclReader; 2888}; 2889 2890/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 2891/// into a diagnostic with <<. 2892const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 2893 AccessSpecifier AS); 2894 2895const PartialDiagnostic &operator<<(const PartialDiagnostic &DB, 2896 AccessSpecifier AS); 2897 2898} // end namespace clang 2899 2900#endif 2901