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