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