DeclCXX.h revision 193326
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. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECLCXX_H 15#define LLVM_CLANG_AST_DECLCXX_H 16 17#include "clang/AST/Decl.h" 18#include "llvm/ADT/SmallVector.h" 19 20namespace clang { 21 22class ClassTemplateDecl; 23class CXXRecordDecl; 24class CXXConstructorDecl; 25class CXXDestructorDecl; 26class CXXConversionDecl; 27class CXXMethodDecl; 28class ClassTemplateSpecializationDecl; 29 30/// OverloadedFunctionDecl - An instance of this class represents a 31/// set of overloaded functions. All of the functions have the same 32/// name and occur within the same scope. 33/// 34/// An OverloadedFunctionDecl has no ownership over the FunctionDecl 35/// nodes it contains. Rather, the FunctionDecls are owned by the 36/// enclosing scope (which also owns the OverloadedFunctionDecl 37/// node). OverloadedFunctionDecl is used primarily to store a set of 38/// overloaded functions for name lookup. 39class OverloadedFunctionDecl : public NamedDecl { 40protected: 41 OverloadedFunctionDecl(DeclContext *DC, DeclarationName N) 42 : NamedDecl(OverloadedFunction, DC, SourceLocation(), N) { } 43 44 /// Functions - the set of overloaded functions contained in this 45 /// overload set. 46 llvm::SmallVector<FunctionDecl *, 4> Functions; 47 48 // FIXME: This should go away when we stop using 49 // OverloadedFunctionDecl to store conversions in CXXRecordDecl. 50 friend class CXXRecordDecl; 51 52public: 53 typedef llvm::SmallVector<FunctionDecl *, 4>::iterator function_iterator; 54 typedef llvm::SmallVector<FunctionDecl *, 4>::const_iterator 55 function_const_iterator; 56 57 static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC, 58 DeclarationName N); 59 60 /// addOverload - Add an overloaded function FD to this set of 61 /// overloaded functions. 62 void addOverload(FunctionDecl *FD) { 63 assert((FD->getDeclName() == getDeclName() || 64 isa<CXXConversionDecl>(FD) || isa<CXXConstructorDecl>(FD)) && 65 "Overloaded functions must have the same name"); 66 Functions.push_back(FD); 67 68 // An overloaded function declaration always has the location of 69 // the most-recently-added function declaration. 70 if (FD->getLocation().isValid()) 71 this->setLocation(FD->getLocation()); 72 } 73 74 function_iterator function_begin() { return Functions.begin(); } 75 function_iterator function_end() { return Functions.end(); } 76 function_const_iterator function_begin() const { return Functions.begin(); } 77 function_const_iterator function_end() const { return Functions.end(); } 78 79 /// getNumFunctions - the number of overloaded functions stored in 80 /// this set. 81 unsigned getNumFunctions() const { return Functions.size(); } 82 83 /// getFunction - retrieve the ith function in the overload set. 84 const FunctionDecl *getFunction(unsigned i) const { 85 assert(i < getNumFunctions() && "Illegal function #"); 86 return Functions[i]; 87 } 88 FunctionDecl *getFunction(unsigned i) { 89 assert(i < getNumFunctions() && "Illegal function #"); 90 return Functions[i]; 91 } 92 93 // getDeclContext - Get the context of these overloaded functions. 94 DeclContext *getDeclContext() { 95 assert(getNumFunctions() > 0 && "Context of an empty overload set"); 96 return getFunction(0)->getDeclContext(); 97 } 98 99 // Implement isa/cast/dyncast/etc. 100 static bool classof(const Decl *D) { 101 return D->getKind() == OverloadedFunction; 102 } 103 static bool classof(const OverloadedFunctionDecl *D) { return true; } 104}; 105 106/// CXXBaseSpecifier - A base class of a C++ class. 107/// 108/// Each CXXBaseSpecifier represents a single, direct base class (or 109/// struct) of a C++ class (or struct). It specifies the type of that 110/// base class, whether it is a virtual or non-virtual base, and what 111/// level of access (public, protected, private) is used for the 112/// derivation. For example: 113/// 114/// @code 115/// class A { }; 116/// class B { }; 117/// class C : public virtual A, protected B { }; 118/// @endcode 119/// 120/// In this code, C will have two CXXBaseSpecifiers, one for "public 121/// virtual A" and the other for "protected B". 122class CXXBaseSpecifier { 123 /// Range - The source code range that covers the full base 124 /// specifier, including the "virtual" (if present) and access 125 /// specifier (if present). 126 SourceRange Range; 127 128 /// Virtual - Whether this is a virtual base class or not. 129 bool Virtual : 1; 130 131 /// BaseOfClass - Whether this is the base of a class (true) or of a 132 /// struct (false). This determines the mapping from the access 133 /// specifier as written in the source code to the access specifier 134 /// used for semantic analysis. 135 bool BaseOfClass : 1; 136 137 /// Access - Access specifier as written in the source code (which 138 /// may be AS_none). The actual type of data stored here is an 139 /// AccessSpecifier, but we use "unsigned" here to work around a 140 /// VC++ bug. 141 unsigned Access : 2; 142 143 /// BaseType - The type of the base class. This will be a class or 144 /// struct (or a typedef of such). 145 QualType BaseType; 146 147public: 148 CXXBaseSpecifier() { } 149 150 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T) 151 : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { } 152 153 /// getSourceRange - Retrieves the source range that contains the 154 /// entire base specifier. 155 SourceRange getSourceRange() const { return Range; } 156 157 /// isVirtual - Determines whether the base class is a virtual base 158 /// class (or not). 159 bool isVirtual() const { return Virtual; } 160 161 /// getAccessSpecifier - Returns the access specifier for this base 162 /// specifier. This is the actual base specifier as used for 163 /// semantic analysis, so the result can never be AS_none. To 164 /// retrieve the access specifier as written in the source code, use 165 /// getAccessSpecifierAsWritten(). 166 AccessSpecifier getAccessSpecifier() const { 167 if ((AccessSpecifier)Access == AS_none) 168 return BaseOfClass? AS_private : AS_public; 169 else 170 return (AccessSpecifier)Access; 171 } 172 173 /// getAccessSpecifierAsWritten - Retrieves the access specifier as 174 /// written in the source code (which may mean that no access 175 /// specifier was explicitly written). Use getAccessSpecifier() to 176 /// retrieve the access specifier for use in semantic analysis. 177 AccessSpecifier getAccessSpecifierAsWritten() const { 178 return (AccessSpecifier)Access; 179 } 180 181 /// getType - Retrieves the type of the base class. This type will 182 /// always be an unqualified class type. 183 QualType getType() const { return BaseType; } 184}; 185 186/// CXXRecordDecl - Represents a C++ struct/union/class. 187/// FIXME: This class will disappear once we've properly taught RecordDecl 188/// to deal with C++-specific things. 189class CXXRecordDecl : public RecordDecl { 190 /// UserDeclaredConstructor - True when this class has a 191 /// user-declared constructor. 192 bool UserDeclaredConstructor : 1; 193 194 /// UserDeclaredCopyConstructor - True when this class has a 195 /// user-declared copy constructor. 196 bool UserDeclaredCopyConstructor : 1; 197 198 /// UserDeclaredCopyAssignment - True when this class has a 199 /// user-declared copy assignment operator. 200 bool UserDeclaredCopyAssignment : 1; 201 202 /// UserDeclaredDestructor - True when this class has a 203 /// user-declared destructor. 204 bool UserDeclaredDestructor : 1; 205 206 /// Aggregate - True when this class is an aggregate. 207 bool Aggregate : 1; 208 209 /// PlainOldData - True when this class is a POD-type. 210 bool PlainOldData : 1; 211 212 /// Polymorphic - True when this class is polymorphic, i.e. has at least one 213 /// virtual member or derives from a polymorphic class. 214 bool Polymorphic : 1; 215 216 /// Abstract - True when this class is abstract, i.e. has at least one 217 /// pure virtual function, (that can come from a base class). 218 bool Abstract : 1; 219 220 /// HasTrivialConstructor - True when this class has a trivial constructor 221 bool HasTrivialConstructor : 1; 222 223 /// HasTrivialDestructor - True when this class has a trivial destructor 224 bool HasTrivialDestructor : 1; 225 226 /// Bases - Base classes of this class. 227 /// FIXME: This is wasted space for a union. 228 CXXBaseSpecifier *Bases; 229 230 /// NumBases - The number of base class specifiers in Bases. 231 unsigned NumBases; 232 233 /// Conversions - Overload set containing the conversion functions 234 /// of this C++ class (but not its inherited conversion 235 /// functions). Each of the entries in this overload set is a 236 /// CXXConversionDecl. 237 OverloadedFunctionDecl Conversions; 238 239 /// \brief The template or declaration that this declaration 240 /// describes or was instantiated from, respectively. 241 /// 242 /// For non-templates, this value will be NULL. For record 243 /// declarations that describe a class template, this will be a 244 /// pointer to a ClassTemplateDecl. For member 245 /// classes of class template specializations, this will be the 246 /// RecordDecl from which the member class was instantiated. 247 llvm::PointerUnion<ClassTemplateDecl*, CXXRecordDecl*> 248 TemplateOrInstantiation; 249 250protected: 251 CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 252 SourceLocation L, IdentifierInfo *Id); 253 254 ~CXXRecordDecl(); 255 256public: 257 /// base_class_iterator - Iterator that traverses the base classes 258 /// of a clas. 259 typedef CXXBaseSpecifier* base_class_iterator; 260 261 /// base_class_const_iterator - Iterator that traverses the base 262 /// classes of a clas. 263 typedef const CXXBaseSpecifier* base_class_const_iterator; 264 265 static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 266 SourceLocation L, IdentifierInfo *Id, 267 CXXRecordDecl* PrevDecl=0, 268 bool DelayTypeCreation = false); 269 270 /// setBases - Sets the base classes of this struct or class. 271 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases); 272 273 /// getNumBases - Retrieves the number of base classes of this 274 /// class. 275 unsigned getNumBases() const { return NumBases; } 276 277 base_class_iterator bases_begin() { return Bases; } 278 base_class_const_iterator bases_begin() const { return Bases; } 279 base_class_iterator bases_end() { return Bases + NumBases; } 280 base_class_const_iterator bases_end() const { return Bases + NumBases; } 281 282 /// hasConstCopyConstructor - Determines whether this class has a 283 /// copy constructor that accepts a const-qualified argument. 284 bool hasConstCopyConstructor(ASTContext &Context) const; 285 286 /// hasConstCopyAssignment - Determines whether this class has a 287 /// copy assignment operator that accepts a const-qualified argument. 288 bool hasConstCopyAssignment(ASTContext &Context) const; 289 290 /// addedConstructor - Notify the class that another constructor has 291 /// been added. This routine helps maintain information about the 292 /// class based on which constructors have been added. 293 void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl); 294 295 /// hasUserDeclaredConstructor - Whether this class has any 296 /// user-declared constructors. When true, a default constructor 297 /// will not be implicitly declared. 298 bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; } 299 300 /// hasUserDeclaredCopyConstructor - Whether this class has a 301 /// user-declared copy constructor. When false, a copy constructor 302 /// will be implicitly declared. 303 bool hasUserDeclaredCopyConstructor() const { 304 return UserDeclaredCopyConstructor; 305 } 306 307 /// addedAssignmentOperator - Notify the class that another assignment 308 /// operator has been added. This routine helps maintain information about the 309 /// class based on which operators have been added. 310 void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl); 311 312 /// hasUserDeclaredCopyAssignment - Whether this class has a 313 /// user-declared copy assignment operator. When false, a copy 314 /// assigment operator will be implicitly declared. 315 bool hasUserDeclaredCopyAssignment() const { 316 return UserDeclaredCopyAssignment; 317 } 318 319 /// hasUserDeclaredDestructor - Whether this class has a 320 /// user-declared destructor. When false, a destructor will be 321 /// implicitly declared. 322 bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; } 323 324 /// setUserDeclaredDestructor - Set whether this class has a 325 /// user-declared destructor. If not set by the time the class is 326 /// fully defined, a destructor will be implicitly declared. 327 void setUserDeclaredDestructor(bool UCD) { 328 UserDeclaredDestructor = UCD; 329 } 330 331 /// getConversions - Retrieve the overload set containing all of the 332 /// conversion functions in this class. 333 OverloadedFunctionDecl *getConversionFunctions() { 334 return &Conversions; 335 } 336 const OverloadedFunctionDecl *getConversionFunctions() const { 337 return &Conversions; 338 } 339 340 /// addConversionFunction - Add a new conversion function to the 341 /// list of conversion functions. 342 void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl); 343 344 /// isAggregate - Whether this class is an aggregate (C++ 345 /// [dcl.init.aggr]), which is a class with no user-declared 346 /// constructors, no private or protected non-static data members, 347 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 348 bool isAggregate() const { return Aggregate; } 349 350 /// setAggregate - Set whether this class is an aggregate (C++ 351 /// [dcl.init.aggr]). 352 void setAggregate(bool Agg) { Aggregate = Agg; } 353 354 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 355 /// that is an aggregate that has no non-static non-POD data members, no 356 /// reference data members, no user-defined copy assignment operator and no 357 /// user-defined destructor. 358 bool isPOD() const { return PlainOldData; } 359 360 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 361 void setPOD(bool POD) { PlainOldData = POD; } 362 363 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 364 /// which means that the class contains or inherits a virtual function. 365 bool isPolymorphic() const { return Polymorphic; } 366 367 /// setPolymorphic - Set whether this class is polymorphic (C++ 368 /// [class.virtual]). 369 void setPolymorphic(bool Poly) { Polymorphic = Poly; } 370 371 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 372 /// which means that the class contains or inherits a pure virtual function. 373 bool isAbstract() const { return Abstract; } 374 375 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 376 void setAbstract(bool Abs) { Abstract = Abs; } 377 378 // hasTrivialConstructor - Whether this class has a trivial constructor 379 // (C++ [class.ctor]p5) 380 bool hasTrivialConstructor() const { return HasTrivialConstructor; } 381 382 // setHasTrivialConstructor - Set whether this class has a trivial constructor 383 // (C++ [class.ctor]p5) 384 void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; } 385 386 // hasTrivialDestructor - Whether this class has a trivial destructor 387 // (C++ [class.dtor]p3) 388 bool hasTrivialDestructor() const { return HasTrivialDestructor; } 389 390 // setHasTrivialDestructor - Set whether this class has a trivial destructor 391 // (C++ [class.dtor]p3) 392 void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; } 393 394 /// \brief If this record is an instantiation of a member class, 395 /// retrieves the member class from which it was instantiated. 396 /// 397 /// This routine will return non-NULL for (non-templated) member 398 /// classes of class templates. For example, given: 399 /// 400 /// \code 401 /// template<typename T> 402 /// struct X { 403 /// struct A { }; 404 /// }; 405 /// \endcode 406 /// 407 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 408 /// whose parent is the class template specialization X<int>. For 409 /// this declaration, getInstantiatedFromMemberClass() will return 410 /// the CXXRecordDecl X<T>::A. When a complete definition of 411 /// X<int>::A is required, it will be instantiated from the 412 /// declaration returned by getInstantiatedFromMemberClass(). 413 CXXRecordDecl *getInstantiatedFromMemberClass() const { 414 return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>(); 415 } 416 417 /// \brief Specify that this record is an instantiation of the 418 /// member class RD. 419 void setInstantiationOfMemberClass(CXXRecordDecl *RD) { 420 TemplateOrInstantiation = RD; 421 } 422 423 /// \brief Retrieves the class template that is described by this 424 /// class declaration. 425 /// 426 /// Every class template is represented as a ClassTemplateDecl and a 427 /// CXXRecordDecl. The former contains template properties (such as 428 /// the template parameter lists) while the latter contains the 429 /// actual description of the template's 430 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 431 /// CXXRecordDecl that from a ClassTemplateDecl, while 432 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 433 /// a CXXRecordDecl. 434 ClassTemplateDecl *getDescribedClassTemplate() const { 435 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 436 } 437 438 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 439 TemplateOrInstantiation = Template; 440 } 441 442 /// getDestructor - Returns the destructor decl for this class. 443 const CXXDestructorDecl *getDestructor(ASTContext &Context); 444 445 /// viewInheritance - Renders and displays an inheritance diagram 446 /// for this C++ class and all of its base classes (transitively) using 447 /// GraphViz. 448 void viewInheritance(ASTContext& Context) const; 449 450 static bool classof(const Decl *D) { 451 return D->getKind() == CXXRecord || 452 D->getKind() == ClassTemplateSpecialization || 453 D->getKind() == ClassTemplatePartialSpecialization; 454 } 455 static bool classof(const CXXRecordDecl *D) { return true; } 456 static bool classof(const ClassTemplateSpecializationDecl *D) { 457 return true; 458 } 459}; 460 461/// CXXMethodDecl - Represents a static or instance method of a 462/// struct/union/class. 463class CXXMethodDecl : public FunctionDecl { 464protected: 465 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 466 DeclarationName N, QualType T, 467 bool isStatic, bool isInline) 468 : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None), 469 isInline) {} 470 471public: 472 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 473 SourceLocation L, DeclarationName N, 474 QualType T, bool isStatic = false, 475 bool isInline = false); 476 477 bool isStatic() const { return getStorageClass() == Static; } 478 bool isInstance() const { return !isStatic(); } 479 480 bool isOutOfLineDefinition() const { 481 return getLexicalDeclContext() != getDeclContext(); 482 } 483 484 bool isVirtual() const { 485 return isVirtualAsWritten() || 486 (begin_overridden_methods() != end_overridden_methods()); 487 } 488 489 /// 490 void addOverriddenMethod(const CXXMethodDecl *MD); 491 492 typedef const CXXMethodDecl ** method_iterator; 493 494 method_iterator begin_overridden_methods() const; 495 method_iterator end_overridden_methods() const; 496 497 /// getParent - Returns the parent of this method declaration, which 498 /// is the class in which this method is defined. 499 const CXXRecordDecl *getParent() const { 500 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 501 } 502 503 /// getParent - Returns the parent of this method declaration, which 504 /// is the class in which this method is defined. 505 CXXRecordDecl *getParent() { 506 return const_cast<CXXRecordDecl *>( 507 cast<CXXRecordDecl>(FunctionDecl::getParent())); 508 } 509 510 /// getThisType - Returns the type of 'this' pointer. 511 /// Should only be called for instance methods. 512 QualType getThisType(ASTContext &C) const; 513 514 unsigned getTypeQualifiers() const { 515 return getType()->getAsFunctionProtoType()->getTypeQuals(); 516 } 517 518 // Implement isa/cast/dyncast/etc. 519 static bool classof(const Decl *D) { 520 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 521 } 522 static bool classof(const CXXMethodDecl *D) { return true; } 523}; 524 525/// CXXBaseOrMemberInitializer - Represents a C++ base or member 526/// initializer, which is part of a constructor initializer that 527/// initializes one non-static member variable or one base class. For 528/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 529/// initializers: 530/// 531/// @code 532/// class A { }; 533/// class B : public A { 534/// float f; 535/// public: 536/// B(A& a) : A(a), f(3.14159) { } 537/// }; 538class CXXBaseOrMemberInitializer { 539 /// BaseOrMember - This points to the entity being initialized, 540 /// which is either a base class (a Type) or a non-static data 541 /// member. When the low bit is 1, it's a base 542 /// class; when the low bit is 0, it's a member. 543 uintptr_t BaseOrMember; 544 545 /// Args - The arguments used to initialize the base or member. 546 Expr **Args; 547 unsigned NumArgs; 548 549public: 550 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 551 explicit 552 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs); 553 554 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 555 explicit 556 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs); 557 558 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 559 ~CXXBaseOrMemberInitializer(); 560 561 /// arg_iterator - Iterates through the member initialization 562 /// arguments. 563 typedef Expr **arg_iterator; 564 565 /// arg_const_iterator - Iterates through the member initialization 566 /// arguments. 567 typedef Expr * const * arg_const_iterator; 568 569 /// isBaseInitializer - Returns true when this initializer is 570 /// initializing a base class. 571 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 572 573 /// isMemberInitializer - Returns true when this initializer is 574 /// initializing a non-static data member. 575 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 576 577 /// getBaseClass - If this is a base class initializer, returns the 578 /// type used to specify the initializer. The resulting type will be 579 /// a class type or a typedef of a class type. If this is not a base 580 /// class initializer, returns NULL. 581 Type *getBaseClass() { 582 if (isBaseInitializer()) 583 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 584 else 585 return 0; 586 } 587 588 /// getBaseClass - If this is a base class initializer, returns the 589 /// type used to specify the initializer. The resulting type will be 590 /// a class type or a typedef of a class type. If this is not a base 591 /// class initializer, returns NULL. 592 const Type *getBaseClass() const { 593 if (isBaseInitializer()) 594 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 595 else 596 return 0; 597 } 598 599 /// getMember - If this is a member initializer, returns the 600 /// declaration of the non-static data member being 601 /// initialized. Otherwise, returns NULL. 602 FieldDecl *getMember() { 603 if (isMemberInitializer()) 604 return reinterpret_cast<FieldDecl *>(BaseOrMember); 605 else 606 return 0; 607 } 608 609 /// begin() - Retrieve an iterator to the first initializer argument. 610 arg_iterator begin() { return Args; } 611 /// begin() - Retrieve an iterator to the first initializer argument. 612 arg_const_iterator begin() const { return Args; } 613 614 /// end() - Retrieve an iterator past the last initializer argument. 615 arg_iterator end() { return Args + NumArgs; } 616 /// end() - Retrieve an iterator past the last initializer argument. 617 arg_const_iterator end() const { return Args + NumArgs; } 618 619 /// getNumArgs - Determine the number of arguments used to 620 /// initialize the member or base. 621 unsigned getNumArgs() const { return NumArgs; } 622}; 623 624/// CXXConstructorDecl - Represents a C++ constructor within a 625/// class. For example: 626/// 627/// @code 628/// class X { 629/// public: 630/// explicit X(int); // represented by a CXXConstructorDecl. 631/// }; 632/// @endcode 633class CXXConstructorDecl : public CXXMethodDecl { 634 /// Explicit - Whether this constructor is explicit. 635 bool Explicit : 1; 636 637 /// ImplicitlyDefined - Whether this constructor was implicitly 638 /// defined by the compiler. When false, the constructor was defined 639 /// by the user. In C++03, this flag will have the same value as 640 /// Implicit. In C++0x, however, a constructor that is 641 /// explicitly defaulted (i.e., defined with " = default") will have 642 /// @c !Implicit && ImplicitlyDefined. 643 bool ImplicitlyDefined : 1; 644 645 /// FIXME: Add support for base and member initializers. 646 647 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 648 DeclarationName N, QualType T, 649 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 650 : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline), 651 Explicit(isExplicit), ImplicitlyDefined(false) { 652 setImplicit(isImplicitlyDeclared); 653 } 654 655public: 656 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 657 SourceLocation L, DeclarationName N, 658 QualType T, bool isExplicit, 659 bool isInline, bool isImplicitlyDeclared); 660 661 /// isExplicit - Whether this constructor was marked "explicit" or not. 662 bool isExplicit() const { return Explicit; } 663 664 /// isImplicitlyDefined - Whether this constructor was implicitly 665 /// defined. If false, then this constructor was defined by the 666 /// user. This operation can only be invoked if the constructor has 667 /// already been defined. 668 bool isImplicitlyDefined(ASTContext &C) const { 669 assert(isThisDeclarationADefinition() && 670 "Can only get the implicit-definition flag once the constructor has been defined"); 671 return ImplicitlyDefined; 672 } 673 674 /// setImplicitlyDefined - Set whether this constructor was 675 /// implicitly defined or not. 676 void setImplicitlyDefined(bool ID) { 677 assert(isThisDeclarationADefinition() && 678 "Can only set the implicit-definition flag once the constructor has been defined"); 679 ImplicitlyDefined = ID; 680 } 681 682 /// isDefaultConstructor - Whether this constructor is a default 683 /// constructor (C++ [class.ctor]p5), which can be used to 684 /// default-initialize a class of this type. 685 bool isDefaultConstructor() const; 686 687 /// isCopyConstructor - Whether this constructor is a copy 688 /// constructor (C++ [class.copy]p2, which can be used to copy the 689 /// class. @p TypeQuals will be set to the qualifiers on the 690 /// argument type. For example, @p TypeQuals would be set to @c 691 /// QualType::Const for the following copy constructor: 692 /// 693 /// @code 694 /// class X { 695 /// public: 696 /// X(const X&); 697 /// }; 698 /// @endcode 699 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 700 701 /// isCopyConstructor - Whether this constructor is a copy 702 /// constructor (C++ [class.copy]p2, which can be used to copy the 703 /// class. 704 bool isCopyConstructor(ASTContext &Context) const { 705 unsigned TypeQuals = 0; 706 return isCopyConstructor(Context, TypeQuals); 707 } 708 709 /// isConvertingConstructor - Whether this constructor is a 710 /// converting constructor (C++ [class.conv.ctor]), which can be 711 /// used for user-defined conversions. 712 bool isConvertingConstructor() const; 713 714 // Implement isa/cast/dyncast/etc. 715 static bool classof(const Decl *D) { 716 return D->getKind() == CXXConstructor; 717 } 718 static bool classof(const CXXConstructorDecl *D) { return true; } 719}; 720 721/// CXXDestructorDecl - Represents a C++ destructor within a 722/// class. For example: 723/// 724/// @code 725/// class X { 726/// public: 727/// ~X(); // represented by a CXXDestructorDecl. 728/// }; 729/// @endcode 730class CXXDestructorDecl : public CXXMethodDecl { 731 /// ImplicitlyDefined - Whether this destructor was implicitly 732 /// defined by the compiler. When false, the destructor was defined 733 /// by the user. In C++03, this flag will have the same value as 734 /// Implicit. In C++0x, however, a destructor that is 735 /// explicitly defaulted (i.e., defined with " = default") will have 736 /// @c !Implicit && ImplicitlyDefined. 737 bool ImplicitlyDefined : 1; 738 739 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 740 DeclarationName N, QualType T, 741 bool isInline, bool isImplicitlyDeclared) 742 : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline), 743 ImplicitlyDefined(false) { 744 setImplicit(isImplicitlyDeclared); 745 } 746 747public: 748 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 749 SourceLocation L, DeclarationName N, 750 QualType T, bool isInline, 751 bool isImplicitlyDeclared); 752 753 /// isImplicitlyDefined - Whether this destructor was implicitly 754 /// defined. If false, then this destructor was defined by the 755 /// user. This operation can only be invoked if the destructor has 756 /// already been defined. 757 bool isImplicitlyDefined() const { 758 assert(isThisDeclarationADefinition() && 759 "Can only get the implicit-definition flag once the destructor has been defined"); 760 return ImplicitlyDefined; 761 } 762 763 /// setImplicitlyDefined - Set whether this destructor was 764 /// implicitly defined or not. 765 void setImplicitlyDefined(bool ID) { 766 assert(isThisDeclarationADefinition() && 767 "Can only set the implicit-definition flag once the destructor has been defined"); 768 ImplicitlyDefined = ID; 769 } 770 771 // Implement isa/cast/dyncast/etc. 772 static bool classof(const Decl *D) { 773 return D->getKind() == CXXDestructor; 774 } 775 static bool classof(const CXXDestructorDecl *D) { return true; } 776}; 777 778/// CXXConversionDecl - Represents a C++ conversion function within a 779/// class. For example: 780/// 781/// @code 782/// class X { 783/// public: 784/// operator bool(); 785/// }; 786/// @endcode 787class CXXConversionDecl : public CXXMethodDecl { 788 /// Explicit - Whether this conversion function is marked 789 /// "explicit", meaning that it can only be applied when the user 790 /// explicitly wrote a cast. This is a C++0x feature. 791 bool Explicit : 1; 792 793 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 794 DeclarationName N, QualType T, 795 bool isInline, bool isExplicit) 796 : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline), 797 Explicit(isExplicit) { } 798 799public: 800 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 801 SourceLocation L, DeclarationName N, 802 QualType T, bool isInline, 803 bool isExplicit); 804 805 /// isExplicit - Whether this is an explicit conversion operator 806 /// (C++0x only). Explicit conversion operators are only considered 807 /// when the user has explicitly written a cast. 808 bool isExplicit() const { return Explicit; } 809 810 /// getConversionType - Returns the type that this conversion 811 /// function is converting to. 812 QualType getConversionType() const { 813 return getType()->getAsFunctionType()->getResultType(); 814 } 815 816 // Implement isa/cast/dyncast/etc. 817 static bool classof(const Decl *D) { 818 return D->getKind() == CXXConversion; 819 } 820 static bool classof(const CXXConversionDecl *D) { return true; } 821}; 822 823/// LinkageSpecDecl - This represents a linkage specification. For example: 824/// extern "C" void foo(); 825/// 826class LinkageSpecDecl : public Decl, public DeclContext { 827public: 828 /// LanguageIDs - Used to represent the language in a linkage 829 /// specification. The values are part of the serialization abi for 830 /// ASTs and cannot be changed without altering that abi. To help 831 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 832 /// from the dwarf standard. 833 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 834 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 835private: 836 /// Language - The language for this linkage specification. 837 LanguageIDs Language; 838 839 /// HadBraces - Whether this linkage specification had curly braces or not. 840 bool HadBraces : 1; 841 842 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 843 bool Braces) 844 : Decl(LinkageSpec, DC, L), 845 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 846 847public: 848 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 849 SourceLocation L, LanguageIDs Lang, 850 bool Braces); 851 852 LanguageIDs getLanguage() const { return Language; } 853 854 /// hasBraces - Determines whether this linkage specification had 855 /// braces in its syntactic form. 856 bool hasBraces() const { return HadBraces; } 857 858 static bool classof(const Decl *D) { 859 return D->getKind() == LinkageSpec; 860 } 861 static bool classof(const LinkageSpecDecl *D) { return true; } 862 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 863 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 864 } 865 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 866 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 867 } 868}; 869 870/// UsingDirectiveDecl - Represents C++ using-directive. For example: 871/// 872/// using namespace std; 873/// 874// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 875// artificial name, for all using-directives in order to store 876// them in DeclContext effectively. 877class UsingDirectiveDecl : public NamedDecl { 878 879 /// SourceLocation - Location of 'namespace' token. 880 SourceLocation NamespaceLoc; 881 882 /// \brief The source range that covers the nested-name-specifier 883 /// preceding the namespace name. 884 SourceRange QualifierRange; 885 886 /// \brief The nested-name-specifier that precedes the namespace 887 /// name, if any. 888 NestedNameSpecifier *Qualifier; 889 890 /// IdentLoc - Location of nominated namespace-name identifier. 891 // FIXME: We don't store location of scope specifier. 892 SourceLocation IdentLoc; 893 894 /// NominatedNamespace - Namespace nominated by using-directive. 895 NamespaceDecl *NominatedNamespace; 896 897 /// Enclosing context containing both using-directive and nomintated 898 /// namespace. 899 DeclContext *CommonAncestor; 900 901 /// getUsingDirectiveName - Returns special DeclarationName used by 902 /// using-directives. This is only used by DeclContext for storing 903 /// UsingDirectiveDecls in its lookup structure. 904 static DeclarationName getName() { 905 return DeclarationName::getUsingDirectiveName(); 906 } 907 908 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 909 SourceLocation NamespcLoc, 910 SourceRange QualifierRange, 911 NestedNameSpecifier *Qualifier, 912 SourceLocation IdentLoc, 913 NamespaceDecl *Nominated, 914 DeclContext *CommonAncestor) 915 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 916 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 917 Qualifier(Qualifier), IdentLoc(IdentLoc), 918 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 919 CommonAncestor(CommonAncestor) { 920 } 921 922public: 923 /// \brief Retrieve the source range of the nested-name-specifier 924 /// that qualifiers the namespace name. 925 SourceRange getQualifierRange() const { return QualifierRange; } 926 927 /// \brief Retrieve the nested-name-specifier that qualifies the 928 /// name of the namespace. 929 NestedNameSpecifier *getQualifier() const { return Qualifier; } 930 931 /// getNominatedNamespace - Returns namespace nominated by using-directive. 932 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 933 934 const NamespaceDecl *getNominatedNamespace() const { 935 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 936 } 937 938 /// getCommonAncestor - returns common ancestor context of using-directive, 939 /// and nominated by it namespace. 940 DeclContext *getCommonAncestor() { return CommonAncestor; } 941 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 942 943 /// getNamespaceKeyLocation - Returns location of namespace keyword. 944 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 945 946 /// getIdentLocation - Returns location of identifier. 947 SourceLocation getIdentLocation() const { return IdentLoc; } 948 949 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 950 SourceLocation L, 951 SourceLocation NamespaceLoc, 952 SourceRange QualifierRange, 953 NestedNameSpecifier *Qualifier, 954 SourceLocation IdentLoc, 955 NamespaceDecl *Nominated, 956 DeclContext *CommonAncestor); 957 958 static bool classof(const Decl *D) { 959 return D->getKind() == Decl::UsingDirective; 960 } 961 static bool classof(const UsingDirectiveDecl *D) { return true; } 962 963 // Friend for getUsingDirectiveName. 964 friend class DeclContext; 965}; 966 967/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 968/// 969/// @code 970/// namespace Foo = Bar; 971/// @endcode 972class NamespaceAliasDecl : public NamedDecl { 973 SourceLocation AliasLoc; 974 975 /// \brief The source range that covers the nested-name-specifier 976 /// preceding the namespace name. 977 SourceRange QualifierRange; 978 979 /// \brief The nested-name-specifier that precedes the namespace 980 /// name, if any. 981 NestedNameSpecifier *Qualifier; 982 983 /// IdentLoc - Location of namespace identifier. 984 SourceLocation IdentLoc; 985 986 /// Namespace - The Decl that this alias points to. Can either be a 987 /// NamespaceDecl or a NamespaceAliasDecl. 988 NamedDecl *Namespace; 989 990 NamespaceAliasDecl(DeclContext *DC, SourceLocation L, 991 SourceLocation AliasLoc, IdentifierInfo *Alias, 992 SourceRange QualifierRange, 993 NestedNameSpecifier *Qualifier, 994 SourceLocation IdentLoc, NamedDecl *Namespace) 995 : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), 996 QualifierRange(QualifierRange), Qualifier(Qualifier), 997 IdentLoc(IdentLoc), Namespace(Namespace) { } 998 999public: 1000 /// \brief Retrieve the source range of the nested-name-specifier 1001 /// that qualifiers the namespace name. 1002 SourceRange getQualifierRange() const { return QualifierRange; } 1003 1004 /// \brief Retrieve the nested-name-specifier that qualifies the 1005 /// name of the namespace. 1006 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1007 1008 NamespaceDecl *getNamespace() { 1009 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1010 return AD->getNamespace(); 1011 1012 return cast<NamespaceDecl>(Namespace); 1013 } 1014 1015 const NamespaceDecl *getNamespace() const { 1016 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1017 } 1018 1019 /// \brief Retrieve the namespace that this alias refers to, which 1020 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1021 NamedDecl *getAliasedNamespace() const { return Namespace; } 1022 1023 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1024 SourceLocation L, SourceLocation AliasLoc, 1025 IdentifierInfo *Alias, 1026 SourceRange QualifierRange, 1027 NestedNameSpecifier *Qualifier, 1028 SourceLocation IdentLoc, 1029 NamedDecl *Namespace); 1030 1031 static bool classof(const Decl *D) { 1032 return D->getKind() == Decl::NamespaceAlias; 1033 } 1034 static bool classof(const NamespaceAliasDecl *D) { return true; } 1035}; 1036 1037/// StaticAssertDecl - Represents a C++0x static_assert declaration. 1038class StaticAssertDecl : public Decl { 1039 Expr *AssertExpr; 1040 StringLiteral *Message; 1041 1042 StaticAssertDecl(DeclContext *DC, SourceLocation L, 1043 Expr *assertexpr, StringLiteral *message) 1044 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 1045 1046public: 1047 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 1048 SourceLocation L, Expr *AssertExpr, 1049 StringLiteral *Message); 1050 1051 Expr *getAssertExpr() { return AssertExpr; } 1052 const Expr *getAssertExpr() const { return AssertExpr; } 1053 1054 StringLiteral *getMessage() { return Message; } 1055 const StringLiteral *getMessage() const { return Message; } 1056 1057 virtual ~StaticAssertDecl(); 1058 virtual void Destroy(ASTContext& C); 1059 1060 static bool classof(const Decl *D) { 1061 return D->getKind() == Decl::StaticAssert; 1062 } 1063 static bool classof(StaticAssertDecl *D) { return true; } 1064}; 1065 1066/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 1067/// into a diagnostic with <<. 1068const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1069 AccessSpecifier AS); 1070 1071} // end namespace clang 1072 1073#endif 1074