DeclCXX.h revision 8419fa3af97208eb00f0cd6c62354ce4ff986677
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 } 454 static bool classof(const CXXRecordDecl *D) { return true; } 455 static bool classof(const ClassTemplateSpecializationDecl *D) { 456 return true; 457 } 458}; 459 460/// CXXMethodDecl - Represents a static or instance method of a 461/// struct/union/class. 462class CXXMethodDecl : public FunctionDecl { 463protected: 464 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 465 DeclarationName N, QualType T, 466 bool isStatic, bool isInline) 467 : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None), 468 isInline) {} 469 470public: 471 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 472 SourceLocation L, DeclarationName N, 473 QualType T, bool isStatic = false, 474 bool isInline = false); 475 476 bool isStatic() const { return getStorageClass() == Static; } 477 bool isInstance() const { return !isStatic(); } 478 479 bool isOutOfLineDefinition() const { 480 return getLexicalDeclContext() != getDeclContext(); 481 } 482 483 bool isVirtual() const { 484 return isVirtualAsWritten() || 485 (begin_overridden_methods() != end_overridden_methods()); 486 } 487 488 /// 489 void addOverriddenMethod(const CXXMethodDecl *MD); 490 491 typedef const CXXMethodDecl ** method_iterator; 492 493 method_iterator begin_overridden_methods() const; 494 method_iterator end_overridden_methods() const; 495 496 /// getParent - Returns the parent of this method declaration, which 497 /// is the class in which this method is defined. 498 const CXXRecordDecl *getParent() const { 499 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 500 } 501 502 /// getParent - Returns the parent of this method declaration, which 503 /// is the class in which this method is defined. 504 CXXRecordDecl *getParent() { 505 return const_cast<CXXRecordDecl *>( 506 cast<CXXRecordDecl>(FunctionDecl::getParent())); 507 } 508 509 /// getThisType - Returns the type of 'this' pointer. 510 /// Should only be called for instance methods. 511 QualType getThisType(ASTContext &C) const; 512 513 unsigned getTypeQualifiers() const { 514 return getType()->getAsFunctionProtoType()->getTypeQuals(); 515 } 516 517 // Implement isa/cast/dyncast/etc. 518 static bool classof(const Decl *D) { 519 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 520 } 521 static bool classof(const CXXMethodDecl *D) { return true; } 522}; 523 524/// CXXBaseOrMemberInitializer - Represents a C++ base or member 525/// initializer, which is part of a constructor initializer that 526/// initializes one non-static member variable or one base class. For 527/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 528/// initializers: 529/// 530/// @code 531/// class A { }; 532/// class B : public A { 533/// float f; 534/// public: 535/// B(A& a) : A(a), f(3.14159) { } 536/// }; 537class CXXBaseOrMemberInitializer { 538 /// BaseOrMember - This points to the entity being initialized, 539 /// which is either a base class (a Type) or a non-static data 540 /// member. When the low bit is 1, it's a base 541 /// class; when the low bit is 0, it's a member. 542 uintptr_t BaseOrMember; 543 544 /// Args - The arguments used to initialize the base or member. 545 Expr **Args; 546 unsigned NumArgs; 547 548public: 549 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 550 explicit 551 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs); 552 553 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 554 explicit 555 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs); 556 557 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 558 ~CXXBaseOrMemberInitializer(); 559 560 /// arg_iterator - Iterates through the member initialization 561 /// arguments. 562 typedef Expr **arg_iterator; 563 564 /// arg_const_iterator - Iterates through the member initialization 565 /// arguments. 566 typedef Expr * const * arg_const_iterator; 567 568 /// isBaseInitializer - Returns true when this initializer is 569 /// initializing a base class. 570 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 571 572 /// isMemberInitializer - Returns true when this initializer is 573 /// initializing a non-static data member. 574 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 575 576 /// getBaseClass - If this is a base class initializer, returns the 577 /// type used to specify the initializer. The resulting type will be 578 /// a class type or a typedef of a class type. If this is not a base 579 /// class initializer, returns NULL. 580 Type *getBaseClass() { 581 if (isBaseInitializer()) 582 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 583 else 584 return 0; 585 } 586 587 /// getBaseClass - If this is a base class initializer, returns the 588 /// type used to specify the initializer. The resulting type will be 589 /// a class type or a typedef of a class type. If this is not a base 590 /// class initializer, returns NULL. 591 const Type *getBaseClass() const { 592 if (isBaseInitializer()) 593 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 594 else 595 return 0; 596 } 597 598 /// getMember - If this is a member initializer, returns the 599 /// declaration of the non-static data member being 600 /// initialized. Otherwise, returns NULL. 601 FieldDecl *getMember() { 602 if (isMemberInitializer()) 603 return reinterpret_cast<FieldDecl *>(BaseOrMember); 604 else 605 return 0; 606 } 607 608 /// begin() - Retrieve an iterator to the first initializer argument. 609 arg_iterator begin() { return Args; } 610 /// begin() - Retrieve an iterator to the first initializer argument. 611 arg_const_iterator begin() const { return Args; } 612 613 /// end() - Retrieve an iterator past the last initializer argument. 614 arg_iterator end() { return Args + NumArgs; } 615 /// end() - Retrieve an iterator past the last initializer argument. 616 arg_const_iterator end() const { return Args + NumArgs; } 617 618 /// getNumArgs - Determine the number of arguments used to 619 /// initialize the member or base. 620 unsigned getNumArgs() const { return NumArgs; } 621}; 622 623/// CXXConstructorDecl - Represents a C++ constructor within a 624/// class. For example: 625/// 626/// @code 627/// class X { 628/// public: 629/// explicit X(int); // represented by a CXXConstructorDecl. 630/// }; 631/// @endcode 632class CXXConstructorDecl : public CXXMethodDecl { 633 /// Explicit - Whether this constructor is explicit. 634 bool Explicit : 1; 635 636 /// ImplicitlyDefined - Whether this constructor was implicitly 637 /// defined by the compiler. When false, the constructor was defined 638 /// by the user. In C++03, this flag will have the same value as 639 /// Implicit. In C++0x, however, a constructor that is 640 /// explicitly defaulted (i.e., defined with " = default") will have 641 /// @c !Implicit && ImplicitlyDefined. 642 bool ImplicitlyDefined : 1; 643 644 /// FIXME: Add support for base and member initializers. 645 646 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 647 DeclarationName N, QualType T, 648 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 649 : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline), 650 Explicit(isExplicit), ImplicitlyDefined(false) { 651 setImplicit(isImplicitlyDeclared); 652 } 653 654public: 655 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 656 SourceLocation L, DeclarationName N, 657 QualType T, bool isExplicit, 658 bool isInline, bool isImplicitlyDeclared); 659 660 /// isExplicit - Whether this constructor was marked "explicit" or not. 661 bool isExplicit() const { return Explicit; } 662 663 /// isImplicitlyDefined - Whether this constructor was implicitly 664 /// defined. If false, then this constructor was defined by the 665 /// user. This operation can only be invoked if the constructor has 666 /// already been defined. 667 bool isImplicitlyDefined(ASTContext &C) const { 668 assert(isThisDeclarationADefinition() && 669 "Can only get the implicit-definition flag once the constructor has been defined"); 670 return ImplicitlyDefined; 671 } 672 673 /// setImplicitlyDefined - Set whether this constructor was 674 /// implicitly defined or not. 675 void setImplicitlyDefined(bool ID) { 676 assert(isThisDeclarationADefinition() && 677 "Can only set the implicit-definition flag once the constructor has been defined"); 678 ImplicitlyDefined = ID; 679 } 680 681 /// isDefaultConstructor - Whether this constructor is a default 682 /// constructor (C++ [class.ctor]p5), which can be used to 683 /// default-initialize a class of this type. 684 bool isDefaultConstructor() const; 685 686 /// isCopyConstructor - Whether this constructor is a copy 687 /// constructor (C++ [class.copy]p2, which can be used to copy the 688 /// class. @p TypeQuals will be set to the qualifiers on the 689 /// argument type. For example, @p TypeQuals would be set to @c 690 /// QualType::Const for the following copy constructor: 691 /// 692 /// @code 693 /// class X { 694 /// public: 695 /// X(const X&); 696 /// }; 697 /// @endcode 698 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 699 700 /// isCopyConstructor - Whether this constructor is a copy 701 /// constructor (C++ [class.copy]p2, which can be used to copy the 702 /// class. 703 bool isCopyConstructor(ASTContext &Context) const { 704 unsigned TypeQuals = 0; 705 return isCopyConstructor(Context, TypeQuals); 706 } 707 708 /// isConvertingConstructor - Whether this constructor is a 709 /// converting constructor (C++ [class.conv.ctor]), which can be 710 /// used for user-defined conversions. 711 bool isConvertingConstructor() const; 712 713 // Implement isa/cast/dyncast/etc. 714 static bool classof(const Decl *D) { 715 return D->getKind() == CXXConstructor; 716 } 717 static bool classof(const CXXConstructorDecl *D) { return true; } 718}; 719 720/// CXXDestructorDecl - Represents a C++ destructor within a 721/// class. For example: 722/// 723/// @code 724/// class X { 725/// public: 726/// ~X(); // represented by a CXXDestructorDecl. 727/// }; 728/// @endcode 729class CXXDestructorDecl : public CXXMethodDecl { 730 /// ImplicitlyDefined - Whether this destructor was implicitly 731 /// defined by the compiler. When false, the destructor was defined 732 /// by the user. In C++03, this flag will have the same value as 733 /// Implicit. In C++0x, however, a destructor that is 734 /// explicitly defaulted (i.e., defined with " = default") will have 735 /// @c !Implicit && ImplicitlyDefined. 736 bool ImplicitlyDefined : 1; 737 738 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 739 DeclarationName N, QualType T, 740 bool isInline, bool isImplicitlyDeclared) 741 : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline), 742 ImplicitlyDefined(false) { 743 setImplicit(isImplicitlyDeclared); 744 } 745 746public: 747 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 748 SourceLocation L, DeclarationName N, 749 QualType T, bool isInline, 750 bool isImplicitlyDeclared); 751 752 /// isImplicitlyDefined - Whether this destructor was implicitly 753 /// defined. If false, then this destructor was defined by the 754 /// user. This operation can only be invoked if the destructor has 755 /// already been defined. 756 bool isImplicitlyDefined() const { 757 assert(isThisDeclarationADefinition() && 758 "Can only get the implicit-definition flag once the destructor has been defined"); 759 return ImplicitlyDefined; 760 } 761 762 /// setImplicitlyDefined - Set whether this destructor was 763 /// implicitly defined or not. 764 void setImplicitlyDefined(bool ID) { 765 assert(isThisDeclarationADefinition() && 766 "Can only set the implicit-definition flag once the destructor has been defined"); 767 ImplicitlyDefined = ID; 768 } 769 770 // Implement isa/cast/dyncast/etc. 771 static bool classof(const Decl *D) { 772 return D->getKind() == CXXDestructor; 773 } 774 static bool classof(const CXXDestructorDecl *D) { return true; } 775}; 776 777/// CXXConversionDecl - Represents a C++ conversion function within a 778/// class. For example: 779/// 780/// @code 781/// class X { 782/// public: 783/// operator bool(); 784/// }; 785/// @endcode 786class CXXConversionDecl : public CXXMethodDecl { 787 /// Explicit - Whether this conversion function is marked 788 /// "explicit", meaning that it can only be applied when the user 789 /// explicitly wrote a cast. This is a C++0x feature. 790 bool Explicit : 1; 791 792 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 793 DeclarationName N, QualType T, 794 bool isInline, bool isExplicit) 795 : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline), 796 Explicit(isExplicit) { } 797 798public: 799 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 800 SourceLocation L, DeclarationName N, 801 QualType T, bool isInline, 802 bool isExplicit); 803 804 /// isExplicit - Whether this is an explicit conversion operator 805 /// (C++0x only). Explicit conversion operators are only considered 806 /// when the user has explicitly written a cast. 807 bool isExplicit() const { return Explicit; } 808 809 /// getConversionType - Returns the type that this conversion 810 /// function is converting to. 811 QualType getConversionType() const { 812 return getType()->getAsFunctionType()->getResultType(); 813 } 814 815 // Implement isa/cast/dyncast/etc. 816 static bool classof(const Decl *D) { 817 return D->getKind() == CXXConversion; 818 } 819 static bool classof(const CXXConversionDecl *D) { return true; } 820}; 821 822/// LinkageSpecDecl - This represents a linkage specification. For example: 823/// extern "C" void foo(); 824/// 825class LinkageSpecDecl : public Decl, public DeclContext { 826public: 827 /// LanguageIDs - Used to represent the language in a linkage 828 /// specification. The values are part of the serialization abi for 829 /// ASTs and cannot be changed without altering that abi. To help 830 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 831 /// from the dwarf standard. 832 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 833 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 834private: 835 /// Language - The language for this linkage specification. 836 LanguageIDs Language; 837 838 /// HadBraces - Whether this linkage specification had curly braces or not. 839 bool HadBraces : 1; 840 841 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 842 bool Braces) 843 : Decl(LinkageSpec, DC, L), 844 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 845 846public: 847 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 848 SourceLocation L, LanguageIDs Lang, 849 bool Braces); 850 851 LanguageIDs getLanguage() const { return Language; } 852 853 /// hasBraces - Determines whether this linkage specification had 854 /// braces in its syntactic form. 855 bool hasBraces() const { return HadBraces; } 856 857 static bool classof(const Decl *D) { 858 return D->getKind() == LinkageSpec; 859 } 860 static bool classof(const LinkageSpecDecl *D) { return true; } 861 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 862 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 863 } 864 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 865 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 866 } 867}; 868 869/// UsingDirectiveDecl - Represents C++ using-directive. For example: 870/// 871/// using namespace std; 872/// 873// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 874// artificial name, for all using-directives in order to store 875// them in DeclContext effectively. 876class UsingDirectiveDecl : public NamedDecl { 877 878 /// SourceLocation - Location of 'namespace' token. 879 SourceLocation NamespaceLoc; 880 881 /// \brief The source range that covers the nested-name-specifier 882 /// preceding the namespace name. 883 SourceRange QualifierRange; 884 885 /// \brief The nested-name-specifier that precedes the namespace 886 /// name, if any. 887 NestedNameSpecifier *Qualifier; 888 889 /// IdentLoc - Location of nominated namespace-name identifier. 890 // FIXME: We don't store location of scope specifier. 891 SourceLocation IdentLoc; 892 893 /// NominatedNamespace - Namespace nominated by using-directive. 894 NamespaceDecl *NominatedNamespace; 895 896 /// Enclosing context containing both using-directive and nomintated 897 /// namespace. 898 DeclContext *CommonAncestor; 899 900 /// getUsingDirectiveName - Returns special DeclarationName used by 901 /// using-directives. This is only used by DeclContext for storing 902 /// UsingDirectiveDecls in its lookup structure. 903 static DeclarationName getName() { 904 return DeclarationName::getUsingDirectiveName(); 905 } 906 907 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 908 SourceLocation NamespcLoc, 909 SourceRange QualifierRange, 910 NestedNameSpecifier *Qualifier, 911 SourceLocation IdentLoc, 912 NamespaceDecl *Nominated, 913 DeclContext *CommonAncestor) 914 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 915 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 916 Qualifier(Qualifier), IdentLoc(IdentLoc), 917 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 918 CommonAncestor(CommonAncestor) { 919 } 920 921public: 922 /// \brief Retrieve the source range of the nested-name-specifier 923 /// that qualifiers the namespace name. 924 SourceRange getQualifierRange() const { return QualifierRange; } 925 926 /// \brief Retrieve the nested-name-specifier that qualifies the 927 /// name of the namespace. 928 NestedNameSpecifier *getQualifier() const { return Qualifier; } 929 930 /// getNominatedNamespace - Returns namespace nominated by using-directive. 931 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 932 933 const NamespaceDecl *getNominatedNamespace() const { 934 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 935 } 936 937 /// getCommonAncestor - returns common ancestor context of using-directive, 938 /// and nominated by it namespace. 939 DeclContext *getCommonAncestor() { return CommonAncestor; } 940 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 941 942 /// getNamespaceKeyLocation - Returns location of namespace keyword. 943 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 944 945 /// getIdentLocation - Returns location of identifier. 946 SourceLocation getIdentLocation() const { return IdentLoc; } 947 948 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 949 SourceLocation L, 950 SourceLocation NamespaceLoc, 951 SourceRange QualifierRange, 952 NestedNameSpecifier *Qualifier, 953 SourceLocation IdentLoc, 954 NamespaceDecl *Nominated, 955 DeclContext *CommonAncestor); 956 957 static bool classof(const Decl *D) { 958 return D->getKind() == Decl::UsingDirective; 959 } 960 static bool classof(const UsingDirectiveDecl *D) { return true; } 961 962 // Friend for getUsingDirectiveName. 963 friend class DeclContext; 964}; 965 966/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 967/// 968/// @code 969/// namespace Foo = Bar; 970/// @endcode 971class NamespaceAliasDecl : public NamedDecl { 972 SourceLocation AliasLoc; 973 974 /// IdentLoc - Location of namespace identifier. 975 /// FIXME: We don't store location of scope specifier. 976 SourceLocation IdentLoc; 977 978 /// Namespace - The Decl that this alias points to. Can either be a 979 /// NamespaceDecl or a NamespaceAliasDecl. 980 NamedDecl *Namespace; 981 982 NamespaceAliasDecl(DeclContext *DC, SourceLocation L, 983 SourceLocation AliasLoc, IdentifierInfo *Alias, 984 SourceLocation IdentLoc, NamedDecl *Namespace) 985 : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), 986 IdentLoc(IdentLoc), Namespace(Namespace) { } 987 988public: 989 990 NamespaceDecl *getNamespace() { 991 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 992 return AD->getNamespace(); 993 994 return cast<NamespaceDecl>(Namespace); 995 } 996 997 const NamespaceDecl *getNamespace() const { 998 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 999 } 1000 1001 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1002 SourceLocation L, SourceLocation AliasLoc, 1003 IdentifierInfo *Alias, 1004 SourceLocation IdentLoc, 1005 NamedDecl *Namespace); 1006 1007 static bool classof(const Decl *D) { 1008 return D->getKind() == Decl::NamespaceAlias; 1009 } 1010 static bool classof(const NamespaceAliasDecl *D) { return true; } 1011}; 1012 1013/// StaticAssertDecl - Represents a C++0x static_assert declaration. 1014class StaticAssertDecl : public Decl { 1015 Expr *AssertExpr; 1016 StringLiteral *Message; 1017 1018 StaticAssertDecl(DeclContext *DC, SourceLocation L, 1019 Expr *assertexpr, StringLiteral *message) 1020 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 1021 1022public: 1023 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 1024 SourceLocation L, Expr *AssertExpr, 1025 StringLiteral *Message); 1026 1027 Expr *getAssertExpr() { return AssertExpr; } 1028 const Expr *getAssertExpr() const { return AssertExpr; } 1029 1030 StringLiteral *getMessage() { return Message; } 1031 const StringLiteral *getMessage() const { return Message; } 1032 1033 virtual ~StaticAssertDecl(); 1034 virtual void Destroy(ASTContext& C); 1035 1036 static bool classof(const Decl *D) { 1037 return D->getKind() == Decl::StaticAssert; 1038 } 1039 static bool classof(StaticAssertDecl *D) { return true; } 1040}; 1041 1042/// CXXTempVarDecl - Represents an implicit C++ temporary variable declaration. 1043class CXXTempVarDecl : public VarDecl { 1044protected: 1045 CXXTempVarDecl(DeclContext *DC, QualType T) 1046 : VarDecl(CXXTempVar, DC, SourceLocation(), 0, T, None) {} 1047 1048public: 1049 static CXXTempVarDecl *Create(ASTContext &C, DeclContext *DC, 1050 QualType T); 1051 1052 static bool classof(const Decl *D) { 1053 return D->getKind() == Decl::CXXTempVar; 1054 } 1055 static bool classof(CXXTempVarDecl *D) { return true; } 1056}; 1057 1058/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 1059/// into a diagnostic with <<. 1060const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1061 AccessSpecifier AS); 1062 1063} // end namespace clang 1064 1065#endif 1066