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