DeclCXX.h revision af80314a085ba7ee3ca18ca97e75806c87adef02
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 /// setMethodAsVirtual - Make input method virtual and set the necesssary 692 /// special function bits and other bits accordingly. 693 void setMethodAsVirtual(FunctionDecl *Method); 694 695 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 696 /// that is an aggregate that has no non-static non-POD data members, no 697 /// reference data members, no user-defined copy assignment operator and no 698 /// user-defined destructor. 699 bool isPOD() const { return data().PlainOldData; } 700 701 /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which 702 /// means it has a virtual function, virtual base, data member (other than 703 /// 0-width bit-field) or inherits from a non-empty class. Does NOT include 704 /// a check for union-ness. 705 bool isEmpty() const { return data().Empty; } 706 707 /// Set whether this class is empty (C++0x [meta.unary.prop]) 708 void setEmpty(bool Emp) { data().Empty = Emp; } 709 710 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 711 /// which means that the class contains or inherits a virtual function. 712 bool isPolymorphic() const { return data().Polymorphic; } 713 714 /// setPolymorphic - Set whether this class is polymorphic (C++ 715 /// [class.virtual]). 716 void setPolymorphic(bool Poly) { data().Polymorphic = Poly; } 717 718 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 719 /// which means that the class contains or inherits a pure virtual function. 720 bool isAbstract() const { return data().Abstract; } 721 722 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 723 void setAbstract(bool Abs) { data().Abstract = Abs; } 724 725 // hasTrivialConstructor - Whether this class has a trivial constructor 726 // (C++ [class.ctor]p5) 727 bool hasTrivialConstructor() const { return data().HasTrivialConstructor; } 728 729 // setHasTrivialConstructor - Set whether this class has a trivial constructor 730 // (C++ [class.ctor]p5) 731 void setHasTrivialConstructor(bool TC) { data().HasTrivialConstructor = TC; } 732 733 // hasTrivialCopyConstructor - Whether this class has a trivial copy 734 // constructor (C++ [class.copy]p6) 735 bool hasTrivialCopyConstructor() const { 736 return data().HasTrivialCopyConstructor; 737 } 738 739 // setHasTrivialCopyConstructor - Set whether this class has a trivial 740 // copy constructor (C++ [class.copy]p6) 741 void setHasTrivialCopyConstructor(bool TC) { 742 data().HasTrivialCopyConstructor = TC; 743 } 744 745 // hasTrivialCopyAssignment - Whether this class has a trivial copy 746 // assignment operator (C++ [class.copy]p11) 747 bool hasTrivialCopyAssignment() const { 748 return data().HasTrivialCopyAssignment; 749 } 750 751 // setHasTrivialCopyAssignment - Set whether this class has a 752 // trivial copy assignment operator (C++ [class.copy]p11) 753 void setHasTrivialCopyAssignment(bool TC) { 754 data().HasTrivialCopyAssignment = TC; 755 } 756 757 // hasTrivialDestructor - Whether this class has a trivial destructor 758 // (C++ [class.dtor]p3) 759 bool hasTrivialDestructor() const { return data().HasTrivialDestructor; } 760 761 // setHasTrivialDestructor - Set whether this class has a trivial destructor 762 // (C++ [class.dtor]p3) 763 void setHasTrivialDestructor(bool TC) { data().HasTrivialDestructor = TC; } 764 765 /// \brief If this record is an instantiation of a member class, 766 /// retrieves the member class from which it was instantiated. 767 /// 768 /// This routine will return non-NULL for (non-templated) member 769 /// classes of class templates. For example, given: 770 /// 771 /// \code 772 /// template<typename T> 773 /// struct X { 774 /// struct A { }; 775 /// }; 776 /// \endcode 777 /// 778 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 779 /// whose parent is the class template specialization X<int>. For 780 /// this declaration, getInstantiatedFromMemberClass() will return 781 /// the CXXRecordDecl X<T>::A. When a complete definition of 782 /// X<int>::A is required, it will be instantiated from the 783 /// declaration returned by getInstantiatedFromMemberClass(). 784 CXXRecordDecl *getInstantiatedFromMemberClass() const; 785 786 /// \brief If this class is an instantiation of a member class of a 787 /// class template specialization, retrieves the member specialization 788 /// information. 789 MemberSpecializationInfo *getMemberSpecializationInfo() const; 790 791 /// \brief Specify that this record is an instantiation of the 792 /// member class RD. 793 void setInstantiationOfMemberClass(CXXRecordDecl *RD, 794 TemplateSpecializationKind TSK); 795 796 /// \brief Retrieves the class template that is described by this 797 /// class declaration. 798 /// 799 /// Every class template is represented as a ClassTemplateDecl and a 800 /// CXXRecordDecl. The former contains template properties (such as 801 /// the template parameter lists) while the latter contains the 802 /// actual description of the template's 803 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 804 /// CXXRecordDecl that from a ClassTemplateDecl, while 805 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 806 /// a CXXRecordDecl. 807 ClassTemplateDecl *getDescribedClassTemplate() const { 808 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 809 } 810 811 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 812 TemplateOrInstantiation = Template; 813 } 814 815 /// \brief Determine whether this particular class is a specialization or 816 /// instantiation of a class template or member class of a class template, 817 /// and how it was instantiated or specialized. 818 TemplateSpecializationKind getTemplateSpecializationKind() const; 819 820 /// \brief Set the kind of specialization or template instantiation this is. 821 void setTemplateSpecializationKind(TemplateSpecializationKind TSK); 822 823 /// getDestructor - Returns the destructor decl for this class. 824 CXXDestructorDecl *getDestructor() const; 825 826 /// isLocalClass - If the class is a local class [class.local], returns 827 /// the enclosing function declaration. 828 const FunctionDecl *isLocalClass() const { 829 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) 830 return RD->isLocalClass(); 831 832 return dyn_cast<FunctionDecl>(getDeclContext()); 833 } 834 835 /// \brief Determine whether this class is derived from the class \p Base. 836 /// 837 /// This routine only determines whether this class is derived from \p Base, 838 /// but does not account for factors that may make a Derived -> Base class 839 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 840 /// base class subobjects. 841 /// 842 /// \param Base the base class we are searching for. 843 /// 844 /// \returns true if this class is derived from Base, false otherwise. 845 bool isDerivedFrom(CXXRecordDecl *Base) const; 846 847 /// \brief Determine whether this class is derived from the type \p Base. 848 /// 849 /// This routine only determines whether this class is derived from \p Base, 850 /// but does not account for factors that may make a Derived -> Base class 851 /// ill-formed, such as private/protected inheritance or multiple, ambiguous 852 /// base class subobjects. 853 /// 854 /// \param Base the base class we are searching for. 855 /// 856 /// \param Paths will contain the paths taken from the current class to the 857 /// given \p Base class. 858 /// 859 /// \returns true if this class is derived from Base, false otherwise. 860 /// 861 /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than 862 /// tangling input and output in \p Paths 863 bool isDerivedFrom(CXXRecordDecl *Base, CXXBasePaths &Paths) const; 864 865 /// \brief Determine whether this class is virtually derived from 866 /// the class \p Base. 867 /// 868 /// This routine only determines whether this class is virtually 869 /// derived from \p Base, but does not account for factors that may 870 /// make a Derived -> Base class ill-formed, such as 871 /// private/protected inheritance or multiple, ambiguous base class 872 /// subobjects. 873 /// 874 /// \param Base the base class we are searching for. 875 /// 876 /// \returns true if this class is virtually derived from Base, 877 /// false otherwise. 878 bool isVirtuallyDerivedFrom(CXXRecordDecl *Base) const; 879 880 /// \brief Determine whether this class is provably not derived from 881 /// the type \p Base. 882 bool isProvablyNotDerivedFrom(const CXXRecordDecl *Base) const; 883 884 /// \brief Function type used by forallBases() as a callback. 885 /// 886 /// \param Base the definition of the base class 887 /// 888 /// \returns true if this base matched the search criteria 889 typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition, 890 void *UserData); 891 892 /// \brief Determines if the given callback holds for all the direct 893 /// or indirect base classes of this type. 894 /// 895 /// The class itself does not count as a base class. This routine 896 /// returns false if the class has non-computable base classes. 897 /// 898 /// \param AllowShortCircuit if false, forces the callback to be called 899 /// for every base class, even if a dependent or non-matching base was 900 /// found. 901 bool forallBases(ForallBasesCallback *BaseMatches, void *UserData, 902 bool AllowShortCircuit = true) const; 903 904 /// \brief Function type used by lookupInBases() to determine whether a 905 /// specific base class subobject matches the lookup criteria. 906 /// 907 /// \param Specifier the base-class specifier that describes the inheritance 908 /// from the base class we are trying to match. 909 /// 910 /// \param Path the current path, from the most-derived class down to the 911 /// base named by the \p Specifier. 912 /// 913 /// \param UserData a single pointer to user-specified data, provided to 914 /// lookupInBases(). 915 /// 916 /// \returns true if this base matched the search criteria, false otherwise. 917 typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier, 918 CXXBasePath &Path, 919 void *UserData); 920 921 /// \brief Look for entities within the base classes of this C++ class, 922 /// transitively searching all base class subobjects. 923 /// 924 /// This routine uses the callback function \p BaseMatches to find base 925 /// classes meeting some search criteria, walking all base class subobjects 926 /// and populating the given \p Paths structure with the paths through the 927 /// inheritance hierarchy that resulted in a match. On a successful search, 928 /// the \p Paths structure can be queried to retrieve the matching paths and 929 /// to determine if there were any ambiguities. 930 /// 931 /// \param BaseMatches callback function used to determine whether a given 932 /// base matches the user-defined search criteria. 933 /// 934 /// \param UserData user data pointer that will be provided to \p BaseMatches. 935 /// 936 /// \param Paths used to record the paths from this class to its base class 937 /// subobjects that match the search criteria. 938 /// 939 /// \returns true if there exists any path from this class to a base class 940 /// subobject that matches the search criteria. 941 bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData, 942 CXXBasePaths &Paths) const; 943 944 /// \brief Base-class lookup callback that determines whether the given 945 /// base class specifier refers to a specific class declaration. 946 /// 947 /// This callback can be used with \c lookupInBases() to determine whether 948 /// a given derived class has is a base class subobject of a particular type. 949 /// The user data pointer should refer to the canonical CXXRecordDecl of the 950 /// base class that we are searching for. 951 static bool FindBaseClass(const CXXBaseSpecifier *Specifier, 952 CXXBasePath &Path, void *BaseRecord); 953 954 /// \brief Base-class lookup callback that determines whether the 955 /// given base class specifier refers to a specific class 956 /// declaration and describes virtual derivation. 957 /// 958 /// This callback can be used with \c lookupInBases() to determine 959 /// whether a given derived class has is a virtual base class 960 /// subobject of a particular type. The user data pointer should 961 /// refer to the canonical CXXRecordDecl of the base class that we 962 /// are searching for. 963 static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier, 964 CXXBasePath &Path, void *BaseRecord); 965 966 /// \brief Base-class lookup callback that determines whether there exists 967 /// a tag with the given name. 968 /// 969 /// This callback can be used with \c lookupInBases() to find tag members 970 /// of the given name within a C++ class hierarchy. The user data pointer 971 /// is an opaque \c DeclarationName pointer. 972 static bool FindTagMember(const CXXBaseSpecifier *Specifier, 973 CXXBasePath &Path, void *Name); 974 975 /// \brief Base-class lookup callback that determines whether there exists 976 /// a member with the given name. 977 /// 978 /// This callback can be used with \c lookupInBases() to find members 979 /// of the given name within a C++ class hierarchy. The user data pointer 980 /// is an opaque \c DeclarationName pointer. 981 static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier, 982 CXXBasePath &Path, void *Name); 983 984 /// \brief Base-class lookup callback that determines whether there exists 985 /// a member with the given name that can be used in a nested-name-specifier. 986 /// 987 /// This callback can be used with \c lookupInBases() to find membes of 988 /// the given name within a C++ class hierarchy that can occur within 989 /// nested-name-specifiers. 990 static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier, 991 CXXBasePath &Path, 992 void *UserData); 993 994 /// \brief Retrieve the final overriders for each virtual member 995 /// function in the class hierarchy where this class is the 996 /// most-derived class in the class hierarchy. 997 void getFinalOverriders(CXXFinalOverriderMap &FinaOverriders) const; 998 999 /// viewInheritance - Renders and displays an inheritance diagram 1000 /// for this C++ class and all of its base classes (transitively) using 1001 /// GraphViz. 1002 void viewInheritance(ASTContext& Context) const; 1003 1004 /// MergeAccess - Calculates the access of a decl that is reached 1005 /// along a path. 1006 static AccessSpecifier MergeAccess(AccessSpecifier PathAccess, 1007 AccessSpecifier DeclAccess) { 1008 assert(DeclAccess != AS_none); 1009 if (DeclAccess == AS_private) return AS_none; 1010 return (PathAccess > DeclAccess ? PathAccess : DeclAccess); 1011 } 1012 1013 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1014 static bool classofKind(Kind K) { 1015 return K >= firstCXXRecord && K <= lastCXXRecord; 1016 } 1017 static bool classof(const CXXRecordDecl *D) { return true; } 1018 static bool classof(const ClassTemplateSpecializationDecl *D) { 1019 return true; 1020 } 1021 1022 friend class ASTDeclReader; 1023 friend class ASTDeclWriter; 1024}; 1025 1026/// CXXMethodDecl - Represents a static or instance method of a 1027/// struct/union/class. 1028class CXXMethodDecl : public FunctionDecl { 1029protected: 1030 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, 1031 const DeclarationNameInfo &NameInfo, 1032 QualType T, TypeSourceInfo *TInfo, 1033 bool isStatic, StorageClass SCAsWritten, bool isInline) 1034 : FunctionDecl(DK, RD, NameInfo, T, TInfo, (isStatic ? SC_Static : SC_None), 1035 SCAsWritten, isInline) {} 1036 1037public: 1038 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1039 const DeclarationNameInfo &NameInfo, 1040 QualType T, TypeSourceInfo *TInfo, 1041 bool isStatic = false, 1042 StorageClass SCAsWritten = SC_None, 1043 bool isInline = false); 1044 1045 bool isStatic() const { return getStorageClass() == SC_Static; } 1046 bool isInstance() const { return !isStatic(); } 1047 1048 bool isVirtual() const { 1049 CXXMethodDecl *CD = 1050 cast<CXXMethodDecl>(const_cast<CXXMethodDecl*>(this)->getCanonicalDecl()); 1051 1052 if (CD->isVirtualAsWritten()) 1053 return true; 1054 1055 return (CD->begin_overridden_methods() != CD->end_overridden_methods()); 1056 } 1057 1058 /// \brief Determine whether this is a usual deallocation function 1059 /// (C++ [basic.stc.dynamic.deallocation]p2), which is an overloaded 1060 /// delete or delete[] operator with a particular signature. 1061 bool isUsualDeallocationFunction() const; 1062 1063 /// \brief Determine whether this is a copy-assignment operator, regardless 1064 /// of whether it was declared implicitly or explicitly. 1065 bool isCopyAssignmentOperator() const; 1066 1067 const CXXMethodDecl *getCanonicalDecl() const { 1068 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1069 } 1070 CXXMethodDecl *getCanonicalDecl() { 1071 return cast<CXXMethodDecl>(FunctionDecl::getCanonicalDecl()); 1072 } 1073 1074 /// 1075 void addOverriddenMethod(const CXXMethodDecl *MD); 1076 1077 typedef const CXXMethodDecl ** method_iterator; 1078 1079 method_iterator begin_overridden_methods() const; 1080 method_iterator end_overridden_methods() const; 1081 unsigned size_overridden_methods() const; 1082 1083 /// getParent - Returns the parent of this method declaration, which 1084 /// is the class in which this method is defined. 1085 const CXXRecordDecl *getParent() const { 1086 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 1087 } 1088 1089 /// getParent - Returns the parent of this method declaration, which 1090 /// is the class in which this method is defined. 1091 CXXRecordDecl *getParent() { 1092 return const_cast<CXXRecordDecl *>( 1093 cast<CXXRecordDecl>(FunctionDecl::getParent())); 1094 } 1095 1096 /// getThisType - Returns the type of 'this' pointer. 1097 /// Should only be called for instance methods. 1098 QualType getThisType(ASTContext &C) const; 1099 1100 unsigned getTypeQualifiers() const { 1101 return getType()->getAs<FunctionProtoType>()->getTypeQuals(); 1102 } 1103 1104 bool hasInlineBody() const; 1105 1106 // Implement isa/cast/dyncast/etc. 1107 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1108 static bool classof(const CXXMethodDecl *D) { return true; } 1109 static bool classofKind(Kind K) { 1110 return K >= firstCXXMethod && K <= lastCXXMethod; 1111 } 1112}; 1113 1114/// CXXBaseOrMemberInitializer - Represents a C++ base or member 1115/// initializer, which is part of a constructor initializer that 1116/// initializes one non-static member variable or one base class. For 1117/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 1118/// initializers: 1119/// 1120/// @code 1121/// class A { }; 1122/// class B : public A { 1123/// float f; 1124/// public: 1125/// B(A& a) : A(a), f(3.14159) { } 1126/// }; 1127/// @endcode 1128class CXXBaseOrMemberInitializer { 1129 /// \brief Either the base class name (stored as a TypeSourceInfo*) or the 1130 /// field being initialized. 1131 llvm::PointerUnion<TypeSourceInfo *, FieldDecl *> BaseOrMember; 1132 1133 /// \brief The source location for the field name. 1134 SourceLocation MemberLocation; 1135 1136 /// \brief The argument used to initialize the base or member, which may 1137 /// end up constructing an object (when multiple arguments are involved). 1138 Stmt *Init; 1139 1140 /// \brief Stores either the constructor to call to initialize this base or 1141 /// member (a CXXConstructorDecl pointer), or stores the anonymous union of 1142 /// which the initialized value is a member. 1143 /// 1144 /// When the value is a FieldDecl pointer, 'BaseOrMember' is class's 1145 /// anonymous union data member, this field holds the FieldDecl for the 1146 /// member of the anonymous union being initialized. 1147 /// @code 1148 /// struct X { 1149 /// X() : au_i1(123) {} 1150 /// union { 1151 /// int au_i1; 1152 /// float au_f1; 1153 /// }; 1154 /// }; 1155 /// @endcode 1156 /// In above example, BaseOrMember holds the field decl. for anonymous union 1157 /// and AnonUnionMember holds field decl for au_i1. 1158 FieldDecl *AnonUnionMember; 1159 1160 /// LParenLoc - Location of the left paren of the ctor-initializer. 1161 SourceLocation LParenLoc; 1162 1163 /// RParenLoc - Location of the right paren of the ctor-initializer. 1164 SourceLocation RParenLoc; 1165 1166 /// IsVirtual - If the initializer is a base initializer, this keeps track 1167 /// of whether the base is virtual or not. 1168 bool IsVirtual : 1; 1169 1170 /// IsWritten - Whether or not the initializer is explicitly written 1171 /// in the sources. 1172 bool IsWritten : 1; 1173 /// SourceOrderOrNumArrayIndices - If IsWritten is true, then this 1174 /// number keeps track of the textual order of this initializer in the 1175 /// original sources, counting from 0; otherwise, if IsWritten is false, 1176 /// it stores the number of array index variables stored after this 1177 /// object in memory. 1178 unsigned SourceOrderOrNumArrayIndices : 14; 1179 1180 CXXBaseOrMemberInitializer(ASTContext &Context, 1181 FieldDecl *Member, SourceLocation MemberLoc, 1182 SourceLocation L, 1183 Expr *Init, 1184 SourceLocation R, 1185 VarDecl **Indices, 1186 unsigned NumIndices); 1187 1188public: 1189 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 1190 explicit 1191 CXXBaseOrMemberInitializer(ASTContext &Context, 1192 TypeSourceInfo *TInfo, bool IsVirtual, 1193 SourceLocation L, 1194 Expr *Init, 1195 SourceLocation R); 1196 1197 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 1198 explicit 1199 CXXBaseOrMemberInitializer(ASTContext &Context, 1200 FieldDecl *Member, SourceLocation MemberLoc, 1201 SourceLocation L, 1202 Expr *Init, 1203 SourceLocation R); 1204 1205 /// \brief Creates a new member initializer that optionally contains 1206 /// array indices used to describe an elementwise initialization. 1207 static CXXBaseOrMemberInitializer *Create(ASTContext &Context, 1208 FieldDecl *Member, 1209 SourceLocation MemberLoc, 1210 SourceLocation L, 1211 Expr *Init, 1212 SourceLocation R, 1213 VarDecl **Indices, 1214 unsigned NumIndices); 1215 1216 /// isBaseInitializer - Returns true when this initializer is 1217 /// initializing a base class. 1218 bool isBaseInitializer() const { return BaseOrMember.is<TypeSourceInfo*>(); } 1219 1220 /// isMemberInitializer - Returns true when this initializer is 1221 /// initializing a non-static data member. 1222 bool isMemberInitializer() const { return BaseOrMember.is<FieldDecl*>(); } 1223 1224 /// If this is a base class initializer, returns the type of the 1225 /// base class with location information. Otherwise, returns an NULL 1226 /// type location. 1227 TypeLoc getBaseClassLoc() const; 1228 1229 /// If this is a base class initializer, returns the type of the base class. 1230 /// Otherwise, returns NULL. 1231 const Type *getBaseClass() const; 1232 Type *getBaseClass(); 1233 1234 /// Returns whether the base is virtual or not. 1235 bool isBaseVirtual() const { 1236 assert(isBaseInitializer() && "Must call this on base initializer!"); 1237 1238 return IsVirtual; 1239 } 1240 1241 /// \brief Returns the declarator information for a base class initializer. 1242 TypeSourceInfo *getBaseClassInfo() const { 1243 return BaseOrMember.dyn_cast<TypeSourceInfo *>(); 1244 } 1245 1246 /// getMember - If this is a member initializer, returns the 1247 /// declaration of the non-static data member being 1248 /// initialized. Otherwise, returns NULL. 1249 FieldDecl *getMember() const { 1250 if (isMemberInitializer()) 1251 return BaseOrMember.get<FieldDecl*>(); 1252 else 1253 return 0; 1254 } 1255 1256 SourceLocation getMemberLocation() const { 1257 return MemberLocation; 1258 } 1259 1260 void setMember(FieldDecl *Member) { 1261 assert(isMemberInitializer()); 1262 BaseOrMember = Member; 1263 } 1264 1265 /// \brief Determine the source location of the initializer. 1266 SourceLocation getSourceLocation() const; 1267 1268 /// \brief Determine the source range covering the entire initializer. 1269 SourceRange getSourceRange() const; 1270 1271 /// isWritten - Returns true if this initializer is explicitly written 1272 /// in the source code. 1273 bool isWritten() const { return IsWritten; } 1274 1275 /// \brief Return the source position of the initializer, counting from 0. 1276 /// If the initializer was implicit, -1 is returned. 1277 int getSourceOrder() const { 1278 return IsWritten ? static_cast<int>(SourceOrderOrNumArrayIndices) : -1; 1279 } 1280 1281 /// \brief Set the source order of this initializer. This method can only 1282 /// be called once for each initializer; it cannot be called on an 1283 /// initializer having a positive number of (implicit) array indices. 1284 void setSourceOrder(int pos) { 1285 assert(!IsWritten && 1286 "calling twice setSourceOrder() on the same initializer"); 1287 assert(SourceOrderOrNumArrayIndices == 0 && 1288 "setSourceOrder() used when there are implicit array indices"); 1289 assert(pos >= 0 && 1290 "setSourceOrder() used to make an initializer implicit"); 1291 IsWritten = true; 1292 SourceOrderOrNumArrayIndices = static_cast<unsigned>(pos); 1293 } 1294 1295 FieldDecl *getAnonUnionMember() const { 1296 return AnonUnionMember; 1297 } 1298 void setAnonUnionMember(FieldDecl *anonMember) { 1299 AnonUnionMember = anonMember; 1300 } 1301 1302 1303 SourceLocation getLParenLoc() const { return LParenLoc; } 1304 SourceLocation getRParenLoc() const { return RParenLoc; } 1305 1306 /// \brief Determine the number of implicit array indices used while 1307 /// described an array member initialization. 1308 unsigned getNumArrayIndices() const { 1309 return IsWritten ? 0 : SourceOrderOrNumArrayIndices; 1310 } 1311 1312 /// \brief Retrieve a particular array index variable used to 1313 /// describe an array member initialization. 1314 VarDecl *getArrayIndex(unsigned I) { 1315 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1316 return reinterpret_cast<VarDecl **>(this + 1)[I]; 1317 } 1318 const VarDecl *getArrayIndex(unsigned I) const { 1319 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1320 return reinterpret_cast<const VarDecl * const *>(this + 1)[I]; 1321 } 1322 void setArrayIndex(unsigned I, VarDecl *Index) { 1323 assert(I < getNumArrayIndices() && "Out of bounds member array index"); 1324 reinterpret_cast<VarDecl **>(this + 1)[I] = Index; 1325 } 1326 1327 Expr *getInit() const { return static_cast<Expr *>(Init); } 1328}; 1329 1330/// CXXConstructorDecl - Represents a C++ constructor within a 1331/// class. For example: 1332/// 1333/// @code 1334/// class X { 1335/// public: 1336/// explicit X(int); // represented by a CXXConstructorDecl. 1337/// }; 1338/// @endcode 1339class CXXConstructorDecl : public CXXMethodDecl { 1340 /// IsExplicitSpecified - Whether this constructor declaration has the 1341 /// 'explicit' keyword specified. 1342 bool IsExplicitSpecified : 1; 1343 1344 /// ImplicitlyDefined - Whether this constructor was implicitly 1345 /// defined by the compiler. When false, the constructor was defined 1346 /// by the user. In C++03, this flag will have the same value as 1347 /// Implicit. In C++0x, however, a constructor that is 1348 /// explicitly defaulted (i.e., defined with " = default") will have 1349 /// @c !Implicit && ImplicitlyDefined. 1350 bool ImplicitlyDefined : 1; 1351 1352 /// Support for base and member initializers. 1353 /// BaseOrMemberInitializers - The arguments used to initialize the base 1354 /// or member. 1355 CXXBaseOrMemberInitializer **BaseOrMemberInitializers; 1356 unsigned NumBaseOrMemberInitializers; 1357 1358 CXXConstructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1359 QualType T, TypeSourceInfo *TInfo, 1360 bool isExplicitSpecified, bool isInline, 1361 bool isImplicitlyDeclared) 1362 : CXXMethodDecl(CXXConstructor, RD, NameInfo, T, TInfo, false, 1363 SC_None, isInline), 1364 IsExplicitSpecified(isExplicitSpecified), ImplicitlyDefined(false), 1365 BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) { 1366 setImplicit(isImplicitlyDeclared); 1367 } 1368 1369public: 1370 static CXXConstructorDecl *Create(ASTContext &C, EmptyShell Empty); 1371 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1372 const DeclarationNameInfo &NameInfo, 1373 QualType T, TypeSourceInfo *TInfo, 1374 bool isExplicit, 1375 bool isInline, bool isImplicitlyDeclared); 1376 1377 /// isExplicitSpecified - Whether this constructor declaration has the 1378 /// 'explicit' keyword specified. 1379 bool isExplicitSpecified() const { return IsExplicitSpecified; } 1380 1381 /// isExplicit - Whether this constructor was marked "explicit" or not. 1382 bool isExplicit() const { 1383 return cast<CXXConstructorDecl>(getFirstDeclaration()) 1384 ->isExplicitSpecified(); 1385 } 1386 1387 /// isImplicitlyDefined - Whether this constructor was implicitly 1388 /// defined. If false, then this constructor was defined by the 1389 /// user. This operation can only be invoked if the constructor has 1390 /// already been defined. 1391 bool isImplicitlyDefined() const { 1392 assert(isThisDeclarationADefinition() && 1393 "Can only get the implicit-definition flag once the " 1394 "constructor has been defined"); 1395 return ImplicitlyDefined; 1396 } 1397 1398 /// setImplicitlyDefined - Set whether this constructor was 1399 /// implicitly defined or not. 1400 void setImplicitlyDefined(bool ID) { 1401 assert(isThisDeclarationADefinition() && 1402 "Can only set the implicit-definition flag once the constructor " 1403 "has been defined"); 1404 ImplicitlyDefined = ID; 1405 } 1406 1407 /// init_iterator - Iterates through the member/base initializer list. 1408 typedef CXXBaseOrMemberInitializer **init_iterator; 1409 1410 /// init_const_iterator - Iterates through the memberbase initializer list. 1411 typedef CXXBaseOrMemberInitializer * const * init_const_iterator; 1412 1413 /// init_begin() - Retrieve an iterator to the first initializer. 1414 init_iterator init_begin() { return BaseOrMemberInitializers; } 1415 /// begin() - Retrieve an iterator to the first initializer. 1416 init_const_iterator init_begin() const { return BaseOrMemberInitializers; } 1417 1418 /// init_end() - Retrieve an iterator past the last initializer. 1419 init_iterator init_end() { 1420 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1421 } 1422 /// end() - Retrieve an iterator past the last initializer. 1423 init_const_iterator init_end() const { 1424 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1425 } 1426 1427 typedef std::reverse_iterator<init_iterator> init_reverse_iterator; 1428 typedef std::reverse_iterator<init_const_iterator> init_const_reverse_iterator; 1429 1430 init_reverse_iterator init_rbegin() { 1431 return init_reverse_iterator(init_end()); 1432 } 1433 init_const_reverse_iterator init_rbegin() const { 1434 return init_const_reverse_iterator(init_end()); 1435 } 1436 1437 init_reverse_iterator init_rend() { 1438 return init_reverse_iterator(init_begin()); 1439 } 1440 init_const_reverse_iterator init_rend() const { 1441 return init_const_reverse_iterator(init_begin()); 1442 } 1443 1444 /// getNumArgs - Determine the number of arguments used to 1445 /// initialize the member or base. 1446 unsigned getNumBaseOrMemberInitializers() const { 1447 return NumBaseOrMemberInitializers; 1448 } 1449 1450 void setNumBaseOrMemberInitializers(unsigned numBaseOrMemberInitializers) { 1451 NumBaseOrMemberInitializers = numBaseOrMemberInitializers; 1452 } 1453 1454 void setBaseOrMemberInitializers(CXXBaseOrMemberInitializer ** initializers) { 1455 BaseOrMemberInitializers = initializers; 1456 } 1457 /// isDefaultConstructor - Whether this constructor is a default 1458 /// constructor (C++ [class.ctor]p5), which can be used to 1459 /// default-initialize a class of this type. 1460 bool isDefaultConstructor() const; 1461 1462 /// isCopyConstructor - Whether this constructor is a copy 1463 /// constructor (C++ [class.copy]p2, which can be used to copy the 1464 /// class. @p TypeQuals will be set to the qualifiers on the 1465 /// argument type. For example, @p TypeQuals would be set to @c 1466 /// QualType::Const for the following copy constructor: 1467 /// 1468 /// @code 1469 /// class X { 1470 /// public: 1471 /// X(const X&); 1472 /// }; 1473 /// @endcode 1474 bool isCopyConstructor(unsigned &TypeQuals) const; 1475 1476 /// isCopyConstructor - Whether this constructor is a copy 1477 /// constructor (C++ [class.copy]p2, which can be used to copy the 1478 /// class. 1479 bool isCopyConstructor() const { 1480 unsigned TypeQuals = 0; 1481 return isCopyConstructor(TypeQuals); 1482 } 1483 1484 /// isConvertingConstructor - Whether this constructor is a 1485 /// converting constructor (C++ [class.conv.ctor]), which can be 1486 /// used for user-defined conversions. 1487 bool isConvertingConstructor(bool AllowExplicit) const; 1488 1489 /// \brief Determine whether this is a member template specialization that 1490 /// looks like a copy constructor. Such constructors are never used to copy 1491 /// an object. 1492 bool isCopyConstructorLikeSpecialization() const; 1493 1494 // Implement isa/cast/dyncast/etc. 1495 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1496 static bool classof(const CXXConstructorDecl *D) { return true; } 1497 static bool classofKind(Kind K) { return K == CXXConstructor; } 1498 1499 friend class ASTDeclReader; 1500 friend class ASTDeclWriter; 1501}; 1502 1503/// CXXDestructorDecl - Represents a C++ destructor within a 1504/// class. For example: 1505/// 1506/// @code 1507/// class X { 1508/// public: 1509/// ~X(); // represented by a CXXDestructorDecl. 1510/// }; 1511/// @endcode 1512class CXXDestructorDecl : public CXXMethodDecl { 1513 /// ImplicitlyDefined - Whether this destructor was implicitly 1514 /// defined by the compiler. When false, the destructor was defined 1515 /// by the user. In C++03, this flag will have the same value as 1516 /// Implicit. In C++0x, however, a destructor that is 1517 /// explicitly defaulted (i.e., defined with " = default") will have 1518 /// @c !Implicit && ImplicitlyDefined. 1519 bool ImplicitlyDefined : 1; 1520 1521 FunctionDecl *OperatorDelete; 1522 1523 CXXDestructorDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1524 QualType T, bool isInline, bool isImplicitlyDeclared) 1525 : CXXMethodDecl(CXXDestructor, RD, NameInfo, T, /*TInfo=*/0, false, 1526 SC_None, isInline), 1527 ImplicitlyDefined(false), OperatorDelete(0) { 1528 setImplicit(isImplicitlyDeclared); 1529 } 1530 1531public: 1532 static CXXDestructorDecl *Create(ASTContext& C, EmptyShell Empty); 1533 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1534 const DeclarationNameInfo &NameInfo, 1535 QualType T, bool isInline, 1536 bool isImplicitlyDeclared); 1537 1538 /// isImplicitlyDefined - Whether this destructor was implicitly 1539 /// defined. If false, then this destructor was defined by the 1540 /// user. This operation can only be invoked if the destructor has 1541 /// already been defined. 1542 bool isImplicitlyDefined() const { 1543 assert(isThisDeclarationADefinition() && 1544 "Can only get the implicit-definition flag once the destructor has been defined"); 1545 return ImplicitlyDefined; 1546 } 1547 1548 /// setImplicitlyDefined - Set whether this destructor was 1549 /// implicitly defined or not. 1550 void setImplicitlyDefined(bool ID) { 1551 assert(isThisDeclarationADefinition() && 1552 "Can only set the implicit-definition flag once the destructor has been defined"); 1553 ImplicitlyDefined = ID; 1554 } 1555 1556 void setOperatorDelete(FunctionDecl *OD) { OperatorDelete = OD; } 1557 const FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1558 1559 // Implement isa/cast/dyncast/etc. 1560 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1561 static bool classof(const CXXDestructorDecl *D) { return true; } 1562 static bool classofKind(Kind K) { return K == CXXDestructor; } 1563 1564 friend class ASTDeclReader; 1565 friend class ASTDeclWriter; 1566}; 1567 1568/// CXXConversionDecl - Represents a C++ conversion function within a 1569/// class. For example: 1570/// 1571/// @code 1572/// class X { 1573/// public: 1574/// operator bool(); 1575/// }; 1576/// @endcode 1577class CXXConversionDecl : public CXXMethodDecl { 1578 /// IsExplicitSpecified - Whether this conversion function declaration is 1579 /// marked "explicit", meaning that it can only be applied when the user 1580 /// explicitly wrote a cast. This is a C++0x feature. 1581 bool IsExplicitSpecified : 1; 1582 1583 CXXConversionDecl(CXXRecordDecl *RD, const DeclarationNameInfo &NameInfo, 1584 QualType T, TypeSourceInfo *TInfo, 1585 bool isInline, bool isExplicitSpecified) 1586 : CXXMethodDecl(CXXConversion, RD, NameInfo, T, TInfo, false, 1587 SC_None, isInline), 1588 IsExplicitSpecified(isExplicitSpecified) { } 1589 1590public: 1591 static CXXConversionDecl *Create(ASTContext &C, EmptyShell Empty); 1592 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1593 const DeclarationNameInfo &NameInfo, 1594 QualType T, TypeSourceInfo *TInfo, 1595 bool isInline, bool isExplicit); 1596 1597 /// IsExplicitSpecified - Whether this conversion function declaration is 1598 /// marked "explicit", meaning that it can only be applied when the user 1599 /// explicitly wrote a cast. This is a C++0x feature. 1600 bool isExplicitSpecified() const { return IsExplicitSpecified; } 1601 1602 /// isExplicit - Whether this is an explicit conversion operator 1603 /// (C++0x only). Explicit conversion operators are only considered 1604 /// when the user has explicitly written a cast. 1605 bool isExplicit() const { 1606 return cast<CXXConversionDecl>(getFirstDeclaration()) 1607 ->isExplicitSpecified(); 1608 } 1609 1610 /// getConversionType - Returns the type that this conversion 1611 /// function is converting to. 1612 QualType getConversionType() const { 1613 return getType()->getAs<FunctionType>()->getResultType(); 1614 } 1615 1616 // Implement isa/cast/dyncast/etc. 1617 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1618 static bool classof(const CXXConversionDecl *D) { return true; } 1619 static bool classofKind(Kind K) { return K == CXXConversion; } 1620 1621 friend class ASTDeclReader; 1622 friend class ASTDeclWriter; 1623}; 1624 1625/// LinkageSpecDecl - This represents a linkage specification. For example: 1626/// extern "C" void foo(); 1627/// 1628class LinkageSpecDecl : public Decl, public DeclContext { 1629public: 1630 /// LanguageIDs - Used to represent the language in a linkage 1631 /// specification. The values are part of the serialization abi for 1632 /// ASTs and cannot be changed without altering that abi. To help 1633 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 1634 /// from the dwarf standard. 1635 enum LanguageIDs { 1636 lang_c = /* DW_LANG_C */ 0x0002, 1637 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 1638 }; 1639private: 1640 /// Language - The language for this linkage specification. 1641 LanguageIDs Language; 1642 1643 /// HadBraces - Whether this linkage specification had curly braces or not. 1644 bool HadBraces : 1; 1645 1646 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 1647 bool Braces) 1648 : Decl(LinkageSpec, DC, L), 1649 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 1650 1651public: 1652 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 1653 SourceLocation L, LanguageIDs Lang, 1654 bool Braces); 1655 1656 /// \brief Return the language specified by this linkage specification. 1657 LanguageIDs getLanguage() const { return Language; } 1658 1659 /// \brief Set the language specified by this linkage specification. 1660 void setLanguage(LanguageIDs L) { Language = L; } 1661 1662 /// \brief Determines whether this linkage specification had braces in 1663 /// its syntactic form. 1664 bool hasBraces() const { return HadBraces; } 1665 1666 /// \brief Set whether this linkage specification has braces in its 1667 /// syntactic form. 1668 void setHasBraces(bool B) { HadBraces = B; } 1669 1670 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1671 static bool classof(const LinkageSpecDecl *D) { return true; } 1672 static bool classofKind(Kind K) { return K == LinkageSpec; } 1673 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 1674 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 1675 } 1676 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 1677 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 1678 } 1679}; 1680 1681/// UsingDirectiveDecl - Represents C++ using-directive. For example: 1682/// 1683/// using namespace std; 1684/// 1685// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 1686// artificial name, for all using-directives in order to store 1687// them in DeclContext effectively. 1688class UsingDirectiveDecl : public NamedDecl { 1689 /// \brief The location of the "using" keyword. 1690 SourceLocation UsingLoc; 1691 1692 /// SourceLocation - Location of 'namespace' token. 1693 SourceLocation NamespaceLoc; 1694 1695 /// \brief The source range that covers the nested-name-specifier 1696 /// preceding the namespace name. 1697 SourceRange QualifierRange; 1698 1699 /// \brief The nested-name-specifier that precedes the namespace 1700 /// name, if any. 1701 NestedNameSpecifier *Qualifier; 1702 1703 /// NominatedNamespace - Namespace nominated by using-directive. 1704 NamedDecl *NominatedNamespace; 1705 1706 /// Enclosing context containing both using-directive and nominated 1707 /// namespace. 1708 DeclContext *CommonAncestor; 1709 1710 /// getUsingDirectiveName - Returns special DeclarationName used by 1711 /// using-directives. This is only used by DeclContext for storing 1712 /// UsingDirectiveDecls in its lookup structure. 1713 static DeclarationName getName() { 1714 return DeclarationName::getUsingDirectiveName(); 1715 } 1716 1717 UsingDirectiveDecl(DeclContext *DC, SourceLocation UsingLoc, 1718 SourceLocation NamespcLoc, 1719 SourceRange QualifierRange, 1720 NestedNameSpecifier *Qualifier, 1721 SourceLocation IdentLoc, 1722 NamedDecl *Nominated, 1723 DeclContext *CommonAncestor) 1724 : NamedDecl(UsingDirective, DC, IdentLoc, getName()), UsingLoc(UsingLoc), 1725 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 1726 Qualifier(Qualifier), NominatedNamespace(Nominated), 1727 CommonAncestor(CommonAncestor) { 1728 } 1729 1730public: 1731 /// \brief Retrieve the source range of the nested-name-specifier 1732 /// that qualifies the namespace name. 1733 SourceRange getQualifierRange() const { return QualifierRange; } 1734 1735 /// \brief Retrieve the nested-name-specifier that qualifies the 1736 /// name of the namespace. 1737 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1738 1739 NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; } 1740 const NamedDecl *getNominatedNamespaceAsWritten() const { 1741 return NominatedNamespace; 1742 } 1743 1744 /// getNominatedNamespace - Returns namespace nominated by using-directive. 1745 NamespaceDecl *getNominatedNamespace(); 1746 1747 const NamespaceDecl *getNominatedNamespace() const { 1748 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 1749 } 1750 1751 /// \brief Returns the common ancestor context of this using-directive and 1752 /// its nominated namespace. 1753 DeclContext *getCommonAncestor() { return CommonAncestor; } 1754 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 1755 1756 /// \brief Return the location of the "using" keyword. 1757 SourceLocation getUsingLoc() const { return UsingLoc; } 1758 1759 // FIXME: Could omit 'Key' in name. 1760 /// getNamespaceKeyLocation - Returns location of namespace keyword. 1761 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 1762 1763 /// getIdentLocation - Returns location of identifier. 1764 SourceLocation getIdentLocation() const { return getLocation(); } 1765 1766 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 1767 SourceLocation UsingLoc, 1768 SourceLocation NamespaceLoc, 1769 SourceRange QualifierRange, 1770 NestedNameSpecifier *Qualifier, 1771 SourceLocation IdentLoc, 1772 NamedDecl *Nominated, 1773 DeclContext *CommonAncestor); 1774 1775 SourceRange getSourceRange() const { 1776 return SourceRange(UsingLoc, getLocation()); 1777 } 1778 1779 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1780 static bool classof(const UsingDirectiveDecl *D) { return true; } 1781 static bool classofKind(Kind K) { return K == UsingDirective; } 1782 1783 // Friend for getUsingDirectiveName. 1784 friend class DeclContext; 1785 1786 friend class ASTDeclReader; 1787}; 1788 1789/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 1790/// 1791/// @code 1792/// namespace Foo = Bar; 1793/// @endcode 1794class NamespaceAliasDecl : public NamedDecl { 1795 /// \brief The location of the "namespace" keyword. 1796 SourceLocation NamespaceLoc; 1797 1798 /// \brief The source range that covers the nested-name-specifier 1799 /// preceding the namespace name. 1800 SourceRange QualifierRange; 1801 1802 /// \brief The nested-name-specifier that precedes the namespace 1803 /// name, if any. 1804 NestedNameSpecifier *Qualifier; 1805 1806 /// IdentLoc - Location of namespace identifier. Accessed by TargetNameLoc. 1807 SourceLocation IdentLoc; 1808 1809 /// Namespace - The Decl that this alias points to. Can either be a 1810 /// NamespaceDecl or a NamespaceAliasDecl. 1811 NamedDecl *Namespace; 1812 1813 NamespaceAliasDecl(DeclContext *DC, SourceLocation NamespaceLoc, 1814 SourceLocation AliasLoc, IdentifierInfo *Alias, 1815 SourceRange QualifierRange, 1816 NestedNameSpecifier *Qualifier, 1817 SourceLocation IdentLoc, NamedDecl *Namespace) 1818 : NamedDecl(NamespaceAlias, DC, AliasLoc, Alias), 1819 NamespaceLoc(NamespaceLoc), QualifierRange(QualifierRange), 1820 Qualifier(Qualifier), IdentLoc(IdentLoc), Namespace(Namespace) { } 1821 1822 friend class ASTDeclReader; 1823 1824public: 1825 /// \brief Retrieve the source range of the nested-name-specifier 1826 /// that qualifiers the namespace name. 1827 SourceRange getQualifierRange() const { return QualifierRange; } 1828 1829 /// \brief Set the source range of the nested-name-specifier that qualifies 1830 /// the namespace name. 1831 void setQualifierRange(SourceRange R) { QualifierRange = R; } 1832 1833 /// \brief Retrieve the nested-name-specifier that qualifies the 1834 /// name of the namespace. 1835 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1836 1837 /// \brief Set the nested-name-specifier that qualifies the name of the 1838 /// namespace. 1839 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 1840 1841 /// \brief Retrieve the namespace declaration aliased by this directive. 1842 NamespaceDecl *getNamespace() { 1843 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1844 return AD->getNamespace(); 1845 1846 return cast<NamespaceDecl>(Namespace); 1847 } 1848 1849 const NamespaceDecl *getNamespace() const { 1850 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1851 } 1852 1853 /// Returns the location of the alias name, i.e. 'foo' in 1854 /// "namespace foo = ns::bar;". 1855 SourceLocation getAliasLoc() const { return getLocation(); } 1856 1857 /// Returns the location of the 'namespace' keyword. 1858 SourceLocation getNamespaceLoc() const { return NamespaceLoc; } 1859 1860 /// Returns the location of the identifier in the named namespace. 1861 SourceLocation getTargetNameLoc() const { return IdentLoc; } 1862 1863 /// \brief Retrieve the namespace that this alias refers to, which 1864 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1865 NamedDecl *getAliasedNamespace() const { return Namespace; } 1866 1867 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1868 SourceLocation NamespaceLoc, 1869 SourceLocation AliasLoc, 1870 IdentifierInfo *Alias, 1871 SourceRange QualifierRange, 1872 NestedNameSpecifier *Qualifier, 1873 SourceLocation IdentLoc, 1874 NamedDecl *Namespace); 1875 1876 virtual SourceRange getSourceRange() const { 1877 return SourceRange(NamespaceLoc, IdentLoc); 1878 } 1879 1880 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1881 static bool classof(const NamespaceAliasDecl *D) { return true; } 1882 static bool classofKind(Kind K) { return K == NamespaceAlias; } 1883}; 1884 1885/// UsingShadowDecl - Represents a shadow declaration introduced into 1886/// a scope by a (resolved) using declaration. For example, 1887/// 1888/// namespace A { 1889/// void foo(); 1890/// } 1891/// namespace B { 1892/// using A::foo(); // <- a UsingDecl 1893/// // Also creates a UsingShadowDecl for A::foo in B 1894/// } 1895/// 1896class UsingShadowDecl : public NamedDecl { 1897 /// The referenced declaration. 1898 NamedDecl *Underlying; 1899 1900 /// The using declaration which introduced this decl. 1901 UsingDecl *Using; 1902 1903 UsingShadowDecl(DeclContext *DC, SourceLocation Loc, UsingDecl *Using, 1904 NamedDecl *Target) 1905 : NamedDecl(UsingShadow, DC, Loc, DeclarationName()), 1906 Underlying(Target), Using(Using) { 1907 if (Target) { 1908 setDeclName(Target->getDeclName()); 1909 IdentifierNamespace = Target->getIdentifierNamespace(); 1910 } 1911 setImplicit(); 1912 } 1913 1914public: 1915 static UsingShadowDecl *Create(ASTContext &C, DeclContext *DC, 1916 SourceLocation Loc, UsingDecl *Using, 1917 NamedDecl *Target) { 1918 return new (C) UsingShadowDecl(DC, Loc, Using, Target); 1919 } 1920 1921 /// \brief Gets the underlying declaration which has been brought into the 1922 /// local scope. 1923 NamedDecl *getTargetDecl() const { return Underlying; } 1924 1925 /// \brief Sets the underlying declaration which has been brought into the 1926 /// local scope. 1927 void setTargetDecl(NamedDecl* ND) { 1928 assert(ND && "Target decl is null!"); 1929 Underlying = ND; 1930 IdentifierNamespace = ND->getIdentifierNamespace(); 1931 } 1932 1933 /// \brief Gets the using declaration to which this declaration is tied. 1934 UsingDecl *getUsingDecl() const { return Using; } 1935 1936 /// \brief Sets the using declaration that introduces this target 1937 /// declaration. 1938 void setUsingDecl(UsingDecl* UD) { Using = UD; } 1939 1940 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1941 static bool classof(const UsingShadowDecl *D) { return true; } 1942 static bool classofKind(Kind K) { return K == Decl::UsingShadow; } 1943}; 1944 1945/// UsingDecl - Represents a C++ using-declaration. For example: 1946/// using someNameSpace::someIdentifier; 1947class UsingDecl : public NamedDecl { 1948 /// \brief The source range that covers the nested-name-specifier 1949 /// preceding the declaration name. 1950 SourceRange NestedNameRange; 1951 1952 /// \brief The source location of the "using" location itself. 1953 SourceLocation UsingLocation; 1954 1955 /// \brief Target nested name specifier. 1956 NestedNameSpecifier* TargetNestedName; 1957 1958 /// DNLoc - Provides source/type location info for the 1959 /// declaration name embedded in the ValueDecl base class. 1960 DeclarationNameLoc DNLoc; 1961 1962 /// \brief The collection of shadow declarations associated with 1963 /// this using declaration. This set can change as a class is 1964 /// processed. 1965 llvm::SmallPtrSet<UsingShadowDecl*, 8> Shadows; 1966 1967 // \brief Has 'typename' keyword. 1968 bool IsTypeName; 1969 1970 UsingDecl(DeclContext *DC, SourceRange NNR, 1971 SourceLocation UL, NestedNameSpecifier* TargetNNS, 1972 const DeclarationNameInfo &NameInfo, bool IsTypeNameArg) 1973 : NamedDecl(Using, DC, NameInfo.getLoc(), NameInfo.getName()), 1974 NestedNameRange(NNR), UsingLocation(UL), TargetNestedName(TargetNNS), 1975 DNLoc(NameInfo.getInfo()), IsTypeName(IsTypeNameArg) { 1976 } 1977 1978public: 1979 /// \brief Returns the source range that covers the nested-name-specifier 1980 /// preceding the namespace name. 1981 SourceRange getNestedNameRange() const { return NestedNameRange; } 1982 1983 /// \brief Set the source range of the nested-name-specifier. 1984 void setNestedNameRange(SourceRange R) { NestedNameRange = R; } 1985 1986 // FIXME: Naming is inconsistent with other get*Loc functions. 1987 /// \brief Returns the source location of the "using" keyword. 1988 SourceLocation getUsingLocation() const { return UsingLocation; } 1989 1990 /// \brief Set the source location of the 'using' keyword. 1991 void setUsingLocation(SourceLocation L) { UsingLocation = L; } 1992 1993 /// \brief Get the target nested name declaration. 1994 NestedNameSpecifier* getTargetNestedNameDecl() const { 1995 return TargetNestedName; 1996 } 1997 1998 /// \brief Set the target nested name declaration. 1999 void setTargetNestedNameDecl(NestedNameSpecifier *NNS) { 2000 TargetNestedName = NNS; 2001 } 2002 2003 DeclarationNameInfo getNameInfo() const { 2004 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2005 } 2006 2007 /// \brief Return true if the using declaration has 'typename'. 2008 bool isTypeName() const { return IsTypeName; } 2009 2010 /// \brief Sets whether the using declaration has 'typename'. 2011 void setTypeName(bool TN) { IsTypeName = TN; } 2012 2013 typedef llvm::SmallPtrSet<UsingShadowDecl*,8>::const_iterator shadow_iterator; 2014 shadow_iterator shadow_begin() const { return Shadows.begin(); } 2015 shadow_iterator shadow_end() const { return Shadows.end(); } 2016 2017 void addShadowDecl(UsingShadowDecl *S) { 2018 assert(S->getUsingDecl() == this); 2019 if (!Shadows.insert(S)) { 2020 assert(false && "declaration already in set"); 2021 } 2022 } 2023 void removeShadowDecl(UsingShadowDecl *S) { 2024 assert(S->getUsingDecl() == this); 2025 if (!Shadows.erase(S)) { 2026 assert(false && "declaration not in set"); 2027 } 2028 } 2029 2030 /// \brief Return the number of shadowed declarations associated with this 2031 /// using declaration. 2032 unsigned getNumShadowDecls() const { 2033 return Shadows.size(); 2034 } 2035 2036 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 2037 SourceRange NNR, SourceLocation UsingL, 2038 NestedNameSpecifier* TargetNNS, 2039 const DeclarationNameInfo &NameInfo, 2040 bool IsTypeNameArg); 2041 2042 SourceRange getSourceRange() const { 2043 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2044 } 2045 2046 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2047 static bool classof(const UsingDecl *D) { return true; } 2048 static bool classofKind(Kind K) { return K == Using; } 2049 2050 friend class ASTDeclReader; 2051 friend class ASTDeclWriter; 2052}; 2053 2054/// UnresolvedUsingValueDecl - Represents a dependent using 2055/// declaration which was not marked with 'typename'. Unlike 2056/// non-dependent using declarations, these *only* bring through 2057/// non-types; otherwise they would break two-phase lookup. 2058/// 2059/// template <class T> class A : public Base<T> { 2060/// using Base<T>::foo; 2061/// }; 2062class UnresolvedUsingValueDecl : public ValueDecl { 2063 /// \brief The source range that covers the nested-name-specifier 2064 /// preceding the declaration name. 2065 SourceRange TargetNestedNameRange; 2066 2067 /// \brief The source location of the 'using' keyword 2068 SourceLocation UsingLocation; 2069 2070 NestedNameSpecifier *TargetNestedNameSpecifier; 2071 2072 /// DNLoc - Provides source/type location info for the 2073 /// declaration name embedded in the ValueDecl base class. 2074 DeclarationNameLoc DNLoc; 2075 2076 UnresolvedUsingValueDecl(DeclContext *DC, QualType Ty, 2077 SourceLocation UsingLoc, SourceRange TargetNNR, 2078 NestedNameSpecifier *TargetNNS, 2079 const DeclarationNameInfo &NameInfo) 2080 : ValueDecl(UnresolvedUsingValue, DC, 2081 NameInfo.getLoc(), NameInfo.getName(), Ty), 2082 TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc), 2083 TargetNestedNameSpecifier(TargetNNS), DNLoc(NameInfo.getInfo()) 2084 { } 2085 2086public: 2087 /// \brief Returns the source range that covers the nested-name-specifier 2088 /// preceding the namespace name. 2089 SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; } 2090 2091 /// \brief Set the source range coverting the nested-name-specifier preceding 2092 /// the namespace name. 2093 void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; } 2094 2095 /// \brief Get target nested name declaration. 2096 NestedNameSpecifier* getTargetNestedNameSpecifier() const { 2097 return TargetNestedNameSpecifier; 2098 } 2099 2100 /// \brief Set the nested name declaration. 2101 void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) { 2102 TargetNestedNameSpecifier = NNS; 2103 } 2104 2105 /// \brief Returns the source location of the 'using' keyword. 2106 SourceLocation getUsingLoc() const { return UsingLocation; } 2107 2108 /// \brief Set the source location of the 'using' keyword. 2109 void setUsingLoc(SourceLocation L) { UsingLocation = L; } 2110 2111 DeclarationNameInfo getNameInfo() const { 2112 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 2113 } 2114 2115 static UnresolvedUsingValueDecl * 2116 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2117 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2118 const DeclarationNameInfo &NameInfo); 2119 2120 SourceRange getSourceRange() const { 2121 return SourceRange(UsingLocation, getNameInfo().getEndLoc()); 2122 } 2123 2124 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2125 static bool classof(const UnresolvedUsingValueDecl *D) { return true; } 2126 static bool classofKind(Kind K) { return K == UnresolvedUsingValue; } 2127}; 2128 2129/// UnresolvedUsingTypenameDecl - Represents a dependent using 2130/// declaration which was marked with 'typename'. 2131/// 2132/// template <class T> class A : public Base<T> { 2133/// using typename Base<T>::foo; 2134/// }; 2135/// 2136/// The type associated with a unresolved using typename decl is 2137/// currently always a typename type. 2138class UnresolvedUsingTypenameDecl : public TypeDecl { 2139 /// \brief The source range that covers the nested-name-specifier 2140 /// preceding the declaration name. 2141 SourceRange TargetNestedNameRange; 2142 2143 /// \brief The source location of the 'using' keyword 2144 SourceLocation UsingLocation; 2145 2146 /// \brief The source location of the 'typename' keyword 2147 SourceLocation TypenameLocation; 2148 2149 NestedNameSpecifier *TargetNestedNameSpecifier; 2150 2151 UnresolvedUsingTypenameDecl(DeclContext *DC, SourceLocation UsingLoc, 2152 SourceLocation TypenameLoc, 2153 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2154 SourceLocation TargetNameLoc, IdentifierInfo *TargetName) 2155 : TypeDecl(UnresolvedUsingTypename, DC, TargetNameLoc, TargetName), 2156 TargetNestedNameRange(TargetNNR), UsingLocation(UsingLoc), 2157 TypenameLocation(TypenameLoc), TargetNestedNameSpecifier(TargetNNS) 2158 { } 2159 2160 friend class ASTDeclReader; 2161 2162public: 2163 /// \brief Returns the source range that covers the nested-name-specifier 2164 /// preceding the namespace name. 2165 SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; } 2166 2167 /// \brief Get target nested name declaration. 2168 NestedNameSpecifier* getTargetNestedNameSpecifier() { 2169 return TargetNestedNameSpecifier; 2170 } 2171 2172 /// \brief Returns the source location of the 'using' keyword. 2173 SourceLocation getUsingLoc() const { return UsingLocation; } 2174 2175 /// \brief Returns the source location of the 'typename' keyword. 2176 SourceLocation getTypenameLoc() const { return TypenameLocation; } 2177 2178 static UnresolvedUsingTypenameDecl * 2179 Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc, 2180 SourceLocation TypenameLoc, 2181 SourceRange TargetNNR, NestedNameSpecifier *TargetNNS, 2182 SourceLocation TargetNameLoc, DeclarationName TargetName); 2183 2184 SourceRange getSourceRange() const { 2185 return SourceRange(UsingLocation, getLocation()); 2186 } 2187 2188 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2189 static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; } 2190 static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; } 2191}; 2192 2193/// StaticAssertDecl - Represents a C++0x static_assert declaration. 2194class StaticAssertDecl : public Decl { 2195 Expr *AssertExpr; 2196 StringLiteral *Message; 2197 2198 StaticAssertDecl(DeclContext *DC, SourceLocation L, 2199 Expr *assertexpr, StringLiteral *message) 2200 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 2201 2202public: 2203 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 2204 SourceLocation L, Expr *AssertExpr, 2205 StringLiteral *Message); 2206 2207 Expr *getAssertExpr() { return AssertExpr; } 2208 const Expr *getAssertExpr() const { return AssertExpr; } 2209 2210 StringLiteral *getMessage() { return Message; } 2211 const StringLiteral *getMessage() const { return Message; } 2212 2213 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2214 static bool classof(StaticAssertDecl *D) { return true; } 2215 static bool classofKind(Kind K) { return K == StaticAssert; } 2216 2217 friend class ASTDeclReader; 2218}; 2219 2220/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 2221/// into a diagnostic with <<. 2222const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 2223 AccessSpecifier AS); 2224 2225} // end namespace clang 2226 2227#endif 2228