DeclCXX.h revision 2a674e8e443b7a3e77957078248fb52b3b1ec321
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/SmallVector.h" 23#include "llvm/ADT/SmallPtrSet.h" 24 25namespace clang { 26 27class ClassTemplateDecl; 28class ClassTemplateSpecializationDecl; 29class CXXBasePath; 30class CXXBasePaths; 31class CXXConstructorDecl; 32class CXXConversionDecl; 33class CXXDestructorDecl; 34class CXXMethodDecl; 35class CXXRecordDecl; 36class CXXMemberLookupCriteria; 37class CXXFinalOverriderMap; 38class FriendDecl; 39 40/// \brief Represents any kind of function declaration, whether it is a 41/// concrete function or a function template. 42class AnyFunctionDecl { 43 NamedDecl *Function; 44 45 AnyFunctionDecl(NamedDecl *ND) : Function(ND) { } 46 47public: 48 AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { } 49 AnyFunctionDecl(FunctionTemplateDecl *FTD); 50 51 /// \brief Implicily converts any function or function template into a 52 /// named declaration. 53 operator NamedDecl *() const { return Function; } 54 55 /// \brief Retrieve the underlying function or function template. 56 NamedDecl *get() const { return Function; } 57 58 static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) { 59 return AnyFunctionDecl(ND); 60 } 61}; 62 63} // end namespace clang 64 65namespace llvm { 66 /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from 67 /// AnyFunctionDecl to any function or function template declaration. 68 template<> struct simplify_type<const ::clang::AnyFunctionDecl> { 69 typedef ::clang::NamedDecl* SimpleType; 70 static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) { 71 return Val; 72 } 73 }; 74 template<> struct simplify_type< ::clang::AnyFunctionDecl> 75 : public simplify_type<const ::clang::AnyFunctionDecl> {}; 76 77 // Provide PointerLikeTypeTraits for non-cvr pointers. 78 template<> 79 class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> { 80 public: 81 static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) { 82 return F.get(); 83 } 84 static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) { 85 return ::clang::AnyFunctionDecl::getFromNamedDecl( 86 static_cast< ::clang::NamedDecl*>(P)); 87 } 88 89 enum { NumLowBitsAvailable = 2 }; 90 }; 91 92} // end namespace llvm 93 94namespace clang { 95 96/// AccessSpecDecl - An access specifier followed by colon ':'. 97/// 98/// An objects of this class represents sugar for the syntactic occurrence 99/// of an access specifier followed by a colon in the list of member 100/// specifiers of a C++ class definition. 101/// 102/// Note that they do not represent other uses of access specifiers, 103/// such as those occurring in a list of base specifiers. 104/// Also note that this class has nothing to do with so-called 105/// "access declarations" (C++98 11.3 [class.access.dcl]). 106class AccessSpecDecl : public Decl { 107 /// ColonLoc - The location of the ':'. 108 SourceLocation ColonLoc; 109 110 AccessSpecDecl(AccessSpecifier AS, DeclContext *DC, 111 SourceLocation ASLoc, SourceLocation ColonLoc) 112 : Decl(AccessSpec, DC, ASLoc), ColonLoc(ColonLoc) { 113 setAccess(AS); 114 } 115 AccessSpecDecl(EmptyShell Empty) 116 : Decl(AccessSpec, Empty) { } 117public: 118 /// getAccessSpecifierLoc - The location of the access specifier. 119 SourceLocation getAccessSpecifierLoc() const { return getLocation(); } 120 /// setAccessSpecifierLoc - Sets the location of the access specifier. 121 void setAccessSpecifierLoc(SourceLocation ASLoc) { setLocation(ASLoc); } 122 123 /// getColonLoc - The location of the colon following the access specifier. 124 SourceLocation getColonLoc() const { return ColonLoc; } 125 /// setColonLoc - Sets the location of the colon. 126 void setColonLoc(SourceLocation CLoc) { ColonLoc = CLoc; } 127 128 SourceRange getSourceRange() const { 129 return SourceRange(getAccessSpecifierLoc(), getColonLoc()); 130 } 131 132 static AccessSpecDecl *Create(ASTContext &C, AccessSpecifier AS, 133 DeclContext *DC, SourceLocation ASLoc, 134 SourceLocation ColonLoc) { 135 return new (C) AccessSpecDecl(AS, DC, ASLoc, ColonLoc); 136 } 137 static AccessSpecDecl *Create(ASTContext &C, EmptyShell Empty) { 138 return new (C) AccessSpecDecl(Empty); 139 } 140 141 // Implement isa/cast/dyncast/etc. 142 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 143 static bool classof(const AccessSpecDecl *D) { return true; } 144 static bool classofKind(Kind K) { return K == AccessSpec; } 145}; 146 147 148/// CXXBaseSpecifier - A base class of a C++ class. 149/// 150/// Each CXXBaseSpecifier represents a single, direct base class (or 151/// struct) of a C++ class (or struct). It specifies the type of that 152/// base class, whether it is a virtual or non-virtual base, and what 153/// level of access (public, protected, private) is used for the 154/// derivation. For example: 155/// 156/// @code 157/// class A { }; 158/// class B { }; 159/// class C : public virtual A, protected B { }; 160/// @endcode 161/// 162/// In this code, C will have two CXXBaseSpecifiers, one for "public 163/// virtual A" and the other for "protected B". 164class CXXBaseSpecifier { 165 /// Range - The source code range that covers the full base 166 /// specifier, including the "virtual" (if present) and access 167 /// specifier (if present). 168 SourceRange Range; 169 170 /// Virtual - Whether this is a virtual base class or not. 171 bool Virtual : 1; 172 173 /// BaseOfClass - Whether this is the base of a class (true) or of a 174 /// struct (false). This determines the mapping from the access 175 /// specifier as written in the source code to the access specifier 176 /// used for semantic analysis. 177 bool BaseOfClass : 1; 178 179 /// Access - Access specifier as written in the source code (which 180 /// may be AS_none). The actual type of data stored here is an 181 /// AccessSpecifier, but we use "unsigned" here to work around a 182 /// VC++ bug. 183 unsigned Access : 2; 184 185 /// BaseTypeInfo - The type of the base class. This will be a class or struct 186 /// (or a typedef of such). The source code range does not include the 187 /// "virtual" or access specifier. 188 TypeSourceInfo *BaseTypeInfo; 189 190public: 191 CXXBaseSpecifier() { } 192 193 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, 194 TypeSourceInfo *TInfo) 195 : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseTypeInfo(TInfo) { } 196 197 /// getSourceRange - Retrieves the source range that contains the 198 /// entire base specifier. 199 SourceRange getSourceRange() const { return Range; } 200 201 /// isVirtual - Determines whether the base class is a virtual base 202 /// class (or not). 203 bool isVirtual() const { return Virtual; } 204 205 /// \brief Determine whether this base class is a base of a class declared 206 /// with the 'class' keyword (vs. one declared with the 'struct' keyword). 207 bool isBaseOfClass() const { return BaseOfClass; } 208 209 /// getAccessSpecifier - Returns the access specifier for this base 210 /// specifier. This is the actual base specifier as used for 211 /// semantic analysis, so the result can never be AS_none. To 212 /// retrieve the access specifier as written in the source code, use 213 /// getAccessSpecifierAsWritten(). 214 AccessSpecifier getAccessSpecifier() const { 215 if ((AccessSpecifier)Access == AS_none) 216 return BaseOfClass? AS_private : AS_public; 217 else 218 return (AccessSpecifier)Access; 219 } 220 221 /// getAccessSpecifierAsWritten - Retrieves the access specifier as 222 /// written in the source code (which may mean that no access 223 /// specifier was explicitly written). Use getAccessSpecifier() to 224 /// retrieve the access specifier for use in semantic analysis. 225 AccessSpecifier getAccessSpecifierAsWritten() const { 226 return (AccessSpecifier)Access; 227 } 228 229 /// getType - Retrieves the type of the base class. This type will 230 /// always be an unqualified class type. 231 QualType getType() const { return BaseTypeInfo->getType(); } 232 233 /// getTypeLoc - Retrieves the type and source location of the base class. 234 TypeSourceInfo *getTypeSourceInfo() const { return BaseTypeInfo; } 235}; 236 237/// CXXRecordDecl - Represents a C++ struct/union/class. 238/// FIXME: This class will disappear once we've properly taught RecordDecl 239/// to deal with C++-specific things. 240class CXXRecordDecl : public RecordDecl { 241 242 friend void TagDecl::startDefinition(); 243 244 struct DefinitionData { 245 DefinitionData(CXXRecordDecl *D); 246 247 /// UserDeclaredConstructor - True when this class has a 248 /// user-declared constructor. 249 bool UserDeclaredConstructor : 1; 250 251 /// UserDeclaredCopyConstructor - True when this class has a 252 /// user-declared copy constructor. 253 bool UserDeclaredCopyConstructor : 1; 254 255 /// UserDeclaredCopyAssignment - True when this class has a 256 /// user-declared copy assignment operator. 257 bool UserDeclaredCopyAssignment : 1; 258 259 /// UserDeclaredDestructor - True when this class has a 260 /// user-declared destructor. 261 bool UserDeclaredDestructor : 1; 262 263 /// Aggregate - True when this class is an aggregate. 264 bool Aggregate : 1; 265 266 /// PlainOldData - True when this class is a POD-type. 267 bool PlainOldData : 1; 268 269 /// Empty - true when this class is empty for traits purposes, 270 /// i.e. has no data members other than 0-width bit-fields, has no 271 /// virtual function/base, and doesn't inherit from a non-empty 272 /// class. Doesn't take union-ness into account. 273 bool Empty : 1; 274 275 /// Polymorphic - True when this class is polymorphic, i.e. has at 276 /// least one virtual member or derives from a polymorphic class. 277 bool Polymorphic : 1; 278 279 /// Abstract - True when this class is abstract, i.e. has at least 280 /// one pure virtual function, (that can come from a base class). 281 bool Abstract : 1; 282 283 /// HasTrivialConstructor - True when this class has a trivial constructor. 284 /// 285 /// C++ [class.ctor]p5. A constructor is trivial if it is an 286 /// implicitly-declared default constructor and if: 287 /// * its class has no virtual functions and no virtual base classes, and 288 /// * all the direct base classes of its class have trivial constructors, and 289 /// * for all the nonstatic data members of its class that are of class type 290 /// (or array thereof), each such class has a trivial constructor. 291 bool HasTrivialConstructor : 1; 292 293 /// HasTrivialCopyConstructor - True when this class has a trivial copy 294 /// constructor. 295 /// 296 /// C++ [class.copy]p6. A copy constructor for class X is trivial 297 /// if it is implicitly declared and if 298 /// * class X has no virtual functions and no virtual base classes, and 299 /// * each direct base class of X has a trivial copy constructor, and 300 /// * for all the nonstatic data members of X that are of class type (or 301 /// array thereof), each such class type has a trivial copy constructor; 302 /// otherwise the copy constructor is non-trivial. 303 bool HasTrivialCopyConstructor : 1; 304 305 /// HasTrivialCopyAssignment - True when this class has a trivial copy 306 /// assignment operator. 307 /// 308 /// C++ [class.copy]p11. A copy assignment operator for class X is 309 /// trivial if it is implicitly declared and if 310 /// * class X has no virtual functions and no virtual base classes, and 311 /// * each direct base class of X has a trivial copy assignment operator, and 312 /// * for all the nonstatic data members of X that are of class type (or 313 /// array thereof), each such class type has a trivial copy assignment 314 /// operator; 315 /// otherwise the copy assignment operator is non-trivial. 316 bool HasTrivialCopyAssignment : 1; 317 318 /// HasTrivialDestructor - True when this class has a trivial destructor. 319 /// 320 /// C++ [class.dtor]p3. A destructor is trivial if it is an 321 /// implicitly-declared destructor and if: 322 /// * all of the direct base classes of its class have trivial destructors 323 /// and 324 /// * for all of the non-static data members of its class that are of class 325 /// type (or array thereof), each such class has a trivial destructor. 326 bool HasTrivialDestructor : 1; 327 328 /// ComputedVisibleConversions - True when visible conversion functions are 329 /// already computed and are available. 330 bool ComputedVisibleConversions : 1; 331 332 /// \brief Whether we have already declared the default constructor or 333 /// do not need to have one declared. 334 bool DeclaredDefaultConstructor : 1; 335 336 /// \brief Whether we have already declared the copy constructor. 337 bool DeclaredCopyConstructor : 1; 338 339 /// \brief Whether we have already declared the copy-assignment operator. 340 bool DeclaredCopyAssignment : 1; 341 342 /// \brief Whether we have already declared a destructor within the class. 343 bool DeclaredDestructor : 1; 344 345 /// Bases - Base classes of this class. 346 /// FIXME: This is wasted space for a union. 347 CXXBaseSpecifier *Bases; 348 349 /// NumBases - The number of base class specifiers in Bases. 350 unsigned NumBases; 351 352 /// VBases - direct and indirect virtual base classes of this class. 353 CXXBaseSpecifier *VBases; 354 355 /// NumVBases - The number of virtual base class specifiers in VBases. 356 unsigned NumVBases; 357 358 /// Conversions - Overload set containing the conversion functions 359 /// of this C++ class (but not its inherited conversion 360 /// functions). Each of the entries in this overload set is a 361 /// CXXConversionDecl. 362 UnresolvedSet<4> Conversions; 363 364 /// VisibleConversions - Overload set containing the conversion 365 /// functions of this C++ class and all those inherited conversion 366 /// functions that are visible in this class. Each of the entries 367 /// in this overload set is a CXXConversionDecl or a 368 /// FunctionTemplateDecl. 369 UnresolvedSet<4> VisibleConversions; 370 371 /// Definition - The declaration which defines this record. 372 CXXRecordDecl *Definition; 373 374 /// FirstFriend - The first friend declaration in this class, or 375 /// null if there aren't any. This is actually currently stored 376 /// in reverse order. 377 FriendDecl *FirstFriend; 378 379 } *DefinitionData; 380 381 struct DefinitionData &data() { 382 assert(DefinitionData && "queried property of class with no definition"); 383 return *DefinitionData; 384 } 385 386 const struct DefinitionData &data() const { 387 assert(DefinitionData && "queried property of class with no definition"); 388 return *DefinitionData; 389 } 390 391 /// \brief The template or declaration that this declaration 392 /// describes or was instantiated from, respectively. 393 /// 394 /// For non-templates, this value will be NULL. For record 395 /// declarations that describe a class template, this will be a 396 /// pointer to a ClassTemplateDecl. For member 397 /// classes of class template specializations, this will be the 398 /// MemberSpecializationInfo referring to the member class that was 399 /// instantiated or specialized. 400 llvm::PointerUnion<ClassTemplateDecl*, MemberSpecializationInfo*> 401 TemplateOrInstantiation; 402 403#ifndef NDEBUG 404 void CheckConversionFunction(NamedDecl *D); 405#endif 406 407 friend class DeclContext; 408 409 /// \brief Notify the class that member has been added. 410 /// 411 /// This routine helps maintain information about the class based on which 412 /// members have been added. It will be invoked by DeclContext::addDecl() 413 /// whenever a member is added to this record. 414 void addedMember(Decl *D); 415 416protected: 417 CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 418 SourceLocation L, IdentifierInfo *Id, 419 CXXRecordDecl *PrevDecl, 420 SourceLocation TKL = SourceLocation()); 421 422public: 423 /// base_class_iterator - Iterator that traverses the base classes 424 /// of a class. 425 typedef CXXBaseSpecifier* base_class_iterator; 426 427 /// base_class_const_iterator - Iterator that traverses the base 428 /// classes of a class. 429 typedef const CXXBaseSpecifier* base_class_const_iterator; 430 431 /// reverse_base_class_iterator = Iterator that traverses the base classes 432 /// of a class in reverse order. 433 typedef std::reverse_iterator<base_class_iterator> 434 reverse_base_class_iterator; 435 436 /// reverse_base_class_iterator = Iterator that traverses the base classes 437 /// of a class in reverse order. 438 typedef std::reverse_iterator<base_class_const_iterator> 439 reverse_base_class_const_iterator; 440 441 virtual CXXRecordDecl *getCanonicalDecl() { 442 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl()); 443 } 444 virtual const CXXRecordDecl *getCanonicalDecl() const { 445 return cast<CXXRecordDecl>(RecordDecl::getCanonicalDecl()); 446 } 447 448 const CXXRecordDecl *getPreviousDeclaration() const { 449 return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDeclaration()); 450 } 451 CXXRecordDecl *getPreviousDeclaration() { 452 return cast_or_null<CXXRecordDecl>(RecordDecl::getPreviousDeclaration()); 453 } 454 455 CXXRecordDecl *getDefinition() const { 456 if (!DefinitionData) return 0; 457 return data().Definition; 458 } 459 460 bool hasDefinition() const { return DefinitionData != 0; } 461 462 static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 463 SourceLocation L, IdentifierInfo *Id, 464 SourceLocation TKL = SourceLocation(), 465 CXXRecordDecl* PrevDecl=0, 466 bool DelayTypeCreation = false); 467 static CXXRecordDecl *Create(ASTContext &C, EmptyShell Empty); 468 469 bool isDynamicClass() const { 470 return data().Polymorphic || data().NumVBases != 0; 471 } 472 473 /// setBases - Sets the base classes of this struct or class. 474 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases); 475 476 /// getNumBases - Retrieves the number of base classes of this 477 /// class. 478 unsigned getNumBases() const { return data().NumBases; } 479 480 base_class_iterator bases_begin() { return data().Bases; } 481 base_class_const_iterator bases_begin() const { return data().Bases; } 482 base_class_iterator bases_end() { return bases_begin() + data().NumBases; } 483 base_class_const_iterator bases_end() const { 484 return bases_begin() + data().NumBases; 485 } 486 reverse_base_class_iterator bases_rbegin() { 487 return reverse_base_class_iterator(bases_end()); 488 } 489 reverse_base_class_const_iterator bases_rbegin() const { 490 return reverse_base_class_const_iterator(bases_end()); 491 } 492 reverse_base_class_iterator bases_rend() { 493 return reverse_base_class_iterator(bases_begin()); 494 } 495 reverse_base_class_const_iterator bases_rend() const { 496 return reverse_base_class_const_iterator(bases_begin()); 497 } 498 499 /// getNumVBases - Retrieves the number of virtual base classes of this 500 /// class. 501 unsigned getNumVBases() const { return data().NumVBases; } 502 503 base_class_iterator vbases_begin() { return data().VBases; } 504 base_class_const_iterator vbases_begin() const { return data().VBases; } 505 base_class_iterator vbases_end() { return vbases_begin() + data().NumVBases; } 506 base_class_const_iterator vbases_end() const { 507 return vbases_begin() + data().NumVBases; 508 } 509 reverse_base_class_iterator vbases_rbegin() { 510 return reverse_base_class_iterator(vbases_end()); 511 } 512 reverse_base_class_const_iterator vbases_rbegin() const { 513 return reverse_base_class_const_iterator(vbases_end()); 514 } 515 reverse_base_class_iterator vbases_rend() { 516 return reverse_base_class_iterator(vbases_begin()); 517 } 518 reverse_base_class_const_iterator vbases_rend() const { 519 return reverse_base_class_const_iterator(vbases_begin()); 520 } 521 522 /// \brief Determine whether this class has any dependent base classes. 523 bool hasAnyDependentBases() const; 524 525 /// Iterator access to method members. The method iterator visits 526 /// all method members of the class, including non-instance methods, 527 /// special methods, etc. 528 typedef specific_decl_iterator<CXXMethodDecl> method_iterator; 529 530 /// method_begin - Method begin iterator. Iterates in the order the methods 531 /// were declared. 532 method_iterator method_begin() const { 533 return method_iterator(decls_begin()); 534 } 535 /// method_end - Method end iterator. 536 method_iterator method_end() const { 537 return method_iterator(decls_end()); 538 } 539 540 /// Iterator access to constructor members. 541 typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator; 542 543 ctor_iterator ctor_begin() const { 544 return ctor_iterator(decls_begin()); 545 } 546 ctor_iterator ctor_end() const { 547 return ctor_iterator(decls_end()); 548 } 549 550 /// An iterator over friend declarations. All of these are defined 551 /// in DeclFriend.h. 552 class friend_iterator; 553 friend_iterator friend_begin() const; 554 friend_iterator friend_end() const; 555 void pushFriendDecl(FriendDecl *FD); 556 557 /// Determines whether this record has any friends. 558 bool hasFriends() const { 559 return data().FirstFriend != 0; 560 } 561 562 /// \brief Determine whether this class has had its default constructor 563 /// declared implicitly or does not need one declared implicitly. 564 /// 565 /// This value is used for lazy creation of default constructors. 566 bool hasDeclaredDefaultConstructor() const { 567 return data().DeclaredDefaultConstructor; 568 } 569 570 /// hasConstCopyConstructor - Determines whether this class has a 571 /// copy constructor that accepts a const-qualified argument. 572 bool hasConstCopyConstructor(ASTContext &Context) const; 573 574 /// getCopyConstructor - Returns the copy constructor for this class 575 CXXConstructorDecl *getCopyConstructor(ASTContext &Context, 576 unsigned TypeQuals) const; 577 578 /// \brief Retrieve the copy-assignment operator for this class, if available. 579 /// 580 /// This routine attempts to find the copy-assignment operator for this 581 /// class, using a simplistic form of overload resolution. 582 /// 583 /// \param ArgIsConst Whether the argument to the copy-assignment operator 584 /// is const-qualified. 585 /// 586 /// \returns The copy-assignment operator that can be invoked, or NULL if 587 /// a unique copy-assignment operator could not be found. 588 CXXMethodDecl *getCopyAssignmentOperator(bool ArgIsConst) const; 589 590 /// hasUserDeclaredConstructor - Whether this class has any 591 /// user-declared constructors. When true, a default constructor 592 /// will not be implicitly declared. 593 bool hasUserDeclaredConstructor() const { 594 return data().UserDeclaredConstructor; 595 } 596 597 /// hasUserDeclaredCopyConstructor - Whether this class has a 598 /// user-declared copy constructor. When false, a copy constructor 599 /// will be implicitly declared. 600 bool hasUserDeclaredCopyConstructor() const { 601 return data().UserDeclaredCopyConstructor; 602 } 603 604 /// \brief Determine whether this class has had its copy constructor 605 /// declared, either via the user or via an implicit declaration. 606 /// 607 /// This value is used for lazy creation of copy constructors. 608 bool hasDeclaredCopyConstructor() const { 609 return data().DeclaredCopyConstructor; 610 } 611 612 /// hasUserDeclaredCopyAssignment - Whether this class has a 613 /// user-declared copy assignment operator. When false, a copy 614 /// assigment operator will be implicitly declared. 615 bool hasUserDeclaredCopyAssignment() const { 616 return data().UserDeclaredCopyAssignment; 617 } 618 619 /// \brief Determine whether this class has had its copy assignment operator 620 /// declared, either via the user or via an implicit declaration. 621 /// 622 /// This value is used for lazy creation of copy assignment operators. 623 bool hasDeclaredCopyAssignment() const { 624 return data().DeclaredCopyAssignment; 625 } 626 627 /// hasUserDeclaredDestructor - Whether this class has a 628 /// user-declared destructor. When false, a destructor will be 629 /// implicitly declared. 630 bool hasUserDeclaredDestructor() const { 631 return data().UserDeclaredDestructor; 632 } 633 634 /// \brief Determine whether this class has had its destructor declared, 635 /// either via the user or via an implicit declaration. 636 /// 637 /// This value is used for lazy creation of destructors. 638 bool hasDeclaredDestructor() const { return data().DeclaredDestructor; } 639 640 /// getConversions - Retrieve the overload set containing all of the 641 /// conversion functions in this class. 642 UnresolvedSetImpl *getConversionFunctions() { 643 return &data().Conversions; 644 } 645 const UnresolvedSetImpl *getConversionFunctions() const { 646 return &data().Conversions; 647 } 648 649 typedef UnresolvedSetImpl::iterator conversion_iterator; 650 conversion_iterator conversion_begin() const { 651 return getConversionFunctions()->begin(); 652 } 653 conversion_iterator conversion_end() const { 654 return getConversionFunctions()->end(); 655 } 656 657 /// Replaces a conversion function with a new declaration. 658 /// 659 /// Returns true if the old conversion was found. 660 bool replaceConversion(const NamedDecl* Old, NamedDecl *New) { 661 return getConversionFunctions()->replace(Old, New); 662 } 663 664 /// Removes a conversion function from this class. The conversion 665 /// function must currently be a member of this class. Furthermore, 666 /// this class must currently be in the process of being defined. 667 void removeConversion(const NamedDecl *Old); 668 669 /// getVisibleConversionFunctions - get all conversion functions visible 670 /// in current class; including conversion function templates. 671 const UnresolvedSetImpl *getVisibleConversionFunctions(); 672 673 /// addConversionFunction - Registers a conversion function which 674 /// this class declares directly. 675 void addConversionFunction(NamedDecl *Decl) { 676#ifndef NDEBUG 677 CheckConversionFunction(Decl); 678#endif 679 680 // We intentionally don't use the decl's access here because it 681 // hasn't been set yet. That's really just a misdesign in Sema. 682 data().Conversions.addDecl(Decl); 683 } 684 685 /// isAggregate - Whether this class is an aggregate (C++ 686 /// [dcl.init.aggr]), which is a class with no user-declared 687 /// constructors, no private or protected non-static data members, 688 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 689 bool isAggregate() const { return data().Aggregate; } 690 691 /// setAggregate - Set whether this class is an aggregate (C++ 692 /// [dcl.init.aggr]). 693 void setAggregate(bool Agg) { data().Aggregate = Agg; } 694 695 /// setMethodAsVirtual - Make input method virtual and set the necesssary 696 /// special function bits and other bits accordingly. 697 void setMethodAsVirtual(FunctionDecl *Method); 698 699 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 700 /// that is an aggregate that has no non-static non-POD data members, no 701 /// reference data members, no user-defined copy assignment operator and no 702 /// user-defined destructor. 703 bool isPOD() const { return data().PlainOldData; } 704 705 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 706 void setPOD(bool POD) { data().PlainOldData = POD; } 707 708 /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which 709 /// means it has a virtual function, virtual base, data member (other than 710 /// 0-width bit-field) or inherits from a non-empty class. Does NOT include 711 /// a check for union-ness. 712 bool isEmpty() const { return data().Empty; } 713 714 /// Set whether this class is empty (C++0x [meta.unary.prop]) 715 void setEmpty(bool Emp) { data().Empty = Emp; } 716 717 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 718 /// which means that the class contains or inherits a virtual function. 719 bool isPolymorphic() const { return data().Polymorphic; } 720 721 /// setPolymorphic - Set whether this class is polymorphic (C++ 722 /// [class.virtual]). 723 void setPolymorphic(bool Poly) { data().Polymorphic = Poly; } 724 725 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 726 /// which means that the class contains or inherits a pure virtual function. 727 bool isAbstract() const { return data().Abstract; } 728 729 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 730 void setAbstract(bool Abs) { data().Abstract = Abs; } 731 732 // hasTrivialConstructor - Whether this class has a trivial constructor 733 // (C++ [class.ctor]p5) 734 bool hasTrivialConstructor() const { return data().HasTrivialConstructor; } 735 736 // setHasTrivialConstructor - Set whether this class has a trivial constructor 737 // (C++ [class.ctor]p5) 738 void setHasTrivialConstructor(bool TC) { data().HasTrivialConstructor = TC; } 739 740 // hasTrivialCopyConstructor - Whether this class has a trivial copy 741 // constructor (C++ [class.copy]p6) 742 bool hasTrivialCopyConstructor() const { 743 return data().HasTrivialCopyConstructor; 744 } 745 746 // setHasTrivialCopyConstructor - Set whether this class has a trivial 747 // copy constructor (C++ [class.copy]p6) 748 void setHasTrivialCopyConstructor(bool TC) { 749 data().HasTrivialCopyConstructor = TC; 750 } 751 752 // hasTrivialCopyAssignment - Whether this class has a trivial copy 753 // assignment operator (C++ [class.copy]p11) 754 bool hasTrivialCopyAssignment() const { 755 return data().HasTrivialCopyAssignment; 756 } 757 758 // setHasTrivialCopyAssignment - Set whether this class has a 759 // trivial copy assignment operator (C++ [class.copy]p11) 760 void setHasTrivialCopyAssignment(bool TC) { 761 data().HasTrivialCopyAssignment = TC; 762 } 763 764 // hasTrivialDestructor - Whether this class has a trivial destructor 765 // (C++ [class.dtor]p3) 766 bool hasTrivialDestructor() const { return data().HasTrivialDestructor; } 767 768 // setHasTrivialDestructor - Set whether this class has a trivial destructor 769 // (C++ [class.dtor]p3) 770 void setHasTrivialDestructor(bool TC) { data().HasTrivialDestructor = TC; } 771 772 /// \brief If this record is an instantiation of a member class, 773 /// retrieves the member class from which it was instantiated. 774 /// 775 /// This routine will return non-NULL for (non-templated) member 776 /// classes of class templates. For example, given: 777 /// 778 /// \code 779 /// template<typename T> 780 /// struct X { 781 /// struct A { }; 782 /// }; 783 /// \endcode 784 /// 785 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 786 /// whose parent is the class template specialization X<int>. For 787 /// this declaration, getInstantiatedFromMemberClass() will return 788 /// the CXXRecordDecl X<T>::A. When a complete definition of 789 /// X<int>::A is required, it will be instantiated from the 790 /// declaration returned by getInstantiatedFromMemberClass(). 791 CXXRecordDecl *getInstantiatedFromMemberClass() const; 792 793 /// \brief If this class is an instantiation of a member class of a 794 /// class template specialization, retrieves the member specialization 795 /// information. 796 MemberSpecializationInfo *getMemberSpecializationInfo() const; 797 798 /// \brief Specify that this record is an instantiation of the 799 /// member class RD. 800 void setInstantiationOfMemberClass(CXXRecordDecl *RD, 801 TemplateSpecializationKind TSK); 802 803 /// \brief Retrieves the class template that is described by this 804 /// class declaration. 805 /// 806 /// Every class template is represented as a ClassTemplateDecl and a 807 /// CXXRecordDecl. The former contains template properties (such as 808 /// the template parameter lists) while the latter contains the 809 /// actual description of the template's 810 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 811 /// CXXRecordDecl that from a ClassTemplateDecl, while 812 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 813 /// a CXXRecordDecl. 814 ClassTemplateDecl *getDescribedClassTemplate() const { 815 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 816 } 817 818 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 819 TemplateOrInstantiation = Template; 820 } 821 822 /// \brief Determine whether this particular class is a specialization or 823 /// instantiation of a class template or member class of a class template, 824 /// and how it was instantiated or specialized. 825 TemplateSpecializationKind getTemplateSpecializationKind() const; 826 827 /// \brief Set the kind of specialization or template instantiation this is. 828 void setTemplateSpecializationKind(TemplateSpecializationKind TSK); 829 830 /// getDestructor - Returns the destructor decl for this class. 831 CXXDestructorDecl *getDestructor() const; 832 833 /// isLocalClass - If the class is a local class [class.local], returns 834 /// the enclosing function declaration. 835 const FunctionDecl *isLocalClass() const { 836 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) 837 return RD->isLocalClass(); 838 839 return dyn_cast<FunctionDecl>(getDeclContext()); 840 } 841 842 /// \brief Determine whether this class is derived from the class \p Base. 843 /// 844 /// This routine only determines whether this class is derived from \p Base, 845 /// but does not account for factors that may make a Derived -> Base class 846 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 847 /// base class subobjects. 848 /// 849 /// \param Base the base class we are searching for. 850 /// 851 /// \returns true if this class is derived from Base, false otherwise. 852 bool isDerivedFrom(CXXRecordDecl *Base) const; 853 854 /// \brief Determine whether this class is derived from the type \p Base. 855 /// 856 /// This routine only determines whether this class is derived from \p Base, 857 /// but does not account for factors that may make a Derived -> Base class 858 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 859 /// base class subobjects. 860 /// 861 /// \param Base the base class we are searching for. 862 /// 863 /// \param Paths will contain the paths taken from the current class to the 864 /// given \p Base class. 865 /// 866 /// \returns true if this class is derived from Base, false otherwise. 867 /// 868 /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than 869 /// tangling input and output in \p Paths 870 bool isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const; 871 872 /// \brief Determine whether this class is virtually derived from 873 /// the class \p Base. 874 /// 875 /// This routine only determines whether this class is virtually 876 /// derived from \p Base, but does not account for factors that may 877 /// make a Derived -> Base class ill-formed, such as 878 /// private/protected inheritance or multiple, ambiguous base class 879 /// subobjects. 880 /// 881 /// \param Base the base class we are searching for. 882 /// 883 /// \returns true if this class is virtually derived from Base, 884 /// false otherwise. 885 bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const; 886 887 /// \brief Determine whether this class is provably not derived from 888 /// the type \p Base. 889 bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const; 890 891 /// \brief Function type used by forallBases() as a callback. 892 /// 893 /// \param Base the definition of the base class 894 /// 895 /// \returns true if this base matched the search criteria 896 typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition, 897 void *UserData); 898 899 /// \brief Determines if the given callback holds for all the direct 900 /// or indirect base classes of this type. 901 /// 902 /// The class itself does not count as a base class. This routine 903 /// returns false if the class has non-computable base classes. 904 /// 905 /// \param AllowShortCircuit if false, forces the callback to be called 906 /// for every base class, even if a dependent or non-matching base was 907 /// found. 908 bool forallBases(ForallBasesCallback *BaseMatches, void *UserData, 909 bool AllowShortCircuit = true) const; 910 911 /// \brief Function type used by lookupInBases() to determine whether a 912 /// specific base class subobject matches the lookup criteria. 913 /// 914 /// \param Specifier the base-class specifier that describes the inheritance 915 /// from the base class we are trying to match. 916 /// 917 /// \param Path the current path, from the most-derived class down to the 918 /// base named by the \p Specifier. 919 /// 920 /// \param UserData a single pointer to user-specified data, provided to 921 /// lookupInBases(). 922 /// 923 /// \returns true if this base matched the search criteria, false otherwise. 924 typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier, 925 CXXBasePath &Path, 926 void *UserData); 927 928 /// \brief Look for entities within the base classes of this C++ class, 929 /// transitively searching all base class subobjects. 930 /// 931 /// This routine uses the callback function \p BaseMatches to find base 932 /// classes meeting some search criteria, walking all base class subobjects 933 /// and populating the given \p Paths structure with the paths through the 934 /// inheritance hierarchy that resulted in a match. On a successful search, 935 /// the \p Paths structure can be queried to retrieve the matching paths and 936 /// to determine if there were any ambiguities. 937 /// 938 /// \param BaseMatches callback function used to determine whether a given 939 /// base matches the user-defined search criteria. 940 /// 941 /// \param UserData user data pointer that will be provided to \p BaseMatches. 942 /// 943 /// \param Paths used to record the paths from this class to its base class 944 /// subobjects that match the search criteria. 945 /// 946 /// \returns true if there exists any path from this class to a base class 947 /// subobject that matches the search criteria. 948 bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData, 949 CXXBasePaths &Paths) const; 950 951 /// \brief Base-class lookup callback that determines whether the given 952 /// base class specifier refers to a specific class declaration. 953 /// 954 /// This callback can be used with \c lookupInBases() to determine whether 955 /// a given derived class has is a base class subobject of a particular type. 956 /// The user data pointer should refer to the canonical CXXRecordDecl of the 957 /// base class that we are searching for. 958 static bool FindBaseClass(const CXXBaseSpecifier *Specifier, 959 CXXBasePath &Path, void *BaseRecord); 960 961 /// \brief Base-class lookup callback that determines whether the 962 /// given base class specifier refers to a specific class 963 /// declaration and describes virtual derivation. 964 /// 965 /// This callback can be used with \c lookupInBases() to determine 966 /// whether a given derived class has is a virtual base class 967 /// subobject of a particular type. The user data pointer should 968 /// refer to the canonical CXXRecordDecl of the base class that we 969 /// are searching for. 970 static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, 971 CXXBasePath &Path, void *BaseRecord); 972 973 /// \brief Base-class lookup callback that determines whether there exists 974 /// a tag with the given name. 975 /// 976 /// This callback can be used with \c lookupInBases() to find tag members 977 /// of the given name within a C++ class hierarchy. The user data pointer 978 /// is an opaque \c DeclarationName pointer. 979 static bool FindTagMember(const CXXBaseSpecifier *Specifier, 980 CXXBasePath &Path, void *Name); 981 982 /// \brief Base-class lookup callback that determines whether there exists 983 /// a member with the given name. 984 /// 985 /// This callback can be used with \c lookupInBases() to find members 986 /// of the given name within a C++ class hierarchy. The user data pointer 987 /// is an opaque \c DeclarationName pointer. 988 static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier, 989 CXXBasePath &Path, void *Name); 990 991 /// \brief Base-class lookup callback that determines whether there exists 992 /// a member with the given name that can be used in a nested-name-specifier. 993 /// 994 /// This callback can be used with \c lookupInBases() to find membes of 995 /// the given name within a C++ class hierarchy that can occur within 996 /// nested-name-specifiers. 997 static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, 998 CXXBasePath &Path, 999 void *UserData); 1000 1001 /// \brief Retrieve the final overriders for each virtual member 1002 /// function in the class hierarchy where this class is the 1003 /// most-derived class in the class hierarchy. 1004 void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const; 1005 1006 /// viewInheritance - Renders and displays an inheritance diagram 1007 /// for this C++ class and all of its base classes (transitively) using 1008 /// GraphViz. 1009 void viewInheritance(ASTContext& Context) const; 1010 1011 /// MergeAccess - Calculates the access of a decl that is reached 1012 /// along a path. 1013 static AccessSpecifier MergeAccess(AccessSpecifier PathAccess, 1014 AccessSpecifier DeclAccess) { 1015 assert(DeclAccess != AS_none); 1016 if (DeclAccess == AS_private) return AS_none; 1017 return (PathAccess > DeclAccess ? PathAccess : DeclAccess); 1018 } 1019 1020 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1021 static bool classofKind(Kind K) { 1022 return K >= firstCXXRecord && K <= lastCXXRecord; 1023 } 1024 static bool classof(const CXXRecordDecl *D) { return true; } 1025 static bool classof(const ClassTemplateSpecializationDecl *D) { 1026 return true; 1027 } 1028 1029 friend class ASTDeclReader; 1030 friend class ASTDeclWriter; 1031}; 1032 1033/// CXXMethodDecl - Represents a static or instance method of a 1034/// struct/union/class. 1035class CXXMethodDecl : public FunctionDecl { 1036protected: 1037 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, 1038 const DeclarationNameInfo &NameInfo, 1039 QualType T, TypeSourceInfo *TInfo, 1040 bool isStatic, StorageClass SCAsWritten, bool isInline) 1041 : FunctionDecl(DK, RD, NameInfo, T, TInfo, (isStatic ? SC_Static : SC_None), 1042 SCAsWritten, isInline) {} 1043 1044public: 1045 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1046 const DeclarationNameInfo &NameInfo, 1047 QualType T, TypeSourceInfo *TInfo, 1048 bool isStatic = false, 1049 StorageClass SCAsWritten = SC_None, 1050 bool isInline = false); 1051 1052 bool isStatic() const { return getStorageClass() == SC_Static; } 1053 bool isInstance() const { return !isStatic(); } 1054 1055 bool isVirtual() const { 1056 CXXMethodDecl *CD = 1057 cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl()); 1058 1059 if (CD->isVirtualAsWritten()) 1060 return true; 1061 1062 return (CD->begin_overridden_methods() != CD->end_overridden_methods()); 1063 } 1064 1065 /// \brief Determine whether this is a usual deallocation function 1066 /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded 1067 /// delete or delete[] operator with a particular signature. 1068 bool isUsualDeallocationFunction() const; 1069 1070 /// \brief Determine whether this is a copy-assignment operator, regardless 1071 /// of whether it was declared implicitly or explicitly. 1072 bool isCopyAssignmentOperator() const; 1073 1074 const CXXMethodDecl *getCanonicalDecl() const { 1075 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1076 } 1077 CXXMethodDecl *getCanonicalDecl() { 1078 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1079 } 1080 1081 /// 1082 void addOverriddenMethod(const CXXMethodDecl *MD); 1083 1084 typedef const CXXMethodDecl ** method_iterator; 1085 1086 method_iterator begin_overridden_methods() const; 1087 method_iterator end_overridden_methods() const; 1088 unsigned size_overridden_methods() const; 1089 1090 /// getParent - Returns the parent of this method declaration, which 1091 /// is the class in which this method is defined. 1092 const CXXRecordDecl *getParent() const { 1093 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 1094 } 1095 1096 /// getParent - Returns the parent of this method declaration, which 1097 /// is the class in which this method is defined. 1098 CXXRecordDecl *getParent() { 1099 return const_cast<CXXRecordDecl *>( 1100 cast<CXXRecordDecl>(FunctionDecl::getParent())); 1101 } 1102 1103 /// getThisType - Returns the type of 'this' pointer. 1104 /// Should only be called for instance methods. 1105 QualType getThisType(ASTContext &C) const; 1106 1107 unsigned getTypeQualifiers() const { 1108 return getType()->getAs<FunctionProtoType>()->getTypeQuals(); 1109 } 1110 1111 bool hasInlineBody() const; 1112 1113 // Implement isa/cast/dyncast/etc. 1114 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1115 static bool classof(const CXXMethodDecl *D) { return true; } 1116 static bool classofKind(Kind K) { 1117 return K >= firstCXXMethod && K <= lastCXXMethod; 1118 } 1119}; 1120 1121/// CXXBaseOrMemberInitializer - Represents a C++ base or member 1122/// initializer, which is part of a constructor initializer that 1123/// initializes one non-static member variable or one base class. For 1124/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 1125/// initializers: 1126/// 1127/// @code 1128/// class A { }; 1129/// class B : public A { 1130/// float f; 1131/// public: 1132/// B(A& a) : A(a), f(3.14159) { } 1133/// }; 1134/// @endcode 1135class CXXBaseOrMemberInitializer { 1136 /// \brief Either the base class name (stored as a TypeSourceInfo*) or the 1137 /// field being initialized. 1138 llvm::PointerUnion<TypeSourceInfo *, FieldDecl *> BaseOrMember; 1139 1140 /// \brief The source location for the field name. 1141 SourceLocation MemberLocation; 1142 1143 /// \brief The argument used to initialize the base or member, which may 1144 /// end up constructing an object (when multiple arguments are involved). 1145 Stmt *Init; 1146 1147 /// \brief Stores either the constructor to call to initialize this base or 1148 /// member (a CXXConstructorDecl pointer), or stores the anonymous union of 1149 /// which the initialized value is a member. 1150 /// 1151 /// When the value is a FieldDecl pointer, 'BaseOrMember' is class's 1152 /// anonymous union data member, this field holds the FieldDecl for the 1153 /// member of the anonymous union being initialized. 1154 /// @code 1155 /// struct X { 1156 /// X() : au_i1(123) {} 1157 /// union { 1158 /// int au_i1; 1159 /// float au_f1; 1160 /// }; 1161 /// }; 1162 /// @endcode 1163 /// In above example, BaseOrMember holds the field decl. for anonymous union 1164 /// and AnonUnionMember holds field decl for au_i1. 1165 FieldDecl *AnonUnionMember; 1166 1167 /// LParenLoc - Location of the left paren of the ctor-initializer. 1168 SourceLocation LParenLoc; 1169 1170 /// RParenLoc - Location of the right paren of the ctor-initializer. 1171 SourceLocation RParenLoc; 1172 1173 /// IsVirtual - If the initializer is a base initializer, this keeps track 1174 /// of whether the base is virtual or not. 1175 bool IsVirtual : 1; 1176 1177 /// IsWritten - Whether or not the initializer is explicitly written 1178 /// in the sources. 1179 bool IsWritten : 1; 1180 /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this 1181 /// number keeps track of the textual order of this initializer in the 1182 /// original sources, counting from 0; otherwise, if IsWritten is false, 1183 /// it stores the number of array index variables stored after this 1184 /// object in memory. 1185 unsigned SourceOrderOrNumArrayIndices : 14; 1186 1187 CXXBaseOrMemberInitializer(ASTContext &Context, 1188 FieldDecl *Member, SourceLocation MemberLoc, 1189 SourceLocation L, 1190 Expr *Init, 1191 SourceLocation R, 1192 VarDecl **Indices, 1193 unsigned NumIndices); 1194 1195public: 1196 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 1197 explicit 1198 CXXBaseOrMemberInitializer(ASTContext &Context, 1199 TypeSourceInfo *TInfo, bool IsVirtual, 1200 SourceLocation L, 1201 Expr *Init, 1202 SourceLocation R); 1203 1204 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 1205 explicit 1206 CXXBaseOrMemberInitializer(ASTContext &Context, 1207 FieldDecl *Member, SourceLocation MemberLoc, 1208 SourceLocation L, 1209 Expr *Init, 1210 SourceLocation R); 1211 1212 /// \brief Creates a new member initializer that optionally contains 1213 /// array indices used to describe an elementwise initialization. 1214 static CXXBaseOrMemberInitializer *Create(ASTContext &Context, 1215 FieldDecl *Member, 1216 SourceLocation MemberLoc, 1217 SourceLocation L, 1218 Expr *Init, 1219 SourceLocation R, 1220 VarDecl **Indices, 1221 unsigned NumIndices); 1222 1223 /// isBaseInitializer - Returns true when this initializer is 1224 /// initializing a base class. 1225 bool isBaseInitializer() const { return BaseOrMember.is<TypeSourceInfo*>(); } 1226 1227 /// isMemberInitializer - Returns true when this initializer is 1228 /// initializing a non-static data member. 1229 bool isMemberInitializer() const { return BaseOrMember.is<FieldDecl*>(); } 1230 1231 /// If this is a base class initializer, returns the type of the 1232 /// base class with location information. Otherwise, returns an NULL 1233 /// type location. 1234 TypeLoc getBaseClassLoc() const; 1235 1236 /// If this is a base class initializer, returns the type of the base class. 1237 /// Otherwise, returns NULL. 1238 const Type *getBaseClass() const; 1239 Type *getBaseClass(); 1240 1241 /// Returns whether the base is virtual or not. 1242 bool isBaseVirtual() const { 1243 assert(isBaseInitializer() && "Must call this on base initializer!"); 1244 1245 return IsVirtual; 1246 } 1247 1248 /// \brief Returns the declarator information for a base class initializer. 1249 TypeSourceInfo *getBaseClassInfo() const { 1250 return BaseOrMember.dyn_cast<TypeSourceInfo *>(); 1251 } 1252 1253 /// getMember - If this is a member initializer, returns the 1254 /// declaration of the non-static data member being 1255 /// initialized. Otherwise, returns NULL. 1256 FieldDecl *getMember() const { 1257 if (isMemberInitializer()) 1258 return BaseOrMember.get<FieldDecl*>(); 1259 else 1260 return 0; 1261 } 1262 1263 SourceLocation getMemberLocation() const { 1264 return MemberLocation; 1265 } 1266 1267 void setMember(FieldDecl *Member) { 1268 assert(isMemberInitializer()); 1269 BaseOrMember = Member; 1270 } 1271 1272 /// \brief Determine the source location of the initializer. 1273 SourceLocation getSourceLocation() const; 1274 1275 /// \brief Determine the source range covering the entire initializer. 1276 SourceRange getSourceRange() const; 1277 1278 /// isWritten - Returns true if this initializer is explicitly written 1279 /// in the source code. 1280 bool isWritten() const { return IsWritten; } 1281 1282 /// \brief Return the source position of the initializer, counting from 0. 1283 /// If the initializer was implicit, -1 is returned. 1284 int getSourceOrder() const { 1285 return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1; 1286 } 1287 1288 /// \brief Set the source order of this initializer. This method can only 1289 /// be called once for each initializer; it cannot be called on an 1290 /// initializer having a positive number of (implicit) array indices. 1291 void setSourceOrder(int pos) { 1292 assert(!IsWritten && 1293 "calling twice setSourceOrder() on the same initializer"); 1294 assert(SourceOrderOrNumArrayIndices == 0 && 1295 "setSourceOrder() used when there are implicit array indices"); 1296 assert(pos >= 0 && 1297 "setSourceOrder() used to make an initializer implicit"); 1298 IsWritten = true; 1299 SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos); 1300 } 1301 1302 FieldDecl *getAnonUnionMember() const { 1303 return AnonUnionMember; 1304 } 1305 void setAnonUnionMember(FieldDecl *anonMember) { 1306 AnonUnionMember = anonMember; 1307 } 1308 1309 1310 SourceLocation getLParenLoc() const { return LParenLoc; } 1311 SourceLocation getRParenLoc() const { return RParenLoc; } 1312 1313 /// \brief Determine the number of implicit array indices used while 1314 /// described an array member initialization. 1315 unsigned getNumArrayIndices() const { 1316 return IsWritten ? 0 : SourceOrderOrNumArrayIndices; 1317 } 1318 1319 /// \brief Retrieve a particular array index variable used to 1320 /// describe an array member initialization. 1321 VarDecl *getArrayIndex(unsigned I) { 1322 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1323 return reinterpret_cast<VarDecl **>(this + 1)[I]; 1324 } 1325 const VarDecl *getArrayIndex(unsigned I) const { 1326 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1327 return reinterpret_cast<const VarDecl * const *>(this + 1)[I]; 1328 } 1329 void setArrayIndex(unsigned I, VarDecl *Index) { 1330 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1331 reinterpret_cast<VarDecl **>(this + 1)[I] = Index; 1332 } 1333 1334 Expr *getInit() const { return static_cast<Expr *>(Init); } 1335}; 1336 1337/// CXXConstructorDecl - Represents a C++ constructor within a 1338/// class. For example: 1339/// 1340/// @code 1341/// class X { 1342/// public: 1343/// explicit X(int); // represented by a CXXConstructorDecl. 1344/// }; 1345/// @endcode 1346class CXXConstructorDecl : public CXXMethodDecl { 1347 /// IsExplicitSpecified - Whether this constructor declaration has the 1348 /// 'explicit' keyword specified. 1349 bool IsExplicitSpecified : 1; 1350 1351 /// ImplicitlyDefined - Whether this constructor was implicitly 1352 /// defined by the compiler. When false, the constructor was defined 1353 /// by the user. In C++03, this flag will have the same value as 1354 /// Implicit. In C++0x, however, a constructor that is 1355 /// explicitly defaulted (i.e., defined with " = default") will have 1356 /// @c !Implicit && ImplicitlyDefined. 1357 bool ImplicitlyDefined : 1; 1358 1359 /// Support for base and member initializers. 1360 /// BaseOrMemberInitializers - The arguments used to initialize the base 1361 /// or member. 1362 CXXBaseOrMemberInitializer **BaseOrMemberInitializers; 1363 unsigned NumBaseOrMemberInitializers; 1364 1365 CXXConstructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1366 QualType T, TypeSourceInfo *TInfo, 1367 bool isExplicitSpecified, bool isInline, 1368 bool isImplicitlyDeclared) 1369 : CXXMethodDecl(CXXConstructor, RD, NameInfo, T, TInfo, false, 1370 SC_None, isInline), 1371 IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false), 1372 BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) { 1373 setImplicit(isImplicitlyDeclared); 1374 } 1375 1376public: 1377 static CXXConstructorDecl *Create(ASTContext &C, EmptyShell Empty); 1378 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1379 const DeclarationNameInfo &NameInfo, 1380 QualType T, TypeSourceInfo *TInfo, 1381 bool isExplicit, 1382 bool isInline, bool isImplicitlyDeclared); 1383 1384 /// isExplicitSpecified - Whether this constructor declaration has the 1385 /// 'explicit' keyword specified. 1386 bool isExplicitSpecified() const { return IsExplicitSpecified; } 1387 1388 /// isExplicit - Whether this constructor was marked "explicit" or not. 1389 bool isExplicit() const { 1390 return cast<CXXConstructorDecl>(getFirstDeclaration()) 1391 ->isExplicitSpecified(); 1392 } 1393 1394 /// isImplicitlyDefined - Whether this constructor was implicitly 1395 /// defined. If false, then this constructor was defined by the 1396 /// user. This operation can only be invoked if the constructor has 1397 /// already been defined. 1398 bool isImplicitlyDefined() const { 1399 assert(isThisDeclarationADefinition() && 1400 "Can only get the implicit-definition flag once the " 1401 "constructor has been defined"); 1402 return ImplicitlyDefined; 1403 } 1404 1405 /// setImplicitlyDefined - Set whether this constructor was 1406 /// implicitly defined or not. 1407 void setImplicitlyDefined(bool ID) { 1408 assert(isThisDeclarationADefinition() && 1409 "Can only set the implicit-definition flag once the constructor " 1410 "has been defined"); 1411 ImplicitlyDefined = ID; 1412 } 1413 1414 /// init_iterator - Iterates through the member/base initializer list. 1415 typedef CXXBaseOrMemberInitializer **init_iterator; 1416 1417 /// init_const_iterator - Iterates through the memberbase initializer list. 1418 typedef CXXBaseOrMemberInitializer * const * init_const_iterator; 1419 1420 /// init_begin() - Retrieve an iterator to the first initializer. 1421 init_iterator init_begin() { return BaseOrMemberInitializers; } 1422 /// begin() - Retrieve an iterator to the first initializer. 1423 init_const_iterator init_begin() const { return BaseOrMemberInitializers; } 1424 1425 /// init_end() - Retrieve an iterator past the last initializer. 1426 init_iterator init_end() { 1427 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1428 } 1429 /// end() - Retrieve an iterator past the last initializer. 1430 init_const_iterator init_end() const { 1431 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1432 } 1433 1434 typedef std::reverse_iterator<init_iterator> init_reverse_iterator; 1435 typedef std::reverse_iterator<init_const_iterator> init_const_reverse_iterator; 1436 1437 init_reverse_iterator init_rbegin() { 1438 return init_reverse_iterator(init_end()); 1439 } 1440 init_const_reverse_iterator init_rbegin() const { 1441 return init_const_reverse_iterator(init_end()); 1442 } 1443 1444 init_reverse_iterator init_rend() { 1445 return init_reverse_iterator(init_begin()); 1446 } 1447 init_const_reverse_iterator init_rend() const { 1448 return init_const_reverse_iterator(init_begin()); 1449 } 1450 1451 /// getNumArgs - Determine the number of arguments used to 1452 /// initialize the member or base. 1453 unsigned getNumBaseOrMemberInitializers() const { 1454 return NumBaseOrMemberInitializers; 1455 } 1456 1457 void setNumBaseOrMemberInitializers(unsigned numBaseOrMemberInitializers) { 1458 NumBaseOrMemberInitializers = numBaseOrMemberInitializers; 1459 } 1460 1461 void setBaseOrMemberInitializers(CXXBaseOrMemberInitializer ** initializers) { 1462 BaseOrMemberInitializers = initializers; 1463 } 1464 /// isDefaultConstructor - Whether this constructor is a default 1465 /// constructor (C++ [class.ctor]p5), which can be used to 1466 /// default-initialize a class of this type. 1467 bool isDefaultConstructor() const; 1468 1469 /// isCopyConstructor - Whether this constructor is a copy 1470 /// constructor (C++ [class.copy]p2, which can be used to copy the 1471 /// class. @p TypeQuals will be set to the qualifiers on the 1472 /// argument type. For example, @p TypeQuals would be set to @c 1473 /// QualType::Const for the following copy constructor: 1474 /// 1475 /// @code 1476 /// class X { 1477 /// public: 1478 /// X(const X&); 1479 /// }; 1480 /// @endcode 1481 bool isCopyConstructor(unsigned &TypeQuals) const; 1482 1483 /// isCopyConstructor - Whether this constructor is a copy 1484 /// constructor (C++ [class.copy]p2, which can be used to copy the 1485 /// class. 1486 bool isCopyConstructor() const { 1487 unsigned TypeQuals = 0; 1488 return isCopyConstructor(TypeQuals); 1489 } 1490 1491 /// isConvertingConstructor - Whether this constructor is a 1492 /// converting constructor (C++ [class.conv.ctor]), which can be 1493 /// used for user-defined conversions. 1494 bool isConvertingConstructor(bool AllowExplicit) const; 1495 1496 /// \brief Determine whether this is a member template specialization that 1497 /// looks like a copy constructor. Such constructors are never used to copy 1498 /// an object. 1499 bool isCopyConstructorLikeSpecialization() const; 1500 1501 // Implement isa/cast/dyncast/etc. 1502 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1503 static bool classof(const CXXConstructorDecl *D) { return true; } 1504 static bool classofKind(Kind K) { return K == CXXConstructor; } 1505 1506 friend class ASTDeclReader; 1507 friend class ASTDeclWriter; 1508}; 1509 1510/// CXXDestructorDecl - Represents a C++ destructor within a 1511/// class. For example: 1512/// 1513/// @code 1514/// class X { 1515/// public: 1516/// ~X(); // represented by a CXXDestructorDecl. 1517/// }; 1518/// @endcode 1519class CXXDestructorDecl : public CXXMethodDecl { 1520 /// ImplicitlyDefined - Whether this destructor was implicitly 1521 /// defined by the compiler. When false, the destructor was defined 1522 /// by the user. In C++03, this flag will have the same value as 1523 /// Implicit. In C++0x, however, a destructor that is 1524 /// explicitly defaulted (i.e., defined with " = default") will have 1525 /// @c !Implicit && ImplicitlyDefined. 1526 bool ImplicitlyDefined : 1; 1527 1528 FunctionDecl *OperatorDelete; 1529 1530 CXXDestructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1531 QualType T, bool isInline, bool isImplicitlyDeclared) 1532 : CXXMethodDecl(CXXDestructor, RD, NameInfo, T, /*TInfo=*/0, false, 1533 SC_None, isInline), 1534 ImplicitlyDefined(false), OperatorDelete(0) { 1535 setImplicit(isImplicitlyDeclared); 1536 } 1537 1538public: 1539 static CXXDestructorDecl *Create(ASTContext& C, EmptyShell Empty); 1540 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1541 const DeclarationNameInfo &NameInfo, 1542 QualType T, bool isInline, 1543 bool isImplicitlyDeclared); 1544 1545 /// isImplicitlyDefined - Whether this destructor was implicitly 1546 /// defined. If false, then this destructor was defined by the 1547 /// user. This operation can only be invoked if the destructor has 1548 /// already been defined. 1549 bool isImplicitlyDefined() const { 1550 assert(isThisDeclarationADefinition() && 1551 "Can only get the implicit-definition flag once the destructor has been defined"); 1552 return ImplicitlyDefined; 1553 } 1554 1555 /// setImplicitlyDefined - Set whether this destructor was 1556 /// implicitly defined or not. 1557 void setImplicitlyDefined(bool ID) { 1558 assert(isThisDeclarationADefinition() && 1559 "Can only set the implicit-definition flag once the destructor has been defined"); 1560 ImplicitlyDefined = ID; 1561 } 1562 1563 void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; } 1564 const FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1565 1566 // Implement isa/cast/dyncast/etc. 1567 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1568 static bool classof(const CXXDestructorDecl *D) { return true; } 1569 static bool classofKind(Kind K) { return K == CXXDestructor; } 1570 1571 friend class ASTDeclReader; 1572 friend class ASTDeclWriter; 1573}; 1574 1575/// CXXConversionDecl - Represents a C++ conversion function within a 1576/// class. For example: 1577/// 1578/// @code 1579/// class X { 1580/// public: 1581/// operator bool(); 1582/// }; 1583/// @endcode 1584class CXXConversionDecl : public CXXMethodDecl { 1585 /// IsExplicitSpecified - Whether this conversion function declaration is 1586 /// marked "explicit", meaning that it can only be applied when the user 1587 /// explicitly wrote a cast. This is a C++0x feature. 1588 bool IsExplicitSpecified : 1; 1589 1590 CXXConversionDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1591 QualType T, TypeSourceInfo *TInfo, 1592 bool isInline, bool isExplicitSpecified) 1593 : CXXMethodDecl(CXXConversion, RD, NameInfo, T, TInfo, false, 1594 SC_None, isInline), 1595 IsExplicitSpecified(isExplicitSpecified) { } 1596 1597public: 1598 static CXXConversionDecl *Create(ASTContext &C, EmptyShell Empty); 1599 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1600 const DeclarationNameInfo &NameInfo, 1601 QualType T, TypeSourceInfo *TInfo, 1602 bool isInline, bool isExplicit); 1603 1604 /// IsExplicitSpecified - Whether this conversion function declaration is 1605 /// marked "explicit", meaning that it can only be applied when the user 1606 /// explicitly wrote a cast. This is a C++0x feature. 1607 bool isExplicitSpecified() const { return IsExplicitSpecified; } 1608 1609 /// isExplicit - Whether this is an explicit conversion operator 1610 /// (C++0x only). Explicit conversion operators are only considered 1611 /// when the user has explicitly written a cast. 1612 bool isExplicit() const { 1613 return cast<CXXConversionDecl>(getFirstDeclaration()) 1614 ->isExplicitSpecified(); 1615 } 1616 1617 /// getConversionType - Returns the type that this conversion 1618 /// function is converting to. 1619 QualType getConversionType() const { 1620 return getType()->getAs<FunctionType>()->getResultType(); 1621 } 1622 1623 // Implement isa/cast/dyncast/etc. 1624 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1625 static bool classof(const CXXConversionDecl *D) { return true; } 1626 static bool classofKind(Kind K) { return K == CXXConversion; } 1627 1628 friend class ASTDeclReader; 1629 friend class ASTDeclWriter; 1630}; 1631 1632/// LinkageSpecDecl - This represents a linkage specification. For example: 1633/// extern "C" void foo(); 1634/// 1635class LinkageSpecDecl : public Decl, public DeclContext { 1636public: 1637 /// LanguageIDs - Used to represent the language in a linkage 1638 /// specification. The values are part of the serialization abi for 1639 /// ASTs and cannot be changed without altering that abi. To help 1640 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 1641 /// from the dwarf standard. 1642 enum LanguageIDs { 1643 lang_c = /* DW_LANG_C */ 0x0002, 1644 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 1645 }; 1646private: 1647 /// Language - The language for this linkage specification. 1648 LanguageIDs Language; 1649 1650 /// HadBraces - Whether this linkage specification had curly braces or not. 1651 bool HadBraces : 1; 1652 1653 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 1654 bool Braces) 1655 : Decl(LinkageSpec, DC, L), 1656 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 1657 1658public: 1659 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 1660 SourceLocation L, LanguageIDs Lang, 1661 bool Braces); 1662 1663 /// \brief Return the language specified by this linkage specification. 1664 LanguageIDs getLanguage() const { return Language; } 1665 1666 /// \brief Set the language specified by this linkage specification. 1667 void setLanguage(LanguageIDs L) { Language = L; } 1668 1669 /// \brief Determines whether this linkage specification had braces in 1670 /// its syntactic form. 1671 bool hasBraces() const { return HadBraces; } 1672 1673 /// \brief Set whether this linkage specification has braces in its 1674 /// syntactic form. 1675 void setHasBraces(bool B) { HadBraces = B; } 1676 1677 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1678 static bool classof(const LinkageSpecDecl *D) { return true; } 1679 static bool classofKind(Kind K) { return K == LinkageSpec; } 1680 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 1681 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 1682 } 1683 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 1684 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 1685 } 1686}; 1687 1688/// UsingDirectiveDecl - Represents C++ using-directive. For example: 1689/// 1690/// using namespace std; 1691/// 1692// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 1693// artificial name, for all using-directives in order to store 1694// them in DeclContext effectively. 1695class UsingDirectiveDecl : public NamedDecl { 1696 /// \brief The location of the "using" keyword. 1697 SourceLocation UsingLoc; 1698 1699 /// SourceLocation - Location of 'namespace' token. 1700 SourceLocation NamespaceLoc; 1701 1702 /// \brief The source range that covers the nested-name-specifier 1703 /// preceding the namespace name. 1704 SourceRange QualifierRange; 1705 1706 /// \brief The nested-name-specifier that precedes the namespace 1707 /// name, if any. 1708 NestedNameSpecifier *Qualifier; 1709 1710 /// NominatedNamespace - Namespace nominated by using-directive. 1711 NamedDecl *NominatedNamespace; 1712 1713 /// Enclosing context containing both using-directive and nominated 1714 /// namespace. 1715 DeclContext *CommonAncestor; 1716 1717 /// getUsingDirectiveName - Returns special DeclarationName used by 1718 /// using-directives. This is only used by DeclContext for storing 1719 /// UsingDirectiveDecls in its lookup structure. 1720 static DeclarationName getName() { 1721 return DeclarationName::getUsingDirectiveName(); 1722 } 1723 1724 UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc, 1725 SourceLocation NamespcLoc, 1726 SourceRange QualifierRange, 1727 NestedNameSpecifier *Qualifier, 1728 SourceLocation IdentLoc, 1729 NamedDecl *Nominated, 1730 DeclContext *CommonAncestor) 1731 : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc), 1732 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 1733 Qualifier(Qualifier), NominatedNamespace(Nominated), 1734 CommonAncestor(CommonAncestor) { 1735 } 1736 1737public: 1738 /// \brief Retrieve the source range of the nested-name-specifier 1739 /// that qualifies the namespace name. 1740 SourceRange getQualifierRange() const { return QualifierRange; } 1741 1742 /// \brief Retrieve the nested-name-specifier that qualifies the 1743 /// name of the namespace. 1744 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1745 1746 NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; } 1747 const NamedDecl *getNominatedNamespaceAsWritten() const { 1748 return NominatedNamespace; 1749 } 1750 1751 /// getNominatedNamespace - Returns namespace nominated by using-directive. 1752 NamespaceDecl *getNominatedNamespace(); 1753 1754 const NamespaceDecl *getNominatedNamespace() const { 1755 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 1756 } 1757 1758 /// \brief Returns the common ancestor context of this using-directive and 1759 /// its nominated namespace. 1760 DeclContext *getCommonAncestor() { return CommonAncestor; } 1761 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 1762 1763 /// \brief Return the location of the "using" keyword. 1764 SourceLocation getUsingLoc() const { return UsingLoc; } 1765 1766 // FIXME: Could omit 'Key' in name. 1767 /// getNamespaceKeyLocation - Returns location of namespace keyword. 1768 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 1769 1770 /// getIdentLocation - Returns location of identifier. 1771 SourceLocation getIdentLocation() const { return getLocation(); } 1772 1773 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 1774 SourceLocation UsingLoc, 1775 SourceLocation NamespaceLoc, 1776 SourceRange QualifierRange, 1777 NestedNameSpecifier *Qualifier, 1778 SourceLocation IdentLoc, 1779 NamedDecl *Nominated, 1780 DeclContext *CommonAncestor); 1781 1782 SourceRange getSourceRange() const { 1783 return SourceRange(UsingLoc, getLocation()); 1784 } 1785 1786 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1787 static bool classof(const UsingDirectiveDecl *D) { return true; } 1788 static bool classofKind(Kind K) { return K == UsingDirective; } 1789 1790 // Friend for getUsingDirectiveName. 1791 friend class DeclContext; 1792 1793 friend class ASTDeclReader; 1794}; 1795 1796/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 1797/// 1798/// @code 1799/// namespace Foo = Bar; 1800/// @endcode 1801class NamespaceAliasDecl : public NamedDecl { 1802 /// \brief The location of the "namespace" keyword. 1803 SourceLocation NamespaceLoc; 1804 1805 /// \brief The source range that covers the nested-name-specifier 1806 /// preceding the namespace name. 1807 SourceRange QualifierRange; 1808 1809 /// \brief The nested-name-specifier that precedes the namespace 1810 /// name, if any. 1811 NestedNameSpecifier *Qualifier; 1812 1813 /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc. 1814 SourceLocation IdentLoc; 1815 1816 /// Namespace - The Decl that this alias points to. Can either be a 1817 /// NamespaceDecl or a NamespaceAliasDecl. 1818 NamedDecl *Namespace; 1819 1820 NamespaceAliasDecl(DeclContext *DC, SourceLocation NamespaceLoc, 1821 SourceLocation AliasLoc, IdentifierInfo *Alias, 1822 SourceRange QualifierRange, 1823 NestedNameSpecifier *Qualifier, 1824 SourceLocation IdentLoc, NamedDecl *Namespace) 1825 : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), 1826 NamespaceLoc(NamespaceLoc), QualifierRange(QualifierRange), 1827 Qualifier(Qualifier), IdentLoc(IdentLoc), Namespace(Namespace) { } 1828 1829 friend class ASTDeclReader; 1830 1831public: 1832 /// \brief Retrieve the source range of the nested-name-specifier 1833 /// that qualifiers the namespace name. 1834 SourceRange getQualifierRange() const { return QualifierRange; } 1835 1836 /// \brief Set the source range of the nested-name-specifier that qualifies 1837 /// the namespace name. 1838 void setQualifierRange(SourceRange R) { QualifierRange = R; } 1839 1840 /// \brief Retrieve the nested-name-specifier that qualifies the 1841 /// name of the namespace. 1842 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1843 1844 /// \brief Set the nested-name-specifier that qualifies the name of the 1845 /// namespace. 1846 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 1847 1848 /// \brief Retrieve the namespace declaration aliased by this directive. 1849 NamespaceDecl *getNamespace() { 1850 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1851 return AD->getNamespace(); 1852 1853 return cast<NamespaceDecl>(Namespace); 1854 } 1855 1856 const NamespaceDecl *getNamespace() const { 1857 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1858 } 1859 1860 /// Returns the location of the alias name, i.e. 'foo' in 1861 /// "namespace foo = ns::bar;". 1862 SourceLocation getAliasLoc() const { return getLocation(); } 1863 1864 /// Returns the location of the 'namespace' keyword. 1865 SourceLocation getNamespaceLoc() const { return NamespaceLoc; } 1866 1867 /// Returns the location of the identifier in the named namespace. 1868 SourceLocation getTargetNameLoc() const { return IdentLoc; } 1869 1870 /// \brief Retrieve the namespace that this alias refers to, which 1871 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1872 NamedDecl *getAliasedNamespace() const { return Namespace; } 1873 1874 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1875 SourceLocation NamespaceLoc, 1876 SourceLocation AliasLoc, 1877 IdentifierInfo *Alias, 1878 SourceRange QualifierRange, 1879 NestedNameSpecifier *Qualifier, 1880 SourceLocation IdentLoc, 1881 NamedDecl *Namespace); 1882 1883 virtual SourceRange getSourceRange() const { 1884 return SourceRange(NamespaceLoc, IdentLoc); 1885 } 1886 1887 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1888 static bool classof(const NamespaceAliasDecl *D) { return true; } 1889 static bool classofKind(Kind K) { return K == NamespaceAlias; } 1890}; 1891 1892/// UsingShadowDecl - Represents a shadow declaration introduced into 1893/// a scope by a (resolved) using declaration. For example, 1894/// 1895/// namespace A { 1896/// void foo(); 1897/// } 1898/// namespace B { 1899/// using A::foo(); // <- a UsingDecl 1900/// // Also creates a UsingShadowDecl for A::foo in B 1901/// } 1902/// 1903class UsingShadowDecl : public NamedDecl { 1904 /// The referenced declaration. 1905 NamedDecl *Underlying; 1906 1907 /// The using declaration which introduced this decl. 1908 UsingDecl *Using; 1909 1910 UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using, 1911 NamedDecl *Target) 1912 : NamedDecl(UsingShadow, DC, Loc, DeclarationName()), 1913 Underlying(Target), Using(Using) { 1914 if (Target) { 1915 setDeclName(Target->getDeclName()); 1916 IdentifierNamespace = Target->getIdentifierNamespace(); 1917 } 1918 setImplicit(); 1919 } 1920 1921public: 1922 static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC, 1923 SourceLocation Loc, UsingDecl *Using, 1924 NamedDecl *Target) { 1925 return new (C) UsingShadowDecl(DC, Loc, Using, Target); 1926 } 1927 1928 /// \brief Gets the underlying declaration which has been brought into the 1929 /// local scope. 1930 NamedDecl *getTargetDecl() const { return Underlying; } 1931 1932 /// \brief Sets the underlying declaration which has been brought into the 1933 /// local scope. 1934 void setTargetDecl(NamedDecl* ND) { 1935 assert(ND && "Target decl is null!"); 1936 Underlying = ND; 1937 IdentifierNamespace = ND->getIdentifierNamespace(); 1938 } 1939 1940 /// \brief Gets the using declaration to which this declaration is tied. 1941 UsingDecl *getUsingDecl() const { return Using; } 1942 1943 /// \brief Sets the using declaration that introduces this target 1944 /// declaration. 1945 void setUsingDecl(UsingDecl* UD) { Using = UD; } 1946 1947 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1948 static bool classof(const UsingShadowDecl *D) { return true; } 1949 static bool classofKind(Kind K) { return K == Decl::UsingShadow; } 1950}; 1951 1952/// UsingDecl - Represents a C++ using-declaration. For example: 1953/// using someNameSpace::someIdentifier; 1954class UsingDecl : public NamedDecl { 1955 /// \brief The source range that covers the nested-name-specifier 1956 /// preceding the declaration name. 1957 SourceRange NestedNameRange; 1958 1959 /// \brief The source location of the "using" location itself. 1960 SourceLocation UsingLocation; 1961 1962 /// \brief Target nested name specifier. 1963 NestedNameSpecifier* TargetNestedName; 1964 1965 /// DNLoc - Provides source/type location info for the 1966 /// declaration name embedded in the ValueDecl base class. 1967 DeclarationNameLoc DNLoc; 1968 1969 /// \brief The collection of shadow declarations associated with 1970 /// this using declaration. This set can change as a class is 1971 /// processed. 1972 llvm::SmallPtrSet<UsingShadowDecl*, 8> Shadows; 1973 1974 // \brief Has 'typename' keyword. 1975 bool IsTypeName; 1976 1977 UsingDecl(DeclContext *DC, SourceRange NNR, 1978 SourceLocation UL, NestedNameSpecifier* TargetNNS, 1979 const DeclarationNameInfo &NameInfo, bool IsTypeNameArg) 1980 : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()), 1981 NestedNameRange(NNR), UsingLocation(UL), TargetNestedName(TargetNNS), 1982 DNLoc(NameInfo.getInfo()), IsTypeName(IsTypeNameArg) { 1983 } 1984 1985public: 1986 /// \brief Returns the source range that covers the nested-name-specifier 1987 /// preceding the namespace name. 1988 SourceRange getNestedNameRange() const { return NestedNameRange; } 1989 1990 /// \brief Set the source range of the nested-name-specifier. 1991 void setNestedNameRange(SourceRange R) { NestedNameRange = R; } 1992 1993 // FIXME: Naming is inconsistent with other get*Loc functions. 1994 /// \brief Returns the source location of the "using" keyword. 1995 SourceLocation getUsingLocation() const { return UsingLocation; } 1996 1997 /// \brief Set the source location of the 'using' keyword. 1998 void setUsingLocation(SourceLocation L) { UsingLocation = L; } 1999 2000 /// \brief Get the target nested name declaration. 2001 NestedNameSpecifier* getTargetNestedNameDecl() const { 2002 return TargetNestedName; 2003 } 2004 2005 /// \brief Set the target nested name declaration. 2006 void setTargetNestedNameDecl(NestedNameSpecifier *NNS) { 2007 TargetNestedName = NNS; 2008 } 2009 2010 DeclarationNameInfo getNameInfo() const { 2011 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2012 } 2013 2014 /// \brief Return true if the using declaration has 'typename'. 2015 bool isTypeName() const { return IsTypeName; } 2016 2017 /// \brief Sets whether the using declaration has 'typename'. 2018 void setTypeName(bool TN) { IsTypeName = TN; } 2019 2020 typedef llvm::SmallPtrSet<UsingShadowDecl*,8>::const_iterator shadow_iterator; 2021 shadow_iterator shadow_begin() const { return Shadows.begin(); } 2022 shadow_iterator shadow_end() const { return Shadows.end(); } 2023 2024 void addShadowDecl(UsingShadowDecl *S) { 2025 assert(S->getUsingDecl() == this); 2026 if (!Shadows.insert(S)) { 2027 assert(false && "declaration already in set"); 2028 } 2029 } 2030 void removeShadowDecl(UsingShadowDecl *S) { 2031 assert(S->getUsingDecl() == this); 2032 if (!Shadows.erase(S)) { 2033 assert(false && "declaration not in set"); 2034 } 2035 } 2036 2037 /// \brief Return the number of shadowed declarations associated with this 2038 /// using declaration. 2039 unsigned getNumShadowDecls() const { 2040 return Shadows.size(); 2041 } 2042 2043 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 2044 SourceRange NNR, SourceLocation UsingL, 2045 NestedNameSpecifier* TargetNNS, 2046 const DeclarationNameInfo &NameInfo, 2047 bool IsTypeNameArg); 2048 2049 SourceRange getSourceRange() const { 2050 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2051 } 2052 2053 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2054 static bool classof(const UsingDecl *D) { return true; } 2055 static bool classofKind(Kind K) { return K == Using; } 2056 2057 friend class ASTDeclReader; 2058 friend class ASTDeclWriter; 2059}; 2060 2061/// UnresolvedUsingValueDecl - Represents a dependent using 2062/// declaration which was not marked with 'typename'. Unlike 2063/// non-dependent using declarations, these *only* bring through 2064/// non-types; otherwise they would break two-phase lookup. 2065/// 2066/// template <class T> class A : public Base<T> { 2067/// using Base<T>::foo; 2068/// }; 2069class UnresolvedUsingValueDecl : public ValueDecl { 2070 /// \brief The source range that covers the nested-name-specifier 2071 /// preceding the declaration name. 2072 SourceRange TargetNestedNameRange; 2073 2074 /// \brief The source location of the 'using' keyword 2075 SourceLocation UsingLocation; 2076 2077 NestedNameSpecifier *TargetNestedNameSpecifier; 2078 2079 /// DNLoc - Provides source/type location info for the 2080 /// declaration name embedded in the ValueDecl base class. 2081 DeclarationNameLoc DNLoc; 2082 2083 UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty, 2084 SourceLocation UsingLoc, SourceRange TargetNNR, 2085 NestedNameSpecifier *TargetNNS, 2086 const DeclarationNameInfo &NameInfo) 2087 : ValueDecl(UnresolvedUsingValue, DC, 2088 NameInfo.getLoc(), NameInfo.getName(), Ty), 2089 TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc), 2090 TargetNestedNameSpecifier(TargetNNS), DNLoc(NameInfo.getInfo()) 2091 { } 2092 2093public: 2094 /// \brief Returns the source range that covers the nested-name-specifier 2095 /// preceding the namespace name. 2096 SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; } 2097 2098 /// \brief Set the source range coverting the nested-name-specifier preceding 2099 /// the namespace name. 2100 void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; } 2101 2102 /// \brief Get target nested name declaration. 2103 NestedNameSpecifier* getTargetNestedNameSpecifier() const { 2104 return TargetNestedNameSpecifier; 2105 } 2106 2107 /// \brief Set the nested name declaration. 2108 void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) { 2109 TargetNestedNameSpecifier = NNS; 2110 } 2111 2112 /// \brief Returns the source location of the 'using' keyword. 2113 SourceLocation getUsingLoc() const { return UsingLocation; } 2114 2115 /// \brief Set the source location of the 'using' keyword. 2116 void setUsingLoc(SourceLocation L) { UsingLocation = L; } 2117 2118 DeclarationNameInfo getNameInfo() const { 2119 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2120 } 2121 2122 static UnresolvedUsingValueDecl * 2123 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2124 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2125 const DeclarationNameInfo &NameInfo); 2126 2127 SourceRange getSourceRange() const { 2128 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2129 } 2130 2131 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2132 static bool classof(const UnresolvedUsingValueDecl *D) { return true; } 2133 static bool classofKind(Kind K) { return K == UnresolvedUsingValue; } 2134}; 2135 2136/// UnresolvedUsingTypenameDecl - Represents a dependent using 2137/// declaration which was marked with 'typename'. 2138/// 2139/// template <class T> class A : public Base<T> { 2140/// using typename Base<T>::foo; 2141/// }; 2142/// 2143/// The type associated with a unresolved using typename decl is 2144/// currently always a typename type. 2145class UnresolvedUsingTypenameDecl : public TypeDecl { 2146 /// \brief The source range that covers the nested-name-specifier 2147 /// preceding the declaration name. 2148 SourceRange TargetNestedNameRange; 2149 2150 /// \brief The source location of the 'using' keyword 2151 SourceLocation UsingLocation; 2152 2153 /// \brief The source location of the 'typename' keyword 2154 SourceLocation TypenameLocation; 2155 2156 NestedNameSpecifier *TargetNestedNameSpecifier; 2157 2158 UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc, 2159 SourceLocation TypenameLoc, 2160 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2161 SourceLocation TargetNameLoc, IdentifierInfo *TargetName) 2162 : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName), 2163 TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc), 2164 TypenameLocation(TypenameLoc), TargetNestedNameSpecifier(TargetNNS) 2165 { } 2166 2167 friend class ASTDeclReader; 2168 2169public: 2170 /// \brief Returns the source range that covers the nested-name-specifier 2171 /// preceding the namespace name. 2172 SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; } 2173 2174 /// \brief Get target nested name declaration. 2175 NestedNameSpecifier* getTargetNestedNameSpecifier() { 2176 return TargetNestedNameSpecifier; 2177 } 2178 2179 /// \brief Returns the source location of the 'using' keyword. 2180 SourceLocation getUsingLoc() const { return UsingLocation; } 2181 2182 /// \brief Returns the source location of the 'typename' keyword. 2183 SourceLocation getTypenameLoc() const { return TypenameLocation; } 2184 2185 static UnresolvedUsingTypenameDecl * 2186 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2187 SourceLocation TypenameLoc, 2188 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2189 SourceLocation TargetNameLoc, DeclarationName TargetName); 2190 2191 SourceRange getSourceRange() const { 2192 return SourceRange(UsingLocation, getLocation()); 2193 } 2194 2195 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2196 static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; } 2197 static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; } 2198}; 2199 2200/// StaticAssertDecl - Represents a C++0x static_assert declaration. 2201class StaticAssertDecl : public Decl { 2202 Expr *AssertExpr; 2203 StringLiteral *Message; 2204 2205 StaticAssertDecl(DeclContext *DC, SourceLocation L, 2206 Expr *assertexpr, StringLiteral *message) 2207 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 2208 2209public: 2210 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 2211 SourceLocation L, Expr *AssertExpr, 2212 StringLiteral *Message); 2213 2214 Expr *getAssertExpr() { return AssertExpr; } 2215 const Expr *getAssertExpr() const { return AssertExpr; } 2216 2217 StringLiteral *getMessage() { return Message; } 2218 const StringLiteral *getMessage() const { return Message; } 2219 2220 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2221 static bool classof(StaticAssertDecl *D) { return true; } 2222 static bool classofKind(Kind K) { return K == StaticAssert; } 2223 2224 friend class ASTDeclReader; 2225}; 2226 2227/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 2228/// into a diagnostic with <<. 2229const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 2230 AccessSpecifier AS); 2231 2232} // end namespace clang 2233 2234#endif 2235