DeclCXX.h revision 8a5ae2401645788144c0ae769a2fb899866801f5
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/Expr.h" 18#include "clang/AST/Decl.h" 19#include "llvm/ADT/SmallVector.h" 20 21namespace clang { 22 23class ClassTemplateDecl; 24class CXXRecordDecl; 25class CXXConstructorDecl; 26class CXXDestructorDecl; 27class CXXConversionDecl; 28class CXXMethodDecl; 29class ClassTemplateSpecializationDecl; 30 31/// \brief Represents any kind of function declaration, whether it is a 32/// concrete function or a function template. 33class AnyFunctionDecl { 34 NamedDecl *Function; 35 36 AnyFunctionDecl(NamedDecl *ND) : Function(ND) { } 37 38public: 39 AnyFunctionDecl(FunctionDecl *FD) : Function(FD) { } 40 AnyFunctionDecl(FunctionTemplateDecl *FTD); 41 42 /// \brief Implicily converts any function or function template into a 43 /// named declaration. 44 operator NamedDecl *() const { return Function; } 45 46 /// \brief Retrieve the underlying function or function template. 47 NamedDecl *get() const { return Function; } 48 49 static AnyFunctionDecl getFromNamedDecl(NamedDecl *ND) { 50 return AnyFunctionDecl(ND); 51 } 52}; 53 54} // end namespace clang 55 56namespace llvm { 57 /// Implement simplify_type for AnyFunctionDecl, so that we can dyn_cast from 58 /// AnyFunctionDecl to any function or function template declaration. 59 template<> struct simplify_type<const ::clang::AnyFunctionDecl> { 60 typedef ::clang::NamedDecl* SimpleType; 61 static SimpleType getSimplifiedValue(const ::clang::AnyFunctionDecl &Val) { 62 return Val; 63 } 64 }; 65 template<> struct simplify_type< ::clang::AnyFunctionDecl> 66 : public simplify_type<const ::clang::AnyFunctionDecl> {}; 67 68 // Provide PointerLikeTypeTraits for non-cvr pointers. 69 template<> 70 class PointerLikeTypeTraits< ::clang::AnyFunctionDecl> { 71 public: 72 static inline void *getAsVoidPointer(::clang::AnyFunctionDecl F) { 73 return F.get(); 74 } 75 static inline ::clang::AnyFunctionDecl getFromVoidPointer(void *P) { 76 return ::clang::AnyFunctionDecl::getFromNamedDecl( 77 static_cast< ::clang::NamedDecl*>(P)); 78 } 79 80 enum { NumLowBitsAvailable = 2 }; 81 }; 82 83} // end namespace llvm 84 85namespace clang { 86 87/// OverloadedFunctionDecl - An instance of this class represents a 88/// set of overloaded functions. All of the functions have the same 89/// name and occur within the same scope. 90/// 91/// An OverloadedFunctionDecl has no ownership over the FunctionDecl 92/// nodes it contains. Rather, the FunctionDecls are owned by the 93/// enclosing scope (which also owns the OverloadedFunctionDecl 94/// node). OverloadedFunctionDecl is used primarily to store a set of 95/// overloaded functions for name lookup. 96class OverloadedFunctionDecl : public NamedDecl { 97protected: 98 OverloadedFunctionDecl(DeclContext *DC, DeclarationName N) 99 : NamedDecl(OverloadedFunction, DC, SourceLocation(), N) { } 100 101 /// Functions - the set of overloaded functions contained in this 102 /// overload set. 103 llvm::SmallVector<AnyFunctionDecl, 4> Functions; 104 105 // FIXME: This should go away when we stop using 106 // OverloadedFunctionDecl to store conversions in CXXRecordDecl. 107 friend class CXXRecordDecl; 108 109public: 110 typedef llvm::SmallVector<AnyFunctionDecl, 4>::iterator function_iterator; 111 typedef llvm::SmallVector<AnyFunctionDecl, 4>::const_iterator 112 function_const_iterator; 113 114 static OverloadedFunctionDecl *Create(ASTContext &C, DeclContext *DC, 115 DeclarationName N); 116 117 /// \brief Add a new overloaded function or function template to the set 118 /// of overloaded function templates. 119 void addOverload(AnyFunctionDecl F); 120 121 function_iterator function_begin() { return Functions.begin(); } 122 function_iterator function_end() { return Functions.end(); } 123 function_const_iterator function_begin() const { return Functions.begin(); } 124 function_const_iterator function_end() const { return Functions.end(); } 125 126 /// \brief Returns the number of overloaded functions stored in 127 /// this set. 128 unsigned size() const { return Functions.size(); } 129 130 // Implement isa/cast/dyncast/etc. 131 static bool classof(const Decl *D) { 132 return D->getKind() == OverloadedFunction; 133 } 134 static bool classof(const OverloadedFunctionDecl *D) { return true; } 135}; 136 137/// \brief Provides uniform iteration syntax for an overload set, function, 138/// or function template. 139class OverloadIterator { 140 /// \brief An overloaded function set, function declaration, or 141 /// function template declaration. 142 NamedDecl *D; 143 144 /// \brief If the declaration is an overloaded function set, this is the 145 /// iterator pointing to the current position within that overloaded 146 /// function set. 147 OverloadedFunctionDecl::function_iterator Iter; 148 149public: 150 typedef AnyFunctionDecl value_type; 151 typedef value_type reference; 152 typedef NamedDecl *pointer; 153 typedef int difference_type; 154 typedef std::forward_iterator_tag iterator_category; 155 156 OverloadIterator() : D(0) { } 157 158 OverloadIterator(FunctionDecl *FD) : D(FD) { } 159 OverloadIterator(FunctionTemplateDecl *FTD) 160 : D(reinterpret_cast<NamedDecl*>(FTD)) { } 161 OverloadIterator(OverloadedFunctionDecl *Ovl) 162 : D(Ovl), Iter(Ovl->function_begin()) { } 163 164 OverloadIterator(NamedDecl *ND); 165 166 reference operator*() const; 167 168 pointer operator->() const { return (**this).get(); } 169 170 OverloadIterator &operator++(); 171 172 OverloadIterator operator++(int) { 173 OverloadIterator Temp(*this); 174 ++(*this); 175 return Temp; 176 } 177 178 bool Equals(const OverloadIterator &Other) const; 179}; 180 181inline bool operator==(const OverloadIterator &X, const OverloadIterator &Y) { 182 return X.Equals(Y); 183} 184 185inline bool operator!=(const OverloadIterator &X, const OverloadIterator &Y) { 186 return !(X == Y); 187} 188 189/// CXXBaseSpecifier - A base class of a C++ class. 190/// 191/// Each CXXBaseSpecifier represents a single, direct base class (or 192/// struct) of a C++ class (or struct). It specifies the type of that 193/// base class, whether it is a virtual or non-virtual base, and what 194/// level of access (public, protected, private) is used for the 195/// derivation. For example: 196/// 197/// @code 198/// class A { }; 199/// class B { }; 200/// class C : public virtual A, protected B { }; 201/// @endcode 202/// 203/// In this code, C will have two CXXBaseSpecifiers, one for "public 204/// virtual A" and the other for "protected B". 205class CXXBaseSpecifier { 206 /// Range - The source code range that covers the full base 207 /// specifier, including the "virtual" (if present) and access 208 /// specifier (if present). 209 SourceRange Range; 210 211 /// Virtual - Whether this is a virtual base class or not. 212 bool Virtual : 1; 213 214 /// BaseOfClass - Whether this is the base of a class (true) or of a 215 /// struct (false). This determines the mapping from the access 216 /// specifier as written in the source code to the access specifier 217 /// used for semantic analysis. 218 bool BaseOfClass : 1; 219 220 /// Access - Access specifier as written in the source code (which 221 /// may be AS_none). The actual type of data stored here is an 222 /// AccessSpecifier, but we use "unsigned" here to work around a 223 /// VC++ bug. 224 unsigned Access : 2; 225 226 /// BaseType - The type of the base class. This will be a class or 227 /// struct (or a typedef of such). 228 QualType BaseType; 229 230public: 231 CXXBaseSpecifier() { } 232 233 CXXBaseSpecifier(SourceRange R, bool V, bool BC, AccessSpecifier A, QualType T) 234 : Range(R), Virtual(V), BaseOfClass(BC), Access(A), BaseType(T) { } 235 236 /// getSourceRange - Retrieves the source range that contains the 237 /// entire base specifier. 238 SourceRange getSourceRange() const { return Range; } 239 240 /// isVirtual - Determines whether the base class is a virtual base 241 /// class (or not). 242 bool isVirtual() const { return Virtual; } 243 244 /// getAccessSpecifier - Returns the access specifier for this base 245 /// specifier. This is the actual base specifier as used for 246 /// semantic analysis, so the result can never be AS_none. To 247 /// retrieve the access specifier as written in the source code, use 248 /// getAccessSpecifierAsWritten(). 249 AccessSpecifier getAccessSpecifier() const { 250 if ((AccessSpecifier)Access == AS_none) 251 return BaseOfClass? AS_private : AS_public; 252 else 253 return (AccessSpecifier)Access; 254 } 255 256 /// getAccessSpecifierAsWritten - Retrieves the access specifier as 257 /// written in the source code (which may mean that no access 258 /// specifier was explicitly written). Use getAccessSpecifier() to 259 /// retrieve the access specifier for use in semantic analysis. 260 AccessSpecifier getAccessSpecifierAsWritten() const { 261 return (AccessSpecifier)Access; 262 } 263 264 /// getType - Retrieves the type of the base class. This type will 265 /// always be an unqualified class type. 266 QualType getType() const { return BaseType; } 267}; 268 269/// CXXRecordDecl - Represents a C++ struct/union/class. 270/// FIXME: This class will disappear once we've properly taught RecordDecl 271/// to deal with C++-specific things. 272class CXXRecordDecl : public RecordDecl { 273 /// UserDeclaredConstructor - True when this class has a 274 /// user-declared constructor. 275 bool UserDeclaredConstructor : 1; 276 277 /// UserDeclaredCopyConstructor - True when this class has a 278 /// user-declared copy constructor. 279 bool UserDeclaredCopyConstructor : 1; 280 281 /// UserDeclaredCopyAssignment - True when this class has a 282 /// user-declared copy assignment operator. 283 bool UserDeclaredCopyAssignment : 1; 284 285 /// UserDeclaredDestructor - True when this class has a 286 /// user-declared destructor. 287 bool UserDeclaredDestructor : 1; 288 289 /// Aggregate - True when this class is an aggregate. 290 bool Aggregate : 1; 291 292 /// PlainOldData - True when this class is a POD-type. 293 bool PlainOldData : 1; 294 295 /// Empty - true when this class is empty for traits purposes, i.e. has no 296 /// data members other than 0-width bit-fields, has no virtual function/base, 297 /// and doesn't inherit from a non-empty class. Doesn't take union-ness into 298 /// account. 299 bool Empty : 1; 300 301 /// Polymorphic - True when this class is polymorphic, i.e. has at least one 302 /// virtual member or derives from a polymorphic class. 303 bool Polymorphic : 1; 304 305 /// Abstract - True when this class is abstract, i.e. has at least one 306 /// pure virtual function, (that can come from a base class). 307 bool Abstract : 1; 308 309 /// HasTrivialConstructor - True when this class has a trivial constructor. 310 /// 311 /// C++ [class.ctor]p5. A constructor is trivial if it is an 312 /// implicitly-declared default constructor and if: 313 /// * its class has no virtual functions and no virtual base classes, and 314 /// * all the direct base classes of its class have trivial constructors, and 315 /// * for all the nonstatic data members of its class that are of class type 316 /// (or array thereof), each such class has a trivial constructor. 317 bool HasTrivialConstructor : 1; 318 319 /// HasTrivialCopyConstructor - True when this class has a trivial copy 320 /// constructor. 321 /// 322 /// C++ [class.copy]p6. A copy constructor for class X is trivial 323 /// if it is implicitly declared and if 324 /// * class X has no virtual functions and no virtual base classes, and 325 /// * each direct base class of X has a trivial copy constructor, and 326 /// * for all the nonstatic data members of X that are of class type (or 327 /// array thereof), each such class type has a trivial copy constructor; 328 /// otherwise the copy constructor is non-trivial. 329 bool HasTrivialCopyConstructor : 1; 330 331 /// HasTrivialCopyAssignment - True when this class has a trivial copy 332 /// assignment operator. 333 /// 334 /// C++ [class.copy]p11. A copy assignment operator for class X is 335 /// trivial if it is implicitly declared and if 336 /// * class X has no virtual functions and no virtual base classes, and 337 /// * each direct base class of X has a trivial copy assignment operator, and 338 /// * for all the nonstatic data members of X that are of class type (or 339 /// array thereof), each such class type has a trivial copy assignment 340 /// operator; 341 /// otherwise the copy assignment operator is non-trivial. 342 bool HasTrivialCopyAssignment : 1; 343 344 /// HasTrivialDestructor - True when this class has a trivial destructor. 345 /// 346 /// C++ [class.dtor]p3. A destructor is trivial if it is an 347 /// implicitly-declared destructor and if: 348 /// * all of the direct base classes of its class have trivial destructors 349 /// and 350 /// * for all of the non-static data members of its class that are of class 351 /// type (or array thereof), each such class has a trivial destructor. 352 bool HasTrivialDestructor : 1; 353 354 /// Bases - Base classes of this class. 355 /// FIXME: This is wasted space for a union. 356 CXXBaseSpecifier *Bases; 357 358 /// NumBases - The number of base class specifiers in Bases. 359 unsigned NumBases; 360 361 /// VBases - direct and indirect virtual base classes of this class. 362 CXXBaseSpecifier *VBases; 363 364 /// NumVBases - The number of virtual base class specifiers in VBases. 365 unsigned NumVBases; 366 367 /// Conversions - Overload set containing the conversion functions 368 /// of this C++ class (but not its inherited conversion 369 /// functions). Each of the entries in this overload set is a 370 /// CXXConversionDecl. 371 OverloadedFunctionDecl Conversions; 372 373 /// \brief The template or declaration that this declaration 374 /// describes or was instantiated from, respectively. 375 /// 376 /// For non-templates, this value will be NULL. For record 377 /// declarations that describe a class template, this will be a 378 /// pointer to a ClassTemplateDecl. For member 379 /// classes of class template specializations, this will be the 380 /// RecordDecl from which the member class was instantiated. 381 llvm::PointerUnion<ClassTemplateDecl*, CXXRecordDecl*> 382 TemplateOrInstantiation; 383 384protected: 385 CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 386 SourceLocation L, IdentifierInfo *Id, 387 CXXRecordDecl *PrevDecl, 388 SourceLocation TKL = SourceLocation()); 389 390 ~CXXRecordDecl(); 391 392public: 393 /// base_class_iterator - Iterator that traverses the base classes 394 /// of a class. 395 typedef CXXBaseSpecifier* base_class_iterator; 396 397 /// base_class_const_iterator - Iterator that traverses the base 398 /// classes of a class. 399 typedef const CXXBaseSpecifier* base_class_const_iterator; 400 401 /// reverse_base_class_iterator = Iterator that traverses the base classes 402 /// of a class in reverse order. 403 typedef std::reverse_iterator<base_class_iterator> 404 reverse_base_class_iterator; 405 406 /// reverse_base_class_iterator = Iterator that traverses the base classes 407 /// of a class in reverse order. 408 typedef std::reverse_iterator<base_class_const_iterator> 409 reverse_base_class_const_iterator; 410 411 static CXXRecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 412 SourceLocation L, IdentifierInfo *Id, 413 SourceLocation TKL = SourceLocation(), 414 CXXRecordDecl* PrevDecl=0, 415 bool DelayTypeCreation = false); 416 417 virtual void Destroy(ASTContext& C); 418 419 bool isDynamicClass() const { 420 return Polymorphic || NumVBases!=0; 421 } 422 423 /// setBases - Sets the base classes of this struct or class. 424 void setBases(ASTContext &C, 425 CXXBaseSpecifier const * const *Bases, unsigned NumBases); 426 427 /// getNumBases - Retrieves the number of base classes of this 428 /// class. 429 unsigned getNumBases() const { return NumBases; } 430 431 base_class_iterator bases_begin() { return Bases; } 432 base_class_const_iterator bases_begin() const { return Bases; } 433 base_class_iterator bases_end() { return Bases + NumBases; } 434 base_class_const_iterator bases_end() const { return Bases + NumBases; } 435 reverse_base_class_iterator bases_rbegin() { 436 return reverse_base_class_iterator(bases_end()); 437 } 438 reverse_base_class_const_iterator bases_rbegin() const { 439 return reverse_base_class_const_iterator(bases_end()); 440 } 441 reverse_base_class_iterator bases_rend() { 442 return reverse_base_class_iterator(bases_begin()); 443 } 444 reverse_base_class_const_iterator bases_rend() const { 445 return reverse_base_class_const_iterator(bases_begin()); 446 } 447 448 /// getNumVBases - Retrieves the number of virtual base classes of this 449 /// class. 450 unsigned getNumVBases() const { return NumVBases; } 451 452 base_class_iterator vbases_begin() { return VBases; } 453 base_class_const_iterator vbases_begin() const { return VBases; } 454 base_class_iterator vbases_end() { return VBases + NumVBases; } 455 base_class_const_iterator vbases_end() const { return VBases + NumVBases; } 456 reverse_base_class_iterator vbases_rbegin() { 457 return reverse_base_class_iterator(vbases_end()); 458 } 459 reverse_base_class_const_iterator vbases_rbegin() const { 460 return reverse_base_class_const_iterator(vbases_end()); 461 } 462 reverse_base_class_iterator vbases_rend() { 463 return reverse_base_class_iterator(vbases_begin()); 464 } 465 reverse_base_class_const_iterator vbases_rend() const { 466 return reverse_base_class_const_iterator(vbases_begin()); 467 } 468 469 /// Iterator access to method members. The method iterator visits 470 /// all method members of the class, including non-instance methods, 471 /// special methods, etc. 472 typedef specific_decl_iterator<CXXMethodDecl> method_iterator; 473 474 /// method_begin - Method begin iterator. Iterates in the order the methods 475 /// were declared. 476 method_iterator method_begin() const { 477 return method_iterator(decls_begin()); 478 } 479 /// method_end - Method end iterator. 480 method_iterator method_end() const { 481 return method_iterator(decls_end()); 482 } 483 484 /// Iterator access to constructor members. 485 typedef specific_decl_iterator<CXXConstructorDecl> ctor_iterator; 486 487 ctor_iterator ctor_begin() const { 488 return ctor_iterator(decls_begin()); 489 } 490 ctor_iterator ctor_end() const { 491 return ctor_iterator(decls_end()); 492 } 493 494 /// hasConstCopyConstructor - Determines whether this class has a 495 /// copy constructor that accepts a const-qualified argument. 496 bool hasConstCopyConstructor(ASTContext &Context) const; 497 498 /// getCopyConstructor - Returns the copy constructor for this class 499 CXXConstructorDecl *getCopyConstructor(ASTContext &Context, 500 unsigned TypeQuals) const; 501 502 /// hasConstCopyAssignment - Determines whether this class has a 503 /// copy assignment operator that accepts a const-qualified argument. 504 /// It returns its decl in MD if found. 505 bool hasConstCopyAssignment(ASTContext &Context, 506 const CXXMethodDecl *&MD) const; 507 508 /// addedConstructor - Notify the class that another constructor has 509 /// been added. This routine helps maintain information about the 510 /// class based on which constructors have been added. 511 void addedConstructor(ASTContext &Context, CXXConstructorDecl *ConDecl); 512 513 /// hasUserDeclaredConstructor - Whether this class has any 514 /// user-declared constructors. When true, a default constructor 515 /// will not be implicitly declared. 516 bool hasUserDeclaredConstructor() const { 517 assert((isDefinition() || 518 cast<RecordType>(getTypeForDecl())->isBeingDefined()) && 519 "Incomplete record decl!"); 520 return UserDeclaredConstructor; 521 } 522 523 /// hasUserDeclaredCopyConstructor - Whether this class has a 524 /// user-declared copy constructor. When false, a copy constructor 525 /// will be implicitly declared. 526 bool hasUserDeclaredCopyConstructor() const { 527 return UserDeclaredCopyConstructor; 528 } 529 530 /// addedAssignmentOperator - Notify the class that another assignment 531 /// operator has been added. This routine helps maintain information about the 532 /// class based on which operators have been added. 533 void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl); 534 535 /// hasUserDeclaredCopyAssignment - Whether this class has a 536 /// user-declared copy assignment operator. When false, a copy 537 /// assigment operator will be implicitly declared. 538 bool hasUserDeclaredCopyAssignment() const { 539 return UserDeclaredCopyAssignment; 540 } 541 542 /// hasUserDeclaredDestructor - Whether this class has a 543 /// user-declared destructor. When false, a destructor will be 544 /// implicitly declared. 545 bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; } 546 547 /// setUserDeclaredDestructor - Set whether this class has a 548 /// user-declared destructor. If not set by the time the class is 549 /// fully defined, a destructor will be implicitly declared. 550 void setUserDeclaredDestructor(bool UCD) { 551 UserDeclaredDestructor = UCD; 552 } 553 554 /// getConversions - Retrieve the overload set containing all of the 555 /// conversion functions in this class. 556 OverloadedFunctionDecl *getConversionFunctions() { 557 assert((this->isDefinition() || 558 cast<RecordType>(getTypeForDecl())->isBeingDefined()) && 559 "getConversionFunctions() called on incomplete type"); 560 return &Conversions; 561 } 562 const OverloadedFunctionDecl *getConversionFunctions() const { 563 assert((this->isDefinition() || 564 cast<RecordType>(getTypeForDecl())->isBeingDefined()) && 565 "getConversionFunctions() called on incomplete type"); 566 return &Conversions; 567 } 568 569 /// addConversionFunction - Add a new conversion function to the 570 /// list of conversion functions. 571 void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl); 572 573 /// \brief Add a new conversion function template to the list of conversion 574 /// functions. 575 void addConversionFunction(ASTContext &Context, 576 FunctionTemplateDecl *ConvDecl); 577 578 /// isAggregate - Whether this class is an aggregate (C++ 579 /// [dcl.init.aggr]), which is a class with no user-declared 580 /// constructors, no private or protected non-static data members, 581 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 582 bool isAggregate() const { return Aggregate; } 583 584 /// setAggregate - Set whether this class is an aggregate (C++ 585 /// [dcl.init.aggr]). 586 void setAggregate(bool Agg) { Aggregate = Agg; } 587 588 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 589 /// that is an aggregate that has no non-static non-POD data members, no 590 /// reference data members, no user-defined copy assignment operator and no 591 /// user-defined destructor. 592 bool isPOD() const { return PlainOldData; } 593 594 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 595 void setPOD(bool POD) { PlainOldData = POD; } 596 597 /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which 598 /// means it has a virtual function, virtual base, data member (other than 599 /// 0-width bit-field) or inherits from a non-empty class. Does NOT include 600 /// a check for union-ness. 601 bool isEmpty() const { return Empty; } 602 603 /// Set whether this class is empty (C++0x [meta.unary.prop]) 604 void setEmpty(bool Emp) { Empty = Emp; } 605 606 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 607 /// which means that the class contains or inherits a virtual function. 608 bool isPolymorphic() const { return Polymorphic; } 609 610 /// setPolymorphic - Set whether this class is polymorphic (C++ 611 /// [class.virtual]). 612 void setPolymorphic(bool Poly) { Polymorphic = Poly; } 613 614 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 615 /// which means that the class contains or inherits a pure virtual function. 616 bool isAbstract() const { return Abstract; } 617 618 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 619 void setAbstract(bool Abs) { Abstract = Abs; } 620 621 // hasTrivialConstructor - Whether this class has a trivial constructor 622 // (C++ [class.ctor]p5) 623 bool hasTrivialConstructor() const { return HasTrivialConstructor; } 624 625 // setHasTrivialConstructor - Set whether this class has a trivial constructor 626 // (C++ [class.ctor]p5) 627 void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; } 628 629 // hasTrivialCopyConstructor - Whether this class has a trivial copy 630 // constructor (C++ [class.copy]p6) 631 bool hasTrivialCopyConstructor() const { return HasTrivialCopyConstructor; } 632 633 // setHasTrivialCopyConstructor - Set whether this class has a trivial 634 // copy constructor (C++ [class.copy]p6) 635 void setHasTrivialCopyConstructor(bool TC) { HasTrivialCopyConstructor = TC; } 636 637 // hasTrivialCopyAssignment - Whether this class has a trivial copy 638 // assignment operator (C++ [class.copy]p11) 639 bool hasTrivialCopyAssignment() const { return HasTrivialCopyAssignment; } 640 641 // setHasTrivialCopyAssignment - Set whether this class has a 642 // trivial copy assignment operator (C++ [class.copy]p11) 643 void setHasTrivialCopyAssignment(bool TC) { HasTrivialCopyAssignment = TC; } 644 645 // hasTrivialDestructor - Whether this class has a trivial destructor 646 // (C++ [class.dtor]p3) 647 bool hasTrivialDestructor() const { return HasTrivialDestructor; } 648 649 // setHasTrivialDestructor - Set whether this class has a trivial destructor 650 // (C++ [class.dtor]p3) 651 void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; } 652 653 /// \brief If this record is an instantiation of a member class, 654 /// retrieves the member class from which it was instantiated. 655 /// 656 /// This routine will return non-NULL for (non-templated) member 657 /// classes of class templates. For example, given: 658 /// 659 /// \code 660 /// template<typename T> 661 /// struct X { 662 /// struct A { }; 663 /// }; 664 /// \endcode 665 /// 666 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 667 /// whose parent is the class template specialization X<int>. For 668 /// this declaration, getInstantiatedFromMemberClass() will return 669 /// the CXXRecordDecl X<T>::A. When a complete definition of 670 /// X<int>::A is required, it will be instantiated from the 671 /// declaration returned by getInstantiatedFromMemberClass(). 672 CXXRecordDecl *getInstantiatedFromMemberClass() const { 673 return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>(); 674 } 675 676 /// \brief Specify that this record is an instantiation of the 677 /// member class RD. 678 void setInstantiationOfMemberClass(CXXRecordDecl *RD) { 679 TemplateOrInstantiation = RD; 680 } 681 682 /// \brief Retrieves the class template that is described by this 683 /// class declaration. 684 /// 685 /// Every class template is represented as a ClassTemplateDecl and a 686 /// CXXRecordDecl. The former contains template properties (such as 687 /// the template parameter lists) while the latter contains the 688 /// actual description of the template's 689 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 690 /// CXXRecordDecl that from a ClassTemplateDecl, while 691 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 692 /// a CXXRecordDecl. 693 ClassTemplateDecl *getDescribedClassTemplate() const { 694 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 695 } 696 697 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 698 TemplateOrInstantiation = Template; 699 } 700 701 /// getDefaultConstructor - Returns the default constructor for this class 702 CXXConstructorDecl *getDefaultConstructor(ASTContext &Context); 703 704 /// getDestructor - Returns the destructor decl for this class. 705 const CXXDestructorDecl *getDestructor(ASTContext &Context); 706 707 /// isLocalClass - If the class is a local class [class.local], returns 708 /// the enclosing function declaration. 709 const FunctionDecl *isLocalClass() const { 710 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) 711 return RD->isLocalClass(); 712 713 return dyn_cast<FunctionDecl>(getDeclContext()); 714 } 715 716 /// viewInheritance - Renders and displays an inheritance diagram 717 /// for this C++ class and all of its base classes (transitively) using 718 /// GraphViz. 719 void viewInheritance(ASTContext& Context) const; 720 721 static bool classof(const Decl *D) { 722 return D->getKind() == CXXRecord || 723 D->getKind() == ClassTemplateSpecialization || 724 D->getKind() == ClassTemplatePartialSpecialization; 725 } 726 static bool classof(const CXXRecordDecl *D) { return true; } 727 static bool classof(const ClassTemplateSpecializationDecl *D) { 728 return true; 729 } 730}; 731 732/// CXXMethodDecl - Represents a static or instance method of a 733/// struct/union/class. 734class CXXMethodDecl : public FunctionDecl { 735protected: 736 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 737 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 738 bool isStatic, bool isInline) 739 : FunctionDecl(DK, RD, L, N, T, DInfo, (isStatic ? Static : None), 740 isInline) {} 741 742public: 743 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 744 SourceLocation L, DeclarationName N, 745 QualType T, DeclaratorInfo *DInfo, 746 bool isStatic = false, 747 bool isInline = false); 748 749 bool isStatic() const { return getStorageClass() == Static; } 750 bool isInstance() const { return !isStatic(); } 751 752 bool isVirtual() const { 753 return isVirtualAsWritten() || 754 (begin_overridden_methods() != end_overridden_methods()); 755 } 756 757 /// 758 void addOverriddenMethod(const CXXMethodDecl *MD); 759 760 typedef const CXXMethodDecl ** method_iterator; 761 762 method_iterator begin_overridden_methods() const; 763 method_iterator end_overridden_methods() const; 764 765 /// getParent - Returns the parent of this method declaration, which 766 /// is the class in which this method is defined. 767 const CXXRecordDecl *getParent() const { 768 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 769 } 770 771 /// getParent - Returns the parent of this method declaration, which 772 /// is the class in which this method is defined. 773 CXXRecordDecl *getParent() { 774 return const_cast<CXXRecordDecl *>( 775 cast<CXXRecordDecl>(FunctionDecl::getParent())); 776 } 777 778 /// getThisType - Returns the type of 'this' pointer. 779 /// Should only be called for instance methods. 780 QualType getThisType(ASTContext &C) const; 781 782 unsigned getTypeQualifiers() const { 783 return getType()->getAsFunctionProtoType()->getTypeQuals(); 784 } 785 786 // Implement isa/cast/dyncast/etc. 787 static bool classof(const Decl *D) { 788 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 789 } 790 static bool classof(const CXXMethodDecl *D) { return true; } 791}; 792 793/// CXXBaseOrMemberInitializer - Represents a C++ base or member 794/// initializer, which is part of a constructor initializer that 795/// initializes one non-static member variable or one base class. For 796/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 797/// initializers: 798/// 799/// @code 800/// class A { }; 801/// class B : public A { 802/// float f; 803/// public: 804/// B(A& a) : A(a), f(3.14159) { } 805/// }; 806/// @endcode 807class CXXBaseOrMemberInitializer { 808 /// BaseOrMember - This points to the entity being initialized, 809 /// which is either a base class (a Type) or a non-static data 810 /// member. When the low bit is 1, it's a base 811 /// class; when the low bit is 0, it's a member. 812 uintptr_t BaseOrMember; 813 814 /// Args - The arguments used to initialize the base or member. 815 Stmt **Args; 816 unsigned NumArgs; 817 818 union { 819 /// CtorToCall - For a base or member needing a constructor for their 820 /// initialization, this is the constructor to call. 821 CXXConstructorDecl *CtorToCall; 822 823 /// AnonUnionMember - When 'BaseOrMember' is class's anonymous union 824 /// data member, this field holds the FieldDecl for the member of the 825 /// anonymous union being initialized. 826 /// @code 827 /// struct X { 828 /// X() : au_i1(123) {} 829 /// union { 830 /// int au_i1; 831 /// float au_f1; 832 /// }; 833 /// }; 834 /// @endcode 835 /// In above example, BaseOrMember holds the field decl. for anonymous union 836 /// and AnonUnionMember holds field decl for au_i1. 837 /// 838 FieldDecl *AnonUnionMember; 839 }; 840 841 /// IdLoc - Location of the id in ctor-initializer list. 842 SourceLocation IdLoc; 843 844public: 845 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 846 explicit 847 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs, 848 CXXConstructorDecl *C, 849 SourceLocation L); 850 851 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 852 explicit 853 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs, 854 CXXConstructorDecl *C, 855 SourceLocation L); 856 857 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 858 ~CXXBaseOrMemberInitializer(); 859 860 /// arg_iterator - Iterates through the member initialization 861 /// arguments. 862 typedef ExprIterator arg_iterator; 863 864 /// arg_const_iterator - Iterates through the member initialization 865 /// arguments. 866 typedef ConstExprIterator const_arg_iterator; 867 868 /// getBaseOrMember - get the generic 'member' representing either the field 869 /// or a base class. 870 void* getBaseOrMember() const { return reinterpret_cast<void*>(BaseOrMember); } 871 872 /// isBaseInitializer - Returns true when this initializer is 873 /// initializing a base class. 874 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 875 876 /// isMemberInitializer - Returns true when this initializer is 877 /// initializing a non-static data member. 878 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 879 880 /// getBaseClass - If this is a base class initializer, returns the 881 /// type used to specify the initializer. The resulting type will be 882 /// a class type or a typedef of a class type. If this is not a base 883 /// class initializer, returns NULL. 884 Type *getBaseClass() { 885 if (isBaseInitializer()) 886 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 887 else 888 return 0; 889 } 890 891 /// getBaseClass - If this is a base class initializer, returns the 892 /// type used to specify the initializer. The resulting type will be 893 /// a class type or a typedef of a class type. If this is not a base 894 /// class initializer, returns NULL. 895 const Type *getBaseClass() const { 896 if (isBaseInitializer()) 897 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 898 else 899 return 0; 900 } 901 902 /// getMember - If this is a member initializer, returns the 903 /// declaration of the non-static data member being 904 /// initialized. Otherwise, returns NULL. 905 FieldDecl *getMember() { 906 if (isMemberInitializer()) 907 return reinterpret_cast<FieldDecl *>(BaseOrMember); 908 else 909 return 0; 910 } 911 912 void setMember(FieldDecl * anonUnionField) { 913 BaseOrMember = reinterpret_cast<uintptr_t>(anonUnionField); 914 } 915 916 FieldDecl *getAnonUnionMember() const { 917 return AnonUnionMember; 918 } 919 void setAnonUnionMember(FieldDecl *anonMember) { 920 AnonUnionMember = anonMember; 921 } 922 923 const CXXConstructorDecl *getConstructor() const { return CtorToCall; } 924 925 SourceLocation getSourceLocation() const { return IdLoc; } 926 927 /// arg_begin() - Retrieve an iterator to the first initializer argument. 928 arg_iterator arg_begin() { return Args; } 929 /// arg_begin() - Retrieve an iterator to the first initializer argument. 930 const_arg_iterator const_arg_begin() const { return Args; } 931 932 /// arg_end() - Retrieve an iterator past the last initializer argument. 933 arg_iterator arg_end() { return Args + NumArgs; } 934 /// arg_end() - Retrieve an iterator past the last initializer argument. 935 const_arg_iterator const_arg_end() const { return Args + NumArgs; } 936 937 /// getNumArgs - Determine the number of arguments used to 938 /// initialize the member or base. 939 unsigned getNumArgs() const { return NumArgs; } 940}; 941 942/// CXXConstructorDecl - Represents a C++ constructor within a 943/// class. For example: 944/// 945/// @code 946/// class X { 947/// public: 948/// explicit X(int); // represented by a CXXConstructorDecl. 949/// }; 950/// @endcode 951class CXXConstructorDecl : public CXXMethodDecl { 952 /// Explicit - Whether this constructor is explicit. 953 bool Explicit : 1; 954 955 /// ImplicitlyDefined - Whether this constructor was implicitly 956 /// defined by the compiler. When false, the constructor was defined 957 /// by the user. In C++03, this flag will have the same value as 958 /// Implicit. In C++0x, however, a constructor that is 959 /// explicitly defaulted (i.e., defined with " = default") will have 960 /// @c !Implicit && ImplicitlyDefined. 961 bool ImplicitlyDefined : 1; 962 963 /// Support for base and member initializers. 964 /// BaseOrMemberInitializers - The arguments used to initialize the base 965 /// or member. 966 CXXBaseOrMemberInitializer **BaseOrMemberInitializers; 967 unsigned NumBaseOrMemberInitializers; 968 969 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 970 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 971 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 972 : CXXMethodDecl(CXXConstructor, RD, L, N, T, DInfo, false, isInline), 973 Explicit(isExplicit), ImplicitlyDefined(false), 974 BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) { 975 setImplicit(isImplicitlyDeclared); 976 } 977 virtual void Destroy(ASTContext& C); 978 979public: 980 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 981 SourceLocation L, DeclarationName N, 982 QualType T, DeclaratorInfo *DInfo, 983 bool isExplicit, 984 bool isInline, bool isImplicitlyDeclared); 985 986 /// isExplicit - Whether this constructor was marked "explicit" or not. 987 bool isExplicit() const { return Explicit; } 988 989 /// isImplicitlyDefined - Whether this constructor was implicitly 990 /// defined. If false, then this constructor was defined by the 991 /// user. This operation can only be invoked if the constructor has 992 /// already been defined. 993 bool isImplicitlyDefined(ASTContext &C) const { 994 assert(isThisDeclarationADefinition() && 995 "Can only get the implicit-definition flag once the " 996 "constructor has been defined"); 997 return ImplicitlyDefined; 998 } 999 1000 /// setImplicitlyDefined - Set whether this constructor was 1001 /// implicitly defined or not. 1002 void setImplicitlyDefined(bool ID) { 1003 assert(isThisDeclarationADefinition() && 1004 "Can only set the implicit-definition flag once the constructor " 1005 "has been defined"); 1006 ImplicitlyDefined = ID; 1007 } 1008 1009 /// init_iterator - Iterates through the member/base initializer list. 1010 typedef CXXBaseOrMemberInitializer **init_iterator; 1011 1012 /// init_const_iterator - Iterates through the memberbase initializer list. 1013 typedef CXXBaseOrMemberInitializer * const * init_const_iterator; 1014 1015 /// init_begin() - Retrieve an iterator to the first initializer. 1016 init_iterator init_begin() { return BaseOrMemberInitializers; } 1017 /// begin() - Retrieve an iterator to the first initializer. 1018 init_const_iterator init_begin() const { return BaseOrMemberInitializers; } 1019 1020 /// init_end() - Retrieve an iterator past the last initializer. 1021 init_iterator init_end() { 1022 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1023 } 1024 /// end() - Retrieve an iterator past the last initializer. 1025 init_const_iterator init_end() const { 1026 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1027 } 1028 1029 /// getNumArgs - Determine the number of arguments used to 1030 /// initialize the member or base. 1031 unsigned getNumBaseOrMemberInitializers() const { 1032 return NumBaseOrMemberInitializers; 1033 } 1034 1035 void setBaseOrMemberInitializers(ASTContext &C, 1036 CXXBaseOrMemberInitializer **Initializers, 1037 unsigned NumInitializers, 1038 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases, 1039 llvm::SmallVectorImpl<FieldDecl *>&Members); 1040 1041 /// isDefaultConstructor - Whether this constructor is a default 1042 /// constructor (C++ [class.ctor]p5), which can be used to 1043 /// default-initialize a class of this type. 1044 bool isDefaultConstructor() const; 1045 1046 /// isCopyConstructor - Whether this constructor is a copy 1047 /// constructor (C++ [class.copy]p2, which can be used to copy the 1048 /// class. @p TypeQuals will be set to the qualifiers on the 1049 /// argument type. For example, @p TypeQuals would be set to @c 1050 /// QualType::Const for the following copy constructor: 1051 /// 1052 /// @code 1053 /// class X { 1054 /// public: 1055 /// X(const X&); 1056 /// }; 1057 /// @endcode 1058 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 1059 1060 /// isCopyConstructor - Whether this constructor is a copy 1061 /// constructor (C++ [class.copy]p2, which can be used to copy the 1062 /// class. 1063 bool isCopyConstructor(ASTContext &Context) const { 1064 unsigned TypeQuals = 0; 1065 return isCopyConstructor(Context, TypeQuals); 1066 } 1067 1068 /// isConvertingConstructor - Whether this constructor is a 1069 /// converting constructor (C++ [class.conv.ctor]), which can be 1070 /// used for user-defined conversions. 1071 bool isConvertingConstructor() const; 1072 1073 // Implement isa/cast/dyncast/etc. 1074 static bool classof(const Decl *D) { 1075 return D->getKind() == CXXConstructor; 1076 } 1077 static bool classof(const CXXConstructorDecl *D) { return true; } 1078}; 1079 1080/// CXXDestructorDecl - Represents a C++ destructor within a 1081/// class. For example: 1082/// 1083/// @code 1084/// class X { 1085/// public: 1086/// ~X(); // represented by a CXXDestructorDecl. 1087/// }; 1088/// @endcode 1089class CXXDestructorDecl : public CXXMethodDecl { 1090 enum KindOfObjectToDestroy { 1091 VBASE = 0x1, 1092 DRCTNONVBASE = 0x2, 1093 ANYBASE = 0x3 1094 }; 1095 1096 /// ImplicitlyDefined - Whether this destructor was implicitly 1097 /// defined by the compiler. When false, the destructor was defined 1098 /// by the user. In C++03, this flag will have the same value as 1099 /// Implicit. In C++0x, however, a destructor that is 1100 /// explicitly defaulted (i.e., defined with " = default") will have 1101 /// @c !Implicit && ImplicitlyDefined. 1102 bool ImplicitlyDefined : 1; 1103 1104 /// Support for base and member destruction. 1105 /// BaseOrMemberDestructions - The arguments used to destruct the base 1106 /// or member. Each uintptr_t value represents one of base classes (either 1107 /// virtual or direct non-virtual base), or non-static data member 1108 /// to be destroyed. The low two bits encode the kind of object 1109 /// being destroyed. 1110 uintptr_t *BaseOrMemberDestructions; 1111 unsigned NumBaseOrMemberDestructions; 1112 1113 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 1114 DeclarationName N, QualType T, 1115 bool isInline, bool isImplicitlyDeclared) 1116 : CXXMethodDecl(CXXDestructor, RD, L, N, T, /*DInfo=*/0, false, isInline), 1117 ImplicitlyDefined(false), 1118 BaseOrMemberDestructions(0), NumBaseOrMemberDestructions(0) { 1119 setImplicit(isImplicitlyDeclared); 1120 } 1121 virtual void Destroy(ASTContext& C); 1122 1123public: 1124 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1125 SourceLocation L, DeclarationName N, 1126 QualType T, bool isInline, 1127 bool isImplicitlyDeclared); 1128 1129 /// isImplicitlyDefined - Whether this destructor was implicitly 1130 /// defined. If false, then this destructor was defined by the 1131 /// user. This operation can only be invoked if the destructor has 1132 /// already been defined. 1133 bool isImplicitlyDefined() const { 1134 assert(isThisDeclarationADefinition() && 1135 "Can only get the implicit-definition flag once the destructor has been defined"); 1136 return ImplicitlyDefined; 1137 } 1138 1139 /// setImplicitlyDefined - Set whether this destructor was 1140 /// implicitly defined or not. 1141 void setImplicitlyDefined(bool ID) { 1142 assert(isThisDeclarationADefinition() && 1143 "Can only set the implicit-definition flag once the destructor has been defined"); 1144 ImplicitlyDefined = ID; 1145 } 1146 1147 /// destr_iterator - Iterates through the member/base destruction list. 1148 1149 /// destr_const_iterator - Iterates through the member/base destruction list. 1150 typedef uintptr_t const destr_const_iterator; 1151 1152 /// destr_begin() - Retrieve an iterator to the first destructed member/base. 1153 uintptr_t* destr_begin() { 1154 return BaseOrMemberDestructions; 1155 } 1156 /// destr_begin() - Retrieve an iterator to the first destructed member/base. 1157 uintptr_t* destr_begin() const { 1158 return BaseOrMemberDestructions; 1159 } 1160 1161 /// destr_end() - Retrieve an iterator past the last destructed member/base. 1162 uintptr_t* destr_end() { 1163 return BaseOrMemberDestructions + NumBaseOrMemberDestructions; 1164 } 1165 /// destr_end() - Retrieve an iterator past the last destructed member/base. 1166 uintptr_t* destr_end() const { 1167 return BaseOrMemberDestructions + NumBaseOrMemberDestructions; 1168 } 1169 1170 /// getNumBaseOrMemberDestructions - Number of base and non-static members 1171 /// to destroy. 1172 unsigned getNumBaseOrMemberDestructions() const { 1173 return NumBaseOrMemberDestructions; 1174 } 1175 1176 /// getBaseOrMember - get the generic 'member' representing either the field 1177 /// or a base class. 1178 uintptr_t* getBaseOrMemberToDestroy() const { 1179 return BaseOrMemberDestructions; 1180 } 1181 1182 /// isVbaseToDestroy - returns true, if object is virtual base. 1183 bool isVbaseToDestroy(uintptr_t Vbase) const { 1184 return (Vbase & VBASE) != 0; 1185 } 1186 /// isDirectNonVBaseToDestroy - returns true, if object is direct non-virtual 1187 /// base. 1188 bool isDirectNonVBaseToDestroy(uintptr_t DrctNonVbase) const { 1189 return (DrctNonVbase & DRCTNONVBASE) != 0; 1190 } 1191 /// isAnyBaseToDestroy - returns true, if object is any base (virtual or 1192 /// direct non-virtual) 1193 bool isAnyBaseToDestroy(uintptr_t AnyBase) const { 1194 return (AnyBase & ANYBASE) != 0; 1195 } 1196 /// isMemberToDestroy - returns true if object is a non-static data member. 1197 bool isMemberToDestroy(uintptr_t Member) const { 1198 return (Member & ANYBASE) == 0; 1199 } 1200 /// getAnyBaseClassToDestroy - Get the type for the given base class object. 1201 Type *getAnyBaseClassToDestroy(uintptr_t Base) const { 1202 if (isAnyBaseToDestroy(Base)) 1203 return reinterpret_cast<Type*>(Base & ~0x03); 1204 return 0; 1205 } 1206 /// getMemberToDestroy - Get the member for the given object. 1207 FieldDecl *getMemberToDestroy(uintptr_t Member) const { 1208 if (isMemberToDestroy(Member)) 1209 return reinterpret_cast<FieldDecl *>(Member); 1210 return 0; 1211 } 1212 /// getVbaseClassToDestroy - Get the virtual base. 1213 Type *getVbaseClassToDestroy(uintptr_t Vbase) const { 1214 if (isVbaseToDestroy(Vbase)) 1215 return reinterpret_cast<Type*>(Vbase & ~0x01); 1216 return 0; 1217 } 1218 /// getDirectNonVBaseClassToDestroy - Get the virtual base. 1219 Type *getDirectNonVBaseClassToDestroy(uintptr_t Base) const { 1220 if (isDirectNonVBaseToDestroy(Base)) 1221 return reinterpret_cast<Type*>(Base & ~0x02); 1222 return 0; 1223 } 1224 1225 /// computeBaseOrMembersToDestroy - Compute information in current 1226 /// destructor decl's AST of bases and non-static data members which will be 1227 /// implicitly destroyed. We are storing the destruction in the order that 1228 /// they should occur (which is the reverse of construction order). 1229 void computeBaseOrMembersToDestroy(ASTContext &C); 1230 1231 // Implement isa/cast/dyncast/etc. 1232 static bool classof(const Decl *D) { 1233 return D->getKind() == CXXDestructor; 1234 } 1235 static bool classof(const CXXDestructorDecl *D) { return true; } 1236}; 1237 1238/// CXXConversionDecl - Represents a C++ conversion function within a 1239/// class. For example: 1240/// 1241/// @code 1242/// class X { 1243/// public: 1244/// operator bool(); 1245/// }; 1246/// @endcode 1247class CXXConversionDecl : public CXXMethodDecl { 1248 /// Explicit - Whether this conversion function is marked 1249 /// "explicit", meaning that it can only be applied when the user 1250 /// explicitly wrote a cast. This is a C++0x feature. 1251 bool Explicit : 1; 1252 1253 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 1254 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 1255 bool isInline, bool isExplicit) 1256 : CXXMethodDecl(CXXConversion, RD, L, N, T, DInfo, false, isInline), 1257 Explicit(isExplicit) { } 1258 1259public: 1260 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1261 SourceLocation L, DeclarationName N, 1262 QualType T, DeclaratorInfo *DInfo, 1263 bool isInline, bool isExplicit); 1264 1265 /// isExplicit - Whether this is an explicit conversion operator 1266 /// (C++0x only). Explicit conversion operators are only considered 1267 /// when the user has explicitly written a cast. 1268 bool isExplicit() const { return Explicit; } 1269 1270 /// getConversionType - Returns the type that this conversion 1271 /// function is converting to. 1272 QualType getConversionType() const { 1273 return getType()->getAsFunctionType()->getResultType(); 1274 } 1275 1276 // Implement isa/cast/dyncast/etc. 1277 static bool classof(const Decl *D) { 1278 return D->getKind() == CXXConversion; 1279 } 1280 static bool classof(const CXXConversionDecl *D) { return true; } 1281}; 1282 1283/// FriendFunctionDecl - Represents the declaration (and possibly 1284/// the definition) of a friend function. For example: 1285/// 1286/// @code 1287/// class A { 1288/// friend int foo(int); 1289/// }; 1290/// @endcode 1291class FriendFunctionDecl : public FunctionDecl { 1292 // Location of the 'friend' specifier. 1293 const SourceLocation FriendLoc; 1294 1295 FriendFunctionDecl(DeclContext *DC, SourceLocation L, 1296 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 1297 bool isInline, SourceLocation FriendL) 1298 : FunctionDecl(FriendFunction, DC, L, N, T, DInfo, None, isInline), 1299 FriendLoc(FriendL) 1300 {} 1301 1302public: 1303 static FriendFunctionDecl *Create(ASTContext &C, DeclContext *DC, 1304 SourceLocation L, DeclarationName N, 1305 QualType T, DeclaratorInfo *DInfo, 1306 bool isInline, SourceLocation FriendL); 1307 1308 SourceLocation getFriendLoc() const { 1309 return FriendLoc; 1310 } 1311 1312 // Implement isa/cast/dyncast/etc. 1313 static bool classof(const Decl *D) { 1314 return D->getKind() == FriendFunction; 1315 } 1316 static bool classof(const FriendFunctionDecl *D) { return true; } 1317}; 1318 1319/// FriendClassDecl - Represents the declaration of a friend class. 1320/// For example: 1321/// 1322/// @code 1323/// class X { 1324/// friend class Y; 1325/// }; 1326/// @endcode 1327class FriendClassDecl : public Decl { 1328 // The friended type. In C++0x, this can be an arbitrary type, 1329 // which we simply ignore if it's not a record type. 1330 QualType FriendType; 1331 1332 // Location of the 'friend' specifier. 1333 SourceLocation FriendLoc; 1334 1335 FriendClassDecl(DeclContext *DC, SourceLocation L, 1336 QualType T, SourceLocation FriendL) 1337 : Decl(FriendClass, DC, L), 1338 FriendType(T), 1339 FriendLoc(FriendL) 1340 {} 1341 1342public: 1343 static FriendClassDecl *Create(ASTContext &C, DeclContext *DC, 1344 SourceLocation L, QualType T, 1345 SourceLocation FriendL); 1346 1347 QualType getFriendType() const { 1348 return FriendType; 1349 } 1350 1351 SourceLocation getFriendLoc() const { 1352 return FriendLoc; 1353 } 1354 1355 // Implement isa/cast/dyncast/etc. 1356 static bool classof(const Decl *D) { 1357 return D->getKind() == FriendClass; 1358 } 1359 static bool classof(const FriendClassDecl *D) { return true; } 1360}; 1361 1362/// LinkageSpecDecl - This represents a linkage specification. For example: 1363/// extern "C" void foo(); 1364/// 1365class LinkageSpecDecl : public Decl, public DeclContext { 1366public: 1367 /// LanguageIDs - Used to represent the language in a linkage 1368 /// specification. The values are part of the serialization abi for 1369 /// ASTs and cannot be changed without altering that abi. To help 1370 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 1371 /// from the dwarf standard. 1372 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 1373 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 1374private: 1375 /// Language - The language for this linkage specification. 1376 LanguageIDs Language; 1377 1378 /// HadBraces - Whether this linkage specification had curly braces or not. 1379 bool HadBraces : 1; 1380 1381 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 1382 bool Braces) 1383 : Decl(LinkageSpec, DC, L), 1384 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 1385 1386public: 1387 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 1388 SourceLocation L, LanguageIDs Lang, 1389 bool Braces); 1390 1391 LanguageIDs getLanguage() const { return Language; } 1392 1393 /// hasBraces - Determines whether this linkage specification had 1394 /// braces in its syntactic form. 1395 bool hasBraces() const { return HadBraces; } 1396 1397 static bool classof(const Decl *D) { 1398 return D->getKind() == LinkageSpec; 1399 } 1400 static bool classof(const LinkageSpecDecl *D) { return true; } 1401 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 1402 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 1403 } 1404 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 1405 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 1406 } 1407}; 1408 1409/// UsingDirectiveDecl - Represents C++ using-directive. For example: 1410/// 1411/// using namespace std; 1412/// 1413// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 1414// artificial name, for all using-directives in order to store 1415// them in DeclContext effectively. 1416class UsingDirectiveDecl : public NamedDecl { 1417 1418 /// SourceLocation - Location of 'namespace' token. 1419 SourceLocation NamespaceLoc; 1420 1421 /// \brief The source range that covers the nested-name-specifier 1422 /// preceding the namespace name. 1423 SourceRange QualifierRange; 1424 1425 /// \brief The nested-name-specifier that precedes the namespace 1426 /// name, if any. 1427 NestedNameSpecifier *Qualifier; 1428 1429 /// IdentLoc - Location of nominated namespace-name identifier. 1430 // FIXME: We don't store location of scope specifier. 1431 SourceLocation IdentLoc; 1432 1433 /// NominatedNamespace - Namespace nominated by using-directive. 1434 NamespaceDecl *NominatedNamespace; 1435 1436 /// Enclosing context containing both using-directive and nomintated 1437 /// namespace. 1438 DeclContext *CommonAncestor; 1439 1440 /// getUsingDirectiveName - Returns special DeclarationName used by 1441 /// using-directives. This is only used by DeclContext for storing 1442 /// UsingDirectiveDecls in its lookup structure. 1443 static DeclarationName getName() { 1444 return DeclarationName::getUsingDirectiveName(); 1445 } 1446 1447 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 1448 SourceLocation NamespcLoc, 1449 SourceRange QualifierRange, 1450 NestedNameSpecifier *Qualifier, 1451 SourceLocation IdentLoc, 1452 NamespaceDecl *Nominated, 1453 DeclContext *CommonAncestor) 1454 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 1455 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 1456 Qualifier(Qualifier), IdentLoc(IdentLoc), 1457 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 1458 CommonAncestor(CommonAncestor) { 1459 } 1460 1461public: 1462 /// \brief Retrieve the source range of the nested-name-specifier 1463 /// that qualifiers the namespace name. 1464 SourceRange getQualifierRange() const { return QualifierRange; } 1465 1466 /// \brief Retrieve the nested-name-specifier that qualifies the 1467 /// name of the namespace. 1468 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1469 1470 /// getNominatedNamespace - Returns namespace nominated by using-directive. 1471 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 1472 1473 const NamespaceDecl *getNominatedNamespace() const { 1474 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 1475 } 1476 1477 /// getCommonAncestor - returns common ancestor context of using-directive, 1478 /// and nominated by it namespace. 1479 DeclContext *getCommonAncestor() { return CommonAncestor; } 1480 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 1481 1482 /// getNamespaceKeyLocation - Returns location of namespace keyword. 1483 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 1484 1485 /// getIdentLocation - Returns location of identifier. 1486 SourceLocation getIdentLocation() const { return IdentLoc; } 1487 1488 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 1489 SourceLocation L, 1490 SourceLocation NamespaceLoc, 1491 SourceRange QualifierRange, 1492 NestedNameSpecifier *Qualifier, 1493 SourceLocation IdentLoc, 1494 NamespaceDecl *Nominated, 1495 DeclContext *CommonAncestor); 1496 1497 static bool classof(const Decl *D) { 1498 return D->getKind() == Decl::UsingDirective; 1499 } 1500 static bool classof(const UsingDirectiveDecl *D) { return true; } 1501 1502 // Friend for getUsingDirectiveName. 1503 friend class DeclContext; 1504}; 1505 1506/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 1507/// 1508/// @code 1509/// namespace Foo = Bar; 1510/// @endcode 1511class NamespaceAliasDecl : public NamedDecl { 1512 SourceLocation AliasLoc; 1513 1514 /// \brief The source range that covers the nested-name-specifier 1515 /// preceding the namespace name. 1516 SourceRange QualifierRange; 1517 1518 /// \brief The nested-name-specifier that precedes the namespace 1519 /// name, if any. 1520 NestedNameSpecifier *Qualifier; 1521 1522 /// IdentLoc - Location of namespace identifier. 1523 SourceLocation IdentLoc; 1524 1525 /// Namespace - The Decl that this alias points to. Can either be a 1526 /// NamespaceDecl or a NamespaceAliasDecl. 1527 NamedDecl *Namespace; 1528 1529 NamespaceAliasDecl(DeclContext *DC, SourceLocation L, 1530 SourceLocation AliasLoc, IdentifierInfo *Alias, 1531 SourceRange QualifierRange, 1532 NestedNameSpecifier *Qualifier, 1533 SourceLocation IdentLoc, NamedDecl *Namespace) 1534 : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), 1535 QualifierRange(QualifierRange), Qualifier(Qualifier), 1536 IdentLoc(IdentLoc), Namespace(Namespace) { } 1537 1538public: 1539 /// \brief Retrieve the source range of the nested-name-specifier 1540 /// that qualifiers the namespace name. 1541 SourceRange getQualifierRange() const { return QualifierRange; } 1542 1543 /// \brief Retrieve the nested-name-specifier that qualifies the 1544 /// name of the namespace. 1545 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1546 1547 NamespaceDecl *getNamespace() { 1548 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1549 return AD->getNamespace(); 1550 1551 return cast<NamespaceDecl>(Namespace); 1552 } 1553 1554 const NamespaceDecl *getNamespace() const { 1555 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1556 } 1557 1558 /// \brief Retrieve the namespace that this alias refers to, which 1559 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1560 NamedDecl *getAliasedNamespace() const { return Namespace; } 1561 1562 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1563 SourceLocation L, SourceLocation AliasLoc, 1564 IdentifierInfo *Alias, 1565 SourceRange QualifierRange, 1566 NestedNameSpecifier *Qualifier, 1567 SourceLocation IdentLoc, 1568 NamedDecl *Namespace); 1569 1570 static bool classof(const Decl *D) { 1571 return D->getKind() == Decl::NamespaceAlias; 1572 } 1573 static bool classof(const NamespaceAliasDecl *D) { return true; } 1574}; 1575 1576/// UsingDecl - Represents a C++ using-declaration. For example: 1577/// using someNameSpace::someIdentifier; 1578class UsingDecl : public NamedDecl { 1579 1580 /// \brief The source range that covers the nested-name-specifier 1581 /// preceding the declaration name. 1582 SourceRange NestedNameRange; 1583 /// \brief The source location of the target declaration name. 1584 SourceLocation TargetNameLocation; 1585 /// \brief The source location of the "using" location itself. 1586 SourceLocation UsingLocation; 1587 /// \brief Target declaration. 1588 NamedDecl* TargetDecl; 1589 /// \brief Target declaration. 1590 NestedNameSpecifier* TargetNestedNameDecl; 1591 1592 // Had 'typename' keyword. 1593 bool IsTypeName; 1594 1595 UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR, 1596 SourceLocation TargetNL, SourceLocation UL, NamedDecl* Target, 1597 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) 1598 : NamedDecl(Decl::Using, DC, L, Target->getDeclName()), 1599 NestedNameRange(NNR), TargetNameLocation(TargetNL), 1600 UsingLocation(UL), TargetDecl(Target), 1601 TargetNestedNameDecl(TargetNNS), IsTypeName(IsTypeNameArg) { 1602 this->IdentifierNamespace = TargetDecl->getIdentifierNamespace(); 1603 } 1604 1605public: 1606 /// \brief Returns the source range that covers the nested-name-specifier 1607 /// preceding the namespace name. 1608 SourceRange getNestedNameRange() { return NestedNameRange; } 1609 1610 /// \brief Returns the source location of the target declaration name. 1611 SourceLocation getTargetNameLocation() { return TargetNameLocation; } 1612 1613 /// \brief Returns the source location of the "using" location itself. 1614 SourceLocation getUsingLocation() { return UsingLocation; } 1615 1616 /// \brief getTargetDecl - Returns target specified by using-decl. 1617 NamedDecl *getTargetDecl() { return TargetDecl; } 1618 const NamedDecl *getTargetDecl() const { return TargetDecl; } 1619 1620 /// \brief Get target nested name declaration. 1621 NestedNameSpecifier* getTargetNestedNameDecl() { 1622 return TargetNestedNameDecl; 1623 } 1624 1625 /// isTypeName - Return true if using decl had 'typename'. 1626 bool isTypeName() const { return IsTypeName; } 1627 1628 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 1629 SourceLocation L, SourceRange NNR, SourceLocation TargetNL, 1630 SourceLocation UL, NamedDecl* Target, 1631 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg); 1632 1633 static bool classof(const Decl *D) { 1634 return D->getKind() == Decl::Using; 1635 } 1636 static bool classof(const UsingDecl *D) { return true; } 1637}; 1638 1639/// StaticAssertDecl - Represents a C++0x static_assert declaration. 1640class StaticAssertDecl : public Decl { 1641 Expr *AssertExpr; 1642 StringLiteral *Message; 1643 1644 StaticAssertDecl(DeclContext *DC, SourceLocation L, 1645 Expr *assertexpr, StringLiteral *message) 1646 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 1647 1648public: 1649 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 1650 SourceLocation L, Expr *AssertExpr, 1651 StringLiteral *Message); 1652 1653 Expr *getAssertExpr() { return AssertExpr; } 1654 const Expr *getAssertExpr() const { return AssertExpr; } 1655 1656 StringLiteral *getMessage() { return Message; } 1657 const StringLiteral *getMessage() const { return Message; } 1658 1659 virtual ~StaticAssertDecl(); 1660 virtual void Destroy(ASTContext& C); 1661 1662 static bool classof(const Decl *D) { 1663 return D->getKind() == Decl::StaticAssert; 1664 } 1665 static bool classof(StaticAssertDecl *D) { return true; } 1666}; 1667 1668/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 1669/// into a diagnostic with <<. 1670const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1671 AccessSpecifier AS); 1672 1673} // end namespace clang 1674 1675#endif 1676