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