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