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