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