DeclCXX.h revision ff7fea809bab2badd0cb241703b14ac20ac258cb
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 /// Bases - Base classes of this class. 223 /// FIXME: This is wasted space for a union. 224 CXXBaseSpecifier *Bases; 225 226 /// NumBases - The number of base class specifiers in Bases. 227 unsigned NumBases; 228 229 /// Conversions - Overload set containing the conversion functions 230 /// of this C++ class (but not its inherited conversion 231 /// functions). Each of the entries in this overload set is a 232 /// CXXConversionDecl. 233 OverloadedFunctionDecl Conversions; 234 235protected: 236 CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 237 SourceLocation L, IdentifierInfo *Id); 238 239 ~CXXRecordDecl(); 240 241public: 242 /// base_class_iterator - Iterator that traverses the base classes 243 /// of a clas. 244 typedef CXXBaseSpecifier* base_class_iterator; 245 246 /// base_class_const_iterator - Iterator that traverses the base 247 /// classes of a clas. 248 typedef const CXXBaseSpecifier* base_class_const_iterator; 249 250 static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 251 SourceLocation L, IdentifierInfo *Id, 252 CXXRecordDecl* PrevDecl=0); 253 254 /// setBases - Sets the base classes of this struct or class. 255 void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases); 256 257 /// getNumBases - Retrieves the number of base classes of this 258 /// class. 259 unsigned getNumBases() const { return NumBases; } 260 261 base_class_iterator bases_begin() { return Bases; } 262 base_class_const_iterator bases_begin() const { return Bases; } 263 base_class_iterator bases_end() { return Bases + NumBases; } 264 base_class_const_iterator bases_end() const { return Bases + NumBases; } 265 266 /// hasConstCopyConstructor - Determines whether this class has a 267 /// copy constructor that accepts a const-qualified argument. 268 bool hasConstCopyConstructor(ASTContext &Context) const; 269 270 /// hasConstCopyAssignment - Determines whether this class has a 271 /// copy assignment operator that accepts a const-qualified argument. 272 bool hasConstCopyAssignment(ASTContext &Context) const; 273 274 /// addedConstructor - Notify the class that another constructor has 275 /// been added. This routine helps maintain information about the 276 /// class based on which constructors have been added. 277 void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl); 278 279 /// hasUserDeclaredConstructor - Whether this class has any 280 /// user-declared constructors. When true, a default constructor 281 /// will not be implicitly declared. 282 bool hasUserDeclaredConstructor() const { return UserDeclaredConstructor; } 283 284 /// hasUserDeclaredCopyConstructor - Whether this class has a 285 /// user-declared copy constructor. When false, a copy constructor 286 /// will be implicitly declared. 287 bool hasUserDeclaredCopyConstructor() const { 288 return UserDeclaredCopyConstructor; 289 } 290 291 /// addedAssignmentOperator - Notify the class that another assignment 292 /// operator has been added. This routine helps maintain information about the 293 /// class based on which operators have been added. 294 void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl); 295 296 /// hasUserDeclaredCopyAssignment - Whether this class has a 297 /// user-declared copy assignment operator. When false, a copy 298 /// assigment operator will be implicitly declared. 299 bool hasUserDeclaredCopyAssignment() const { 300 return UserDeclaredCopyAssignment; 301 } 302 303 /// hasUserDeclaredDestructor - Whether this class has a 304 /// user-declared destructor. When false, a destructor will be 305 /// implicitly declared. 306 bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; } 307 308 /// setUserDeclaredDestructor - Set whether this class has a 309 /// user-declared destructor. If not set by the time the class is 310 /// fully defined, a destructor will be implicitly declared. 311 void setUserDeclaredDestructor(bool UCD = true) { 312 UserDeclaredDestructor = UCD; 313 } 314 315 /// getConversions - Retrieve the overload set containing all of the 316 /// conversion functions in this class. 317 OverloadedFunctionDecl *getConversionFunctions() { 318 return &Conversions; 319 } 320 const OverloadedFunctionDecl *getConversionFunctions() const { 321 return &Conversions; 322 } 323 324 /// addConversionFunction - Add a new conversion function to the 325 /// list of conversion functions. 326 void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl); 327 328 /// isAggregate - Whether this class is an aggregate (C++ 329 /// [dcl.init.aggr]), which is a class with no user-declared 330 /// constructors, no private or protected non-static data members, 331 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 332 bool isAggregate() const { return Aggregate; } 333 334 /// setAggregate - Set whether this class is an aggregate (C++ 335 /// [dcl.init.aggr]). 336 void setAggregate(bool Agg) { Aggregate = Agg; } 337 338 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 339 /// that is an aggregate that has no non-static non-POD data members, no 340 /// reference data members, no user-defined copy assignment operator and no 341 /// user-defined destructor. 342 bool isPOD() const { return PlainOldData; } 343 344 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 345 void setPOD(bool POD) { PlainOldData = POD; } 346 347 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 348 /// which means that the class contains or inherits a virtual function. 349 bool isPolymorphic() const { return Polymorphic; } 350 351 /// setPolymorphic - Set whether this class is polymorphic (C++ 352 /// [class.virtual]). 353 void setPolymorphic(bool Poly) { Polymorphic = Poly; } 354 355 /// viewInheritance - Renders and displays an inheritance diagram 356 /// for this C++ class and all of its base classes (transitively) using 357 /// GraphViz. 358 void viewInheritance(ASTContext& Context) const; 359 360 static bool classof(const Decl *D) { 361 return D->getKind() == CXXRecord || 362 D->getKind() == ClassTemplateSpecialization; 363 } 364 static bool classof(const CXXRecordDecl *D) { return true; } 365 static bool classof(const ClassTemplateSpecializationDecl *D) { 366 return true; 367 } 368 369protected: 370 /// EmitImpl - Serialize this CXXRecordDecl. Called by Decl::Emit. 371 // FIXME: Implement this. 372 //virtual void EmitImpl(llvm::Serializer& S) const; 373 374 /// CreateImpl - Deserialize a CXXRecordDecl. Called by Decl::Create. 375 // FIXME: Implement this. 376 static CXXRecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D, ASTContext& C); 377 378 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 379}; 380 381/// CXXMethodDecl - Represents a static or instance method of a 382/// struct/union/class. 383class CXXMethodDecl : public FunctionDecl { 384protected: 385 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 386 DeclarationName N, QualType T, 387 bool isStatic, bool isInline) 388 : FunctionDecl(DK, RD, L, N, T, (isStatic ? Static : None), 389 isInline) {} 390 391public: 392 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 393 SourceLocation L, DeclarationName N, 394 QualType T, bool isStatic = false, 395 bool isInline = false); 396 397 bool isStatic() const { return getStorageClass() == Static; } 398 bool isInstance() const { return !isStatic(); } 399 400 bool isOutOfLineDefinition() const { 401 return getLexicalDeclContext() != getDeclContext(); 402 } 403 404 /// getParent - Returns the parent of this method declaration, which 405 /// is the class in which this method is defined. 406 const CXXRecordDecl *getParent() const { 407 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 408 } 409 410 /// getParent - Returns the parent of this method declaration, which 411 /// is the class in which this method is defined. 412 CXXRecordDecl *getParent() { 413 return const_cast<CXXRecordDecl *>( 414 cast<CXXRecordDecl>(FunctionDecl::getParent())); 415 } 416 417 /// getThisType - Returns the type of 'this' pointer. 418 /// Should only be called for instance methods. 419 QualType getThisType(ASTContext &C) const; 420 421 unsigned getTypeQualifiers() const { 422 return getType()->getAsFunctionProtoType()->getTypeQuals(); 423 } 424 425 // Implement isa/cast/dyncast/etc. 426 static bool classof(const Decl *D) { 427 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 428 } 429 static bool classof(const CXXMethodDecl *D) { return true; } 430 431protected: 432 /// EmitImpl - Serialize this CXXMethodDecl. Called by Decl::Emit. 433 // FIXME: Implement this. 434 //virtual void EmitImpl(llvm::Serializer& S) const; 435 436 /// CreateImpl - Deserialize a CXXMethodDecl. Called by Decl::Create. 437 // FIXME: Implement this. 438 static CXXMethodDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 439 440 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 441}; 442 443/// CXXBaseOrMemberInitializer - Represents a C++ base or member 444/// initializer, which is part of a constructor initializer that 445/// initializes one non-static member variable or one base class. For 446/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 447/// initializers: 448/// 449/// @code 450/// class A { }; 451/// class B : public A { 452/// float f; 453/// public: 454/// B(A& a) : A(a), f(3.14159) { } 455/// }; 456class CXXBaseOrMemberInitializer { 457 /// BaseOrMember - This points to the entity being initialized, 458 /// which is either a base class (a Type) or a non-static data 459 /// member. When the low bit is 1, it's a base 460 /// class; when the low bit is 0, it's a member. 461 uintptr_t BaseOrMember; 462 463 /// Args - The arguments used to initialize the base or member. 464 Expr **Args; 465 unsigned NumArgs; 466 467public: 468 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 469 explicit 470 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs); 471 472 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 473 explicit 474 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs); 475 476 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 477 ~CXXBaseOrMemberInitializer(); 478 479 /// arg_iterator - Iterates through the member initialization 480 /// arguments. 481 typedef Expr **arg_iterator; 482 483 /// arg_const_iterator - Iterates through the member initialization 484 /// arguments. 485 typedef Expr * const * arg_const_iterator; 486 487 /// isBaseInitializer - Returns true when this initializer is 488 /// initializing a base class. 489 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 490 491 /// isMemberInitializer - Returns true when this initializer is 492 /// initializing a non-static data member. 493 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 494 495 /// getBaseClass - If this is a base class initializer, returns the 496 /// type used to specify the initializer. The resulting type will be 497 /// a class type or a typedef of a class type. If this is not a base 498 /// class initializer, returns NULL. 499 Type *getBaseClass() { 500 if (isBaseInitializer()) 501 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 502 else 503 return 0; 504 } 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 const Type *getBaseClass() const { 511 if (isBaseInitializer()) 512 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 513 else 514 return 0; 515 } 516 517 /// getMember - If this is a member initializer, returns the 518 /// declaration of the non-static data member being 519 /// initialized. Otherwise, returns NULL. 520 FieldDecl *getMember() { 521 if (isMemberInitializer()) 522 return reinterpret_cast<FieldDecl *>(BaseOrMember); 523 else 524 return 0; 525 } 526 527 /// begin() - Retrieve an iterator to the first initializer argument. 528 arg_iterator begin() { return Args; } 529 /// begin() - Retrieve an iterator to the first initializer argument. 530 arg_const_iterator begin() const { return Args; } 531 532 /// end() - Retrieve an iterator past the last initializer argument. 533 arg_iterator end() { return Args + NumArgs; } 534 /// end() - Retrieve an iterator past the last initializer argument. 535 arg_const_iterator end() const { return Args + NumArgs; } 536 537 /// getNumArgs - Determine the number of arguments used to 538 /// initialize the member or base. 539 unsigned getNumArgs() const { return NumArgs; } 540}; 541 542/// CXXConstructorDecl - Represents a C++ constructor within a 543/// class. For example: 544/// 545/// @code 546/// class X { 547/// public: 548/// explicit X(int); // represented by a CXXConstructorDecl. 549/// }; 550/// @endcode 551class CXXConstructorDecl : public CXXMethodDecl { 552 /// Explicit - Whether this constructor is explicit. 553 bool Explicit : 1; 554 555 /// ImplicitlyDefined - Whether this constructor was implicitly 556 /// defined by the compiler. When false, the constructor was defined 557 /// by the user. In C++03, this flag will have the same value as 558 /// Implicit. In C++0x, however, a constructor that is 559 /// explicitly defaulted (i.e., defined with " = default") will have 560 /// @c !Implicit && ImplicitlyDefined. 561 bool ImplicitlyDefined : 1; 562 563 /// FIXME: Add support for base and member initializers. 564 565 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 566 DeclarationName N, QualType T, 567 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 568 : CXXMethodDecl(CXXConstructor, RD, L, N, T, false, isInline), 569 Explicit(isExplicit), ImplicitlyDefined(false) { 570 setImplicit(isImplicitlyDeclared); 571 } 572 573public: 574 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 575 SourceLocation L, DeclarationName N, 576 QualType T, bool isExplicit, 577 bool isInline, bool isImplicitlyDeclared); 578 579 /// isExplicit - Whether this constructor was marked "explicit" or not. 580 bool isExplicit() const { return Explicit; } 581 582 /// isImplicitlyDefined - Whether this constructor was implicitly 583 /// defined. If false, then this constructor was defined by the 584 /// user. This operation can only be invoked if the constructor has 585 /// already been defined. 586 bool isImplicitlyDefined() const { 587 assert(getBody() != 0 && 588 "Can only get the implicit-definition flag once the constructor has been defined"); 589 return ImplicitlyDefined; 590 } 591 592 /// setImplicitlyDefined - Set whether this constructor was 593 /// implicitly defined or not. 594 void setImplicitlyDefined(bool ID) { 595 assert(getBody() != 0 && 596 "Can only set the implicit-definition flag once the constructor has been defined"); 597 ImplicitlyDefined = ID; 598 } 599 600 /// isDefaultConstructor - Whether this constructor is a default 601 /// constructor (C++ [class.ctor]p5), which can be used to 602 /// default-initialize a class of this type. 603 bool isDefaultConstructor() const; 604 605 /// isCopyConstructor - Whether this constructor is a copy 606 /// constructor (C++ [class.copy]p2, which can be used to copy the 607 /// class. @p TypeQuals will be set to the qualifiers on the 608 /// argument type. For example, @p TypeQuals would be set to @c 609 /// QualType::Const for the following copy constructor: 610 /// 611 /// @code 612 /// class X { 613 /// public: 614 /// X(const X&); 615 /// }; 616 /// @endcode 617 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 618 619 /// isCopyConstructor - Whether this constructor is a copy 620 /// constructor (C++ [class.copy]p2, which can be used to copy the 621 /// class. 622 bool isCopyConstructor(ASTContext &Context) const { 623 unsigned TypeQuals = 0; 624 return isCopyConstructor(Context, TypeQuals); 625 } 626 627 /// isConvertingConstructor - Whether this constructor is a 628 /// converting constructor (C++ [class.conv.ctor]), which can be 629 /// used for user-defined conversions. 630 bool isConvertingConstructor() const; 631 632 // Implement isa/cast/dyncast/etc. 633 static bool classof(const Decl *D) { 634 return D->getKind() == CXXConstructor; 635 } 636 static bool classof(const CXXConstructorDecl *D) { return true; } 637 /// EmitImpl - Serialize this CXXConstructorDecl. Called by Decl::Emit. 638 // FIXME: Implement this. 639 //virtual void EmitImpl(llvm::Serializer& S) const; 640 641 /// CreateImpl - Deserialize a CXXConstructorDecl. Called by Decl::Create. 642 // FIXME: Implement this. 643 static CXXConstructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 644}; 645 646/// CXXDestructorDecl - Represents a C++ destructor within a 647/// class. For example: 648/// 649/// @code 650/// class X { 651/// public: 652/// ~X(); // represented by a CXXDestructorDecl. 653/// }; 654/// @endcode 655class CXXDestructorDecl : public CXXMethodDecl { 656 /// ImplicitlyDefined - Whether this destructor was implicitly 657 /// defined by the compiler. When false, the destructor was defined 658 /// by the user. In C++03, this flag will have the same value as 659 /// Implicit. In C++0x, however, a destructor that is 660 /// explicitly defaulted (i.e., defined with " = default") will have 661 /// @c !Implicit && ImplicitlyDefined. 662 bool ImplicitlyDefined : 1; 663 664 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 665 DeclarationName N, QualType T, 666 bool isInline, bool isImplicitlyDeclared) 667 : CXXMethodDecl(CXXDestructor, RD, L, N, T, false, isInline), 668 ImplicitlyDefined(false) { 669 setImplicit(isImplicitlyDeclared); 670 } 671 672public: 673 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 674 SourceLocation L, DeclarationName N, 675 QualType T, bool isInline, 676 bool isImplicitlyDeclared); 677 678 /// isImplicitlyDefined - Whether this destructor was implicitly 679 /// defined. If false, then this destructor was defined by the 680 /// user. This operation can only be invoked if the destructor has 681 /// already been defined. 682 bool isImplicitlyDefined() const { 683 assert(getBody() != 0 && 684 "Can only get the implicit-definition flag once the destructor has been defined"); 685 return ImplicitlyDefined; 686 } 687 688 /// setImplicitlyDefined - Set whether this destructor was 689 /// implicitly defined or not. 690 void setImplicitlyDefined(bool ID) { 691 assert(getBody() != 0 && 692 "Can only set the implicit-definition flag once the destructor has been defined"); 693 ImplicitlyDefined = ID; 694 } 695 696 // Implement isa/cast/dyncast/etc. 697 static bool classof(const Decl *D) { 698 return D->getKind() == CXXDestructor; 699 } 700 static bool classof(const CXXDestructorDecl *D) { return true; } 701 /// EmitImpl - Serialize this CXXDestructorDecl. Called by Decl::Emit. 702 // FIXME: Implement this. 703 //virtual void EmitImpl(llvm::Serializer& S) const; 704 705 /// CreateImpl - Deserialize a CXXDestructorDecl. Called by Decl::Create. 706 // FIXME: Implement this. 707 static CXXDestructorDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 708}; 709 710/// CXXConversionDecl - Represents a C++ conversion function within a 711/// class. For example: 712/// 713/// @code 714/// class X { 715/// public: 716/// operator bool(); 717/// }; 718/// @endcode 719class CXXConversionDecl : public CXXMethodDecl { 720 /// Explicit - Whether this conversion function is marked 721 /// "explicit", meaning that it can only be applied when the user 722 /// explicitly wrote a cast. This is a C++0x feature. 723 bool Explicit : 1; 724 725 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 726 DeclarationName N, QualType T, 727 bool isInline, bool isExplicit) 728 : CXXMethodDecl(CXXConversion, RD, L, N, T, false, isInline), 729 Explicit(isExplicit) { } 730 731public: 732 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 733 SourceLocation L, DeclarationName N, 734 QualType T, bool isInline, 735 bool isExplicit); 736 737 /// isExplicit - Whether this is an explicit conversion operator 738 /// (C++0x only). Explicit conversion operators are only considered 739 /// when the user has explicitly written a cast. 740 bool isExplicit() const { return Explicit; } 741 742 /// getConversionType - Returns the type that this conversion 743 /// function is converting to. 744 QualType getConversionType() const { 745 return getType()->getAsFunctionType()->getResultType(); 746 } 747 748 // Implement isa/cast/dyncast/etc. 749 static bool classof(const Decl *D) { 750 return D->getKind() == CXXConversion; 751 } 752 static bool classof(const CXXConversionDecl *D) { return true; } 753 /// EmitImpl - Serialize this CXXConversionDecl. Called by Decl::Emit. 754 // FIXME: Implement this. 755 //virtual void EmitImpl(llvm::Serializer& S) const; 756 757 /// CreateImpl - Deserialize a CXXConversionDecl. Called by Decl::Create. 758 // FIXME: Implement this. 759 static CXXConversionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 760}; 761 762/// LinkageSpecDecl - This represents a linkage specification. For example: 763/// extern "C" void foo(); 764/// 765class LinkageSpecDecl : public Decl, public DeclContext { 766public: 767 /// LanguageIDs - Used to represent the language in a linkage 768 /// specification. The values are part of the serialization abi for 769 /// ASTs and cannot be changed without altering that abi. To help 770 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 771 /// from the dwarf standard. 772 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 773 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 774private: 775 /// Language - The language for this linkage specification. 776 LanguageIDs Language; 777 778 /// HadBraces - Whether this linkage specification had curly braces or not. 779 bool HadBraces : 1; 780 781 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 782 bool Braces) 783 : Decl(LinkageSpec, DC, L), 784 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 785 786public: 787 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 788 SourceLocation L, LanguageIDs Lang, 789 bool Braces); 790 791 LanguageIDs getLanguage() const { return Language; } 792 793 /// hasBraces - Determines whether this linkage specification had 794 /// braces in its syntactic form. 795 bool hasBraces() const { return HadBraces; } 796 797 static bool classof(const Decl *D) { 798 return D->getKind() == LinkageSpec; 799 } 800 static bool classof(const LinkageSpecDecl *D) { return true; } 801 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 802 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 803 } 804 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 805 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 806 } 807 808protected: 809 void EmitInRec(llvm::Serializer& S) const; 810 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 811}; 812 813/// UsingDirectiveDecl - Represents C++ using-directive. For example: 814/// 815/// using namespace std; 816/// 817// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 818// artificial name, for all using-directives in order to store 819// them in DeclContext effectively. 820class UsingDirectiveDecl : public NamedDecl { 821 822 /// SourceLocation - Location of 'namespace' token. 823 SourceLocation NamespaceLoc; 824 825 /// IdentLoc - Location of nominated namespace-name identifier. 826 // FIXME: We don't store location of scope specifier. 827 SourceLocation IdentLoc; 828 829 /// NominatedNamespace - Namespace nominated by using-directive. 830 NamespaceDecl *NominatedNamespace; 831 832 /// Enclosing context containing both using-directive and nomintated 833 /// namespace. 834 DeclContext *CommonAncestor; 835 836 /// getUsingDirectiveName - Returns special DeclarationName used by 837 /// using-directives. This is only used by DeclContext for storing 838 /// UsingDirectiveDecls in its lookup structure. 839 static DeclarationName getName() { 840 return DeclarationName::getUsingDirectiveName(); 841 } 842 843 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 844 SourceLocation NamespcLoc, 845 SourceLocation IdentLoc, 846 NamespaceDecl *Nominated, 847 DeclContext *CommonAncestor) 848 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 849 NamespaceLoc(NamespcLoc), IdentLoc(IdentLoc), 850 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 851 CommonAncestor(CommonAncestor) { 852 } 853 854public: 855 /// getNominatedNamespace - Returns namespace nominated by using-directive. 856 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 857 858 const NamespaceDecl *getNominatedNamespace() const { 859 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 860 } 861 862 /// getCommonAncestor - returns common ancestor context of using-directive, 863 /// and nominated by it namespace. 864 DeclContext *getCommonAncestor() { return CommonAncestor; } 865 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 866 867 /// getNamespaceKeyLocation - Returns location of namespace keyword. 868 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 869 870 /// getIdentLocation - Returns location of identifier. 871 SourceLocation getIdentLocation() const { return IdentLoc; } 872 873 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 874 SourceLocation L, 875 SourceLocation NamespaceLoc, 876 SourceLocation IdentLoc, 877 NamespaceDecl *Nominated, 878 DeclContext *CommonAncestor); 879 880 static bool classof(const Decl *D) { 881 return D->getKind() == Decl::UsingDirective; 882 } 883 static bool classof(const UsingDirectiveDecl *D) { return true; } 884 885 // Friend for getUsingDirectiveName. 886 friend class DeclContext; 887}; 888 889} // end namespace clang 890 891#endif 892