DeclCXX.h revision 194711
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 /// getCopyConstructor - Returns the copy constructor for this class 287 CXXConstructorDecl *getCopyConstructor(ASTContext &Context, 288 unsigned TypeQuals) const; 289 290 /// hasConstCopyAssignment - Determines whether this class has a 291 /// copy assignment operator that accepts a const-qualified argument. 292 bool hasConstCopyAssignment(ASTContext &Context) const; 293 294 /// addedConstructor - Notify the class that another constructor has 295 /// been added. This routine helps maintain information about the 296 /// class based on which constructors have been added. 297 void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl); 298 299 /// hasUserDeclaredConstructor - Whether this class has any 300 /// user-declared constructors. When true, a default constructor 301 /// will not be implicitly declared. 302 bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; } 303 304 /// hasUserDeclaredCopyConstructor - Whether this class has a 305 /// user-declared copy constructor. When false, a copy constructor 306 /// will be implicitly declared. 307 bool hasUserDeclaredCopyConstructor() const { 308 return UserDeclaredCopyConstructor; 309 } 310 311 /// addedAssignmentOperator - Notify the class that another assignment 312 /// operator has been added. This routine helps maintain information about the 313 /// class based on which operators have been added. 314 void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl); 315 316 /// hasUserDeclaredCopyAssignment - Whether this class has a 317 /// user-declared copy assignment operator. When false, a copy 318 /// assigment operator will be implicitly declared. 319 bool hasUserDeclaredCopyAssignment() const { 320 return UserDeclaredCopyAssignment; 321 } 322 323 /// hasUserDeclaredDestructor - Whether this class has a 324 /// user-declared destructor. When false, a destructor will be 325 /// implicitly declared. 326 bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; } 327 328 /// setUserDeclaredDestructor - Set whether this class has a 329 /// user-declared destructor. If not set by the time the class is 330 /// fully defined, a destructor will be implicitly declared. 331 void setUserDeclaredDestructor(bool UCD) { 332 UserDeclaredDestructor = UCD; 333 } 334 335 /// getConversions - Retrieve the overload set containing all of the 336 /// conversion functions in this class. 337 OverloadedFunctionDecl *getConversionFunctions() { 338 return &Conversions; 339 } 340 const OverloadedFunctionDecl *getConversionFunctions() const { 341 return &Conversions; 342 } 343 344 /// addConversionFunction - Add a new conversion function to the 345 /// list of conversion functions. 346 void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl); 347 348 /// isAggregate - Whether this class is an aggregate (C++ 349 /// [dcl.init.aggr]), which is a class with no user-declared 350 /// constructors, no private or protected non-static data members, 351 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 352 bool isAggregate() const { return Aggregate; } 353 354 /// setAggregate - Set whether this class is an aggregate (C++ 355 /// [dcl.init.aggr]). 356 void setAggregate(bool Agg) { Aggregate = Agg; } 357 358 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 359 /// that is an aggregate that has no non-static non-POD data members, no 360 /// reference data members, no user-defined copy assignment operator and no 361 /// user-defined destructor. 362 bool isPOD() const { return PlainOldData; } 363 364 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 365 void setPOD(bool POD) { PlainOldData = POD; } 366 367 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 368 /// which means that the class contains or inherits a virtual function. 369 bool isPolymorphic() const { return Polymorphic; } 370 371 /// setPolymorphic - Set whether this class is polymorphic (C++ 372 /// [class.virtual]). 373 void setPolymorphic(bool Poly) { Polymorphic = Poly; } 374 375 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 376 /// which means that the class contains or inherits a pure virtual function. 377 bool isAbstract() const { return Abstract; } 378 379 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 380 void setAbstract(bool Abs) { Abstract = Abs; } 381 382 // hasTrivialConstructor - Whether this class has a trivial constructor 383 // (C++ [class.ctor]p5) 384 bool hasTrivialConstructor() const { return HasTrivialConstructor; } 385 386 // setHasTrivialConstructor - Set whether this class has a trivial constructor 387 // (C++ [class.ctor]p5) 388 void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; } 389 390 // hasTrivialDestructor - Whether this class has a trivial destructor 391 // (C++ [class.dtor]p3) 392 bool hasTrivialDestructor() const { return HasTrivialDestructor; } 393 394 // setHasTrivialDestructor - Set whether this class has a trivial destructor 395 // (C++ [class.dtor]p3) 396 void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; } 397 398 /// \brief If this record is an instantiation of a member class, 399 /// retrieves the member class from which it was instantiated. 400 /// 401 /// This routine will return non-NULL for (non-templated) member 402 /// classes of class templates. For example, given: 403 /// 404 /// \code 405 /// template<typename T> 406 /// struct X { 407 /// struct A { }; 408 /// }; 409 /// \endcode 410 /// 411 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 412 /// whose parent is the class template specialization X<int>. For 413 /// this declaration, getInstantiatedFromMemberClass() will return 414 /// the CXXRecordDecl X<T>::A. When a complete definition of 415 /// X<int>::A is required, it will be instantiated from the 416 /// declaration returned by getInstantiatedFromMemberClass(). 417 CXXRecordDecl *getInstantiatedFromMemberClass() const { 418 return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>(); 419 } 420 421 /// \brief Specify that this record is an instantiation of the 422 /// member class RD. 423 void setInstantiationOfMemberClass(CXXRecordDecl *RD) { 424 TemplateOrInstantiation = RD; 425 } 426 427 /// \brief Retrieves the class template that is described by this 428 /// class declaration. 429 /// 430 /// Every class template is represented as a ClassTemplateDecl and a 431 /// CXXRecordDecl. The former contains template properties (such as 432 /// the template parameter lists) while the latter contains the 433 /// actual description of the template's 434 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 435 /// CXXRecordDecl that from a ClassTemplateDecl, while 436 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 437 /// a CXXRecordDecl. 438 ClassTemplateDecl *getDescribedClassTemplate() const { 439 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 440 } 441 442 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 443 TemplateOrInstantiation = Template; 444 } 445 446 /// getDefaultConstructor - Returns the default constructor for this class 447 CXXConstructorDecl *getDefaultConstructor(ASTContext &Context); 448 449 /// getDestructor - Returns the destructor decl for this class. 450 const CXXDestructorDecl *getDestructor(ASTContext &Context); 451 452 /// viewInheritance - Renders and displays an inheritance diagram 453 /// for this C++ class and all of its base classes (transitively) using 454 /// GraphViz. 455 void viewInheritance(ASTContext& Context) const; 456 457 static bool classof(const Decl *D) { 458 return D->getKind() == CXXRecord || 459 D->getKind() == ClassTemplateSpecialization || 460 D->getKind() == ClassTemplatePartialSpecialization; 461 } 462 static bool classof(const CXXRecordDecl *D) { return true; } 463 static bool classof(const ClassTemplateSpecializationDecl *D) { 464 return true; 465 } 466}; 467 468/// CXXMethodDecl - Represents a static or instance method of a 469/// struct/union/class. 470class CXXMethodDecl : public FunctionDecl { 471protected: 472 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 473 DeclarationName N, QualType T, 474 bool isStatic, bool isInline) 475 : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None), 476 isInline) {} 477 478public: 479 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 480 SourceLocation L, DeclarationName N, 481 QualType T, bool isStatic = false, 482 bool isInline = false); 483 484 bool isStatic() const { return getStorageClass() == Static; } 485 bool isInstance() const { return !isStatic(); } 486 487 bool isVirtual() const { 488 return isVirtualAsWritten() || 489 (begin_overridden_methods() != end_overridden_methods()); 490 } 491 492 /// 493 void addOverriddenMethod(const CXXMethodDecl *MD); 494 495 typedef const CXXMethodDecl ** method_iterator; 496 497 method_iterator begin_overridden_methods() const; 498 method_iterator end_overridden_methods() const; 499 500 /// getParent - Returns the parent of this method declaration, which 501 /// is the class in which this method is defined. 502 const CXXRecordDecl *getParent() const { 503 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 504 } 505 506 /// getParent - Returns the parent of this method declaration, which 507 /// is the class in which this method is defined. 508 CXXRecordDecl *getParent() { 509 return const_cast<CXXRecordDecl *>( 510 cast<CXXRecordDecl>(FunctionDecl::getParent())); 511 } 512 513 /// getThisType - Returns the type of 'this' pointer. 514 /// Should only be called for instance methods. 515 QualType getThisType(ASTContext &C) const; 516 517 unsigned getTypeQualifiers() const { 518 return getType()->getAsFunctionProtoType()->getTypeQuals(); 519 } 520 521 // Implement isa/cast/dyncast/etc. 522 static bool classof(const Decl *D) { 523 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 524 } 525 static bool classof(const CXXMethodDecl *D) { return true; } 526}; 527 528/// CXXBaseOrMemberInitializer - Represents a C++ base or member 529/// initializer, which is part of a constructor initializer that 530/// initializes one non-static member variable or one base class. For 531/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 532/// initializers: 533/// 534/// @code 535/// class A { }; 536/// class B : public A { 537/// float f; 538/// public: 539/// B(A& a) : A(a), f(3.14159) { } 540/// }; 541/// @endcode 542class CXXBaseOrMemberInitializer { 543 /// BaseOrMember - This points to the entity being initialized, 544 /// which is either a base class (a Type) or a non-static data 545 /// member. When the low bit is 1, it's a base 546 /// class; when the low bit is 0, it's a member. 547 uintptr_t BaseOrMember; 548 549 /// Args - The arguments used to initialize the base or member. 550 Expr **Args; 551 unsigned NumArgs; 552 553public: 554 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 555 explicit 556 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs); 557 558 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 559 explicit 560 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs); 561 562 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 563 ~CXXBaseOrMemberInitializer(); 564 565 /// arg_iterator - Iterates through the member initialization 566 /// arguments. 567 typedef Expr **arg_iterator; 568 569 /// arg_const_iterator - Iterates through the member initialization 570 /// arguments. 571 typedef Expr * const * arg_const_iterator; 572 573 /// isBaseInitializer - Returns true when this initializer is 574 /// initializing a base class. 575 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 576 577 /// isMemberInitializer - Returns true when this initializer is 578 /// initializing a non-static data member. 579 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 580 581 /// getBaseClass - If this is a base class initializer, returns the 582 /// type used to specify the initializer. The resulting type will be 583 /// a class type or a typedef of a class type. If this is not a base 584 /// class initializer, returns NULL. 585 Type *getBaseClass() { 586 if (isBaseInitializer()) 587 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 588 else 589 return 0; 590 } 591 592 /// getBaseClass - If this is a base class initializer, returns the 593 /// type used to specify the initializer. The resulting type will be 594 /// a class type or a typedef of a class type. If this is not a base 595 /// class initializer, returns NULL. 596 const Type *getBaseClass() const { 597 if (isBaseInitializer()) 598 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 599 else 600 return 0; 601 } 602 603 /// getMember - If this is a member initializer, returns the 604 /// declaration of the non-static data member being 605 /// initialized. Otherwise, returns NULL. 606 FieldDecl *getMember() { 607 if (isMemberInitializer()) 608 return reinterpret_cast<FieldDecl *>(BaseOrMember); 609 else 610 return 0; 611 } 612 613 /// begin() - Retrieve an iterator to the first initializer argument. 614 arg_iterator begin() { return Args; } 615 /// begin() - Retrieve an iterator to the first initializer argument. 616 arg_const_iterator begin() const { return Args; } 617 618 /// end() - Retrieve an iterator past the last initializer argument. 619 arg_iterator end() { return Args + NumArgs; } 620 /// end() - Retrieve an iterator past the last initializer argument. 621 arg_const_iterator end() const { return Args + NumArgs; } 622 623 /// getNumArgs - Determine the number of arguments used to 624 /// initialize the member or base. 625 unsigned getNumArgs() const { return NumArgs; } 626}; 627 628/// CXXConstructorDecl - Represents a C++ constructor within a 629/// class. For example: 630/// 631/// @code 632/// class X { 633/// public: 634/// explicit X(int); // represented by a CXXConstructorDecl. 635/// }; 636/// @endcode 637class CXXConstructorDecl : public CXXMethodDecl { 638 /// Explicit - Whether this constructor is explicit. 639 bool Explicit : 1; 640 641 /// ImplicitlyDefined - Whether this constructor was implicitly 642 /// defined by the compiler. When false, the constructor was defined 643 /// by the user. In C++03, this flag will have the same value as 644 /// Implicit. In C++0x, however, a constructor that is 645 /// explicitly defaulted (i.e., defined with " = default") will have 646 /// @c !Implicit && ImplicitlyDefined. 647 bool ImplicitlyDefined : 1; 648 649 /// FIXME: Add support for base and member initializers. 650 651 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 652 DeclarationName N, QualType T, 653 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 654 : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline), 655 Explicit(isExplicit), ImplicitlyDefined(false) { 656 setImplicit(isImplicitlyDeclared); 657 } 658 659public: 660 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 661 SourceLocation L, DeclarationName N, 662 QualType T, bool isExplicit, 663 bool isInline, bool isImplicitlyDeclared); 664 665 /// isExplicit - Whether this constructor was marked "explicit" or not. 666 bool isExplicit() const { return Explicit; } 667 668 /// isImplicitlyDefined - Whether this constructor was implicitly 669 /// defined. If false, then this constructor was defined by the 670 /// user. This operation can only be invoked if the constructor has 671 /// already been defined. 672 bool isImplicitlyDefined(ASTContext &C) const { 673 assert(isThisDeclarationADefinition() && 674 "Can only get the implicit-definition flag once the constructor has been defined"); 675 return ImplicitlyDefined; 676 } 677 678 /// setImplicitlyDefined - Set whether this constructor was 679 /// implicitly defined or not. 680 void setImplicitlyDefined(bool ID) { 681 assert(isThisDeclarationADefinition() && 682 "Can only set the implicit-definition flag once the constructor has been defined"); 683 ImplicitlyDefined = ID; 684 } 685 686 /// isDefaultConstructor - Whether this constructor is a default 687 /// constructor (C++ [class.ctor]p5), which can be used to 688 /// default-initialize a class of this type. 689 bool isDefaultConstructor() const; 690 691 /// isCopyConstructor - Whether this constructor is a copy 692 /// constructor (C++ [class.copy]p2, which can be used to copy the 693 /// class. @p TypeQuals will be set to the qualifiers on the 694 /// argument type. For example, @p TypeQuals would be set to @c 695 /// QualType::Const for the following copy constructor: 696 /// 697 /// @code 698 /// class X { 699 /// public: 700 /// X(const X&); 701 /// }; 702 /// @endcode 703 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 704 705 /// isCopyConstructor - Whether this constructor is a copy 706 /// constructor (C++ [class.copy]p2, which can be used to copy the 707 /// class. 708 bool isCopyConstructor(ASTContext &Context) const { 709 unsigned TypeQuals = 0; 710 return isCopyConstructor(Context, TypeQuals); 711 } 712 713 /// isConvertingConstructor - Whether this constructor is a 714 /// converting constructor (C++ [class.conv.ctor]), which can be 715 /// used for user-defined conversions. 716 bool isConvertingConstructor() const; 717 718 // Implement isa/cast/dyncast/etc. 719 static bool classof(const Decl *D) { 720 return D->getKind() == CXXConstructor; 721 } 722 static bool classof(const CXXConstructorDecl *D) { return true; } 723}; 724 725/// CXXDestructorDecl - Represents a C++ destructor within a 726/// class. For example: 727/// 728/// @code 729/// class X { 730/// public: 731/// ~X(); // represented by a CXXDestructorDecl. 732/// }; 733/// @endcode 734class CXXDestructorDecl : public CXXMethodDecl { 735 /// ImplicitlyDefined - Whether this destructor was implicitly 736 /// defined by the compiler. When false, the destructor was defined 737 /// by the user. In C++03, this flag will have the same value as 738 /// Implicit. In C++0x, however, a destructor that is 739 /// explicitly defaulted (i.e., defined with " = default") will have 740 /// @c !Implicit && ImplicitlyDefined. 741 bool ImplicitlyDefined : 1; 742 743 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 744 DeclarationName N, QualType T, 745 bool isInline, bool isImplicitlyDeclared) 746 : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline), 747 ImplicitlyDefined(false) { 748 setImplicit(isImplicitlyDeclared); 749 } 750 751public: 752 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 753 SourceLocation L, DeclarationName N, 754 QualType T, bool isInline, 755 bool isImplicitlyDeclared); 756 757 /// isImplicitlyDefined - Whether this destructor was implicitly 758 /// defined. If false, then this destructor was defined by the 759 /// user. This operation can only be invoked if the destructor has 760 /// already been defined. 761 bool isImplicitlyDefined() const { 762 assert(isThisDeclarationADefinition() && 763 "Can only get the implicit-definition flag once the destructor has been defined"); 764 return ImplicitlyDefined; 765 } 766 767 /// setImplicitlyDefined - Set whether this destructor was 768 /// implicitly defined or not. 769 void setImplicitlyDefined(bool ID) { 770 assert(isThisDeclarationADefinition() && 771 "Can only set the implicit-definition flag once the destructor has been defined"); 772 ImplicitlyDefined = ID; 773 } 774 775 // Implement isa/cast/dyncast/etc. 776 static bool classof(const Decl *D) { 777 return D->getKind() == CXXDestructor; 778 } 779 static bool classof(const CXXDestructorDecl *D) { return true; } 780}; 781 782/// CXXConversionDecl - Represents a C++ conversion function within a 783/// class. For example: 784/// 785/// @code 786/// class X { 787/// public: 788/// operator bool(); 789/// }; 790/// @endcode 791class CXXConversionDecl : public CXXMethodDecl { 792 /// Explicit - Whether this conversion function is marked 793 /// "explicit", meaning that it can only be applied when the user 794 /// explicitly wrote a cast. This is a C++0x feature. 795 bool Explicit : 1; 796 797 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 798 DeclarationName N, QualType T, 799 bool isInline, bool isExplicit) 800 : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline), 801 Explicit(isExplicit) { } 802 803public: 804 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 805 SourceLocation L, DeclarationName N, 806 QualType T, bool isInline, 807 bool isExplicit); 808 809 /// isExplicit - Whether this is an explicit conversion operator 810 /// (C++0x only). Explicit conversion operators are only considered 811 /// when the user has explicitly written a cast. 812 bool isExplicit() const { return Explicit; } 813 814 /// getConversionType - Returns the type that this conversion 815 /// function is converting to. 816 QualType getConversionType() const { 817 return getType()->getAsFunctionType()->getResultType(); 818 } 819 820 // Implement isa/cast/dyncast/etc. 821 static bool classof(const Decl *D) { 822 return D->getKind() == CXXConversion; 823 } 824 static bool classof(const CXXConversionDecl *D) { return true; } 825}; 826 827/// LinkageSpecDecl - This represents a linkage specification. For example: 828/// extern "C" void foo(); 829/// 830class LinkageSpecDecl : public Decl, public DeclContext { 831public: 832 /// LanguageIDs - Used to represent the language in a linkage 833 /// specification. The values are part of the serialization abi for 834 /// ASTs and cannot be changed without altering that abi. To help 835 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 836 /// from the dwarf standard. 837 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 838 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 839private: 840 /// Language - The language for this linkage specification. 841 LanguageIDs Language; 842 843 /// HadBraces - Whether this linkage specification had curly braces or not. 844 bool HadBraces : 1; 845 846 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 847 bool Braces) 848 : Decl(LinkageSpec, DC, L), 849 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 850 851public: 852 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 853 SourceLocation L, LanguageIDs Lang, 854 bool Braces); 855 856 LanguageIDs getLanguage() const { return Language; } 857 858 /// hasBraces - Determines whether this linkage specification had 859 /// braces in its syntactic form. 860 bool hasBraces() const { return HadBraces; } 861 862 static bool classof(const Decl *D) { 863 return D->getKind() == LinkageSpec; 864 } 865 static bool classof(const LinkageSpecDecl *D) { return true; } 866 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 867 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 868 } 869 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 870 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 871 } 872}; 873 874/// UsingDirectiveDecl - Represents C++ using-directive. For example: 875/// 876/// using namespace std; 877/// 878// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 879// artificial name, for all using-directives in order to store 880// them in DeclContext effectively. 881class UsingDirectiveDecl : public NamedDecl { 882 883 /// SourceLocation - Location of 'namespace' token. 884 SourceLocation NamespaceLoc; 885 886 /// \brief The source range that covers the nested-name-specifier 887 /// preceding the namespace name. 888 SourceRange QualifierRange; 889 890 /// \brief The nested-name-specifier that precedes the namespace 891 /// name, if any. 892 NestedNameSpecifier *Qualifier; 893 894 /// IdentLoc - Location of nominated namespace-name identifier. 895 // FIXME: We don't store location of scope specifier. 896 SourceLocation IdentLoc; 897 898 /// NominatedNamespace - Namespace nominated by using-directive. 899 NamespaceDecl *NominatedNamespace; 900 901 /// Enclosing context containing both using-directive and nomintated 902 /// namespace. 903 DeclContext *CommonAncestor; 904 905 /// getUsingDirectiveName - Returns special DeclarationName used by 906 /// using-directives. This is only used by DeclContext for storing 907 /// UsingDirectiveDecls in its lookup structure. 908 static DeclarationName getName() { 909 return DeclarationName::getUsingDirectiveName(); 910 } 911 912 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 913 SourceLocation NamespcLoc, 914 SourceRange QualifierRange, 915 NestedNameSpecifier *Qualifier, 916 SourceLocation IdentLoc, 917 NamespaceDecl *Nominated, 918 DeclContext *CommonAncestor) 919 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 920 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 921 Qualifier(Qualifier), IdentLoc(IdentLoc), 922 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 923 CommonAncestor(CommonAncestor) { 924 } 925 926public: 927 /// \brief Retrieve the source range of the nested-name-specifier 928 /// that qualifiers the namespace name. 929 SourceRange getQualifierRange() const { return QualifierRange; } 930 931 /// \brief Retrieve the nested-name-specifier that qualifies the 932 /// name of the namespace. 933 NestedNameSpecifier *getQualifier() const { return Qualifier; } 934 935 /// getNominatedNamespace - Returns namespace nominated by using-directive. 936 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 937 938 const NamespaceDecl *getNominatedNamespace() const { 939 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 940 } 941 942 /// getCommonAncestor - returns common ancestor context of using-directive, 943 /// and nominated by it namespace. 944 DeclContext *getCommonAncestor() { return CommonAncestor; } 945 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 946 947 /// getNamespaceKeyLocation - Returns location of namespace keyword. 948 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 949 950 /// getIdentLocation - Returns location of identifier. 951 SourceLocation getIdentLocation() const { return IdentLoc; } 952 953 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 954 SourceLocation L, 955 SourceLocation NamespaceLoc, 956 SourceRange QualifierRange, 957 NestedNameSpecifier *Qualifier, 958 SourceLocation IdentLoc, 959 NamespaceDecl *Nominated, 960 DeclContext *CommonAncestor); 961 962 static bool classof(const Decl *D) { 963 return D->getKind() == Decl::UsingDirective; 964 } 965 static bool classof(const UsingDirectiveDecl *D) { return true; } 966 967 // Friend for getUsingDirectiveName. 968 friend class DeclContext; 969}; 970 971/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 972/// 973/// @code 974/// namespace Foo = Bar; 975/// @endcode 976class NamespaceAliasDecl : public NamedDecl { 977 SourceLocation AliasLoc; 978 979 /// \brief The source range that covers the nested-name-specifier 980 /// preceding the namespace name. 981 SourceRange QualifierRange; 982 983 /// \brief The nested-name-specifier that precedes the namespace 984 /// name, if any. 985 NestedNameSpecifier *Qualifier; 986 987 /// IdentLoc - Location of namespace identifier. 988 SourceLocation IdentLoc; 989 990 /// Namespace - The Decl that this alias points to. Can either be a 991 /// NamespaceDecl or a NamespaceAliasDecl. 992 NamedDecl *Namespace; 993 994 NamespaceAliasDecl(DeclContext *DC, SourceLocation L, 995 SourceLocation AliasLoc, IdentifierInfo *Alias, 996 SourceRange QualifierRange, 997 NestedNameSpecifier *Qualifier, 998 SourceLocation IdentLoc, NamedDecl *Namespace) 999 : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), 1000 QualifierRange(QualifierRange), Qualifier(Qualifier), 1001 IdentLoc(IdentLoc), Namespace(Namespace) { } 1002 1003public: 1004 /// \brief Retrieve the source range of the nested-name-specifier 1005 /// that qualifiers the namespace name. 1006 SourceRange getQualifierRange() const { return QualifierRange; } 1007 1008 /// \brief Retrieve the nested-name-specifier that qualifies the 1009 /// name of the namespace. 1010 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1011 1012 NamespaceDecl *getNamespace() { 1013 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1014 return AD->getNamespace(); 1015 1016 return cast<NamespaceDecl>(Namespace); 1017 } 1018 1019 const NamespaceDecl *getNamespace() const { 1020 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1021 } 1022 1023 /// \brief Retrieve the namespace that this alias refers to, which 1024 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1025 NamedDecl *getAliasedNamespace() const { return Namespace; } 1026 1027 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1028 SourceLocation L, SourceLocation AliasLoc, 1029 IdentifierInfo *Alias, 1030 SourceRange QualifierRange, 1031 NestedNameSpecifier *Qualifier, 1032 SourceLocation IdentLoc, 1033 NamedDecl *Namespace); 1034 1035 static bool classof(const Decl *D) { 1036 return D->getKind() == Decl::NamespaceAlias; 1037 } 1038 static bool classof(const NamespaceAliasDecl *D) { return true; } 1039}; 1040 1041/// UsingDecl - Represents a C++ using-declaration. For example: 1042/// using someNameSpace::someIdentifier; 1043class UsingDecl : public NamedDecl { 1044 1045 /// \brief The source range that covers the nested-name-specifier 1046 /// preceding the declaration name. 1047 SourceRange NestedNameRange; 1048 /// \brief The source location of the target declaration name. 1049 SourceLocation TargetNameLocation; 1050 /// \brief The source location of the "using" location itself. 1051 SourceLocation UsingLocation; 1052 /// \brief Target declaration. 1053 NamedDecl* TargetDecl; 1054 /// \brief Target declaration. 1055 NestedNameSpecifier* TargetNestedNameDecl; 1056 1057 // Had 'typename' keyword. 1058 bool IsTypeName; 1059 1060 UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR, 1061 SourceLocation TargetNL, SourceLocation UL, NamedDecl* Target, 1062 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) 1063 : NamedDecl(Decl::Using, DC, L, Target->getDeclName()), 1064 NestedNameRange(NNR), TargetNameLocation(TargetNL), 1065 UsingLocation(UL), TargetDecl(Target), 1066 TargetNestedNameDecl(TargetNNS), IsTypeName(IsTypeNameArg) { 1067 this->IdentifierNamespace = TargetDecl->getIdentifierNamespace(); 1068 } 1069 1070public: 1071 /// \brief Returns the source range that covers the nested-name-specifier 1072 /// preceding the namespace name. 1073 SourceRange getNestedNameRange() { return(NestedNameRange); } 1074 /// \brief Returns the source location of the target declaration name. 1075 SourceLocation getTargetNameLocation() { return(TargetNameLocation); } 1076 /// \brief Returns the source location of the "using" location itself. 1077 SourceLocation getUsingLocation() { return(UsingLocation); } 1078 /// \brief getTargetDecl - Returns target specified by using-decl. 1079 NamedDecl *getTargetDecl() { return(TargetDecl); } 1080 /// \brief Get target nested name declaration. 1081 NestedNameSpecifier* getTargetNestedNameDecl() { return(TargetNestedNameDecl); } 1082 /// isTypeName - Return true if using decl had 'typename'. 1083 bool isTypeName() const { return(IsTypeName); } 1084 1085 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 1086 SourceLocation L, SourceRange NNR, SourceLocation TargetNL, 1087 SourceLocation UL, NamedDecl* Target, 1088 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg); 1089 1090 static bool classof(const Decl *D) { 1091 return D->getKind() == Decl::Using; 1092 } 1093 static bool classof(const UsingDecl *D) { return true; } 1094}; 1095 1096/// StaticAssertDecl - Represents a C++0x static_assert declaration. 1097class StaticAssertDecl : public Decl { 1098 Expr *AssertExpr; 1099 StringLiteral *Message; 1100 1101 StaticAssertDecl(DeclContext *DC, SourceLocation L, 1102 Expr *assertexpr, StringLiteral *message) 1103 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 1104 1105public: 1106 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 1107 SourceLocation L, Expr *AssertExpr, 1108 StringLiteral *Message); 1109 1110 Expr *getAssertExpr() { return AssertExpr; } 1111 const Expr *getAssertExpr() const { return AssertExpr; } 1112 1113 StringLiteral *getMessage() { return Message; } 1114 const StringLiteral *getMessage() const { return Message; } 1115 1116 virtual ~StaticAssertDecl(); 1117 virtual void Destroy(ASTContext& C); 1118 1119 static bool classof(const Decl *D) { 1120 return D->getKind() == Decl::StaticAssert; 1121 } 1122 static bool classof(StaticAssertDecl *D) { return true; } 1123}; 1124 1125/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 1126/// into a diagnostic with <<. 1127const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1128 AccessSpecifier AS); 1129 1130} // end namespace clang 1131 1132#endif 1133