DeclCXX.h revision 65ec1fda479688d143fe2403242cd9c730c800a1
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 { return UserDeclaredConstructor; } 517 518 /// hasUserDeclaredCopyConstructor - Whether this class has a 519 /// user-declared copy constructor. When false, a copy constructor 520 /// will be implicitly declared. 521 bool hasUserDeclaredCopyConstructor() const { 522 return UserDeclaredCopyConstructor; 523 } 524 525 /// addedAssignmentOperator - Notify the class that another assignment 526 /// operator has been added. This routine helps maintain information about the 527 /// class based on which operators have been added. 528 void addedAssignmentOperator(ASTContext &Context, CXXMethodDecl *OpDecl); 529 530 /// hasUserDeclaredCopyAssignment - Whether this class has a 531 /// user-declared copy assignment operator. When false, a copy 532 /// assigment operator will be implicitly declared. 533 bool hasUserDeclaredCopyAssignment() const { 534 return UserDeclaredCopyAssignment; 535 } 536 537 /// hasUserDeclaredDestructor - Whether this class has a 538 /// user-declared destructor. When false, a destructor will be 539 /// implicitly declared. 540 bool hasUserDeclaredDestructor() const { return UserDeclaredDestructor; } 541 542 /// setUserDeclaredDestructor - Set whether this class has a 543 /// user-declared destructor. If not set by the time the class is 544 /// fully defined, a destructor will be implicitly declared. 545 void setUserDeclaredDestructor(bool UCD) { 546 UserDeclaredDestructor = UCD; 547 } 548 549 /// getConversions - Retrieve the overload set containing all of the 550 /// conversion functions in this class. 551 OverloadedFunctionDecl *getConversionFunctions() { 552 return &Conversions; 553 } 554 const OverloadedFunctionDecl *getConversionFunctions() const { 555 return &Conversions; 556 } 557 558 /// addConversionFunction - Add a new conversion function to the 559 /// list of conversion functions. 560 void addConversionFunction(ASTContext &Context, CXXConversionDecl *ConvDecl); 561 562 /// \brief Add a new conversion function template to the list of conversion 563 /// functions. 564 void addConversionFunction(ASTContext &Context, 565 FunctionTemplateDecl *ConvDecl); 566 567 /// isAggregate - Whether this class is an aggregate (C++ 568 /// [dcl.init.aggr]), which is a class with no user-declared 569 /// constructors, no private or protected non-static data members, 570 /// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1). 571 bool isAggregate() const { return Aggregate; } 572 573 /// setAggregate - Set whether this class is an aggregate (C++ 574 /// [dcl.init.aggr]). 575 void setAggregate(bool Agg) { Aggregate = Agg; } 576 577 /// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class 578 /// that is an aggregate that has no non-static non-POD data members, no 579 /// reference data members, no user-defined copy assignment operator and no 580 /// user-defined destructor. 581 bool isPOD() const { return PlainOldData; } 582 583 /// setPOD - Set whether this class is a POD-type (C++ [class]p4). 584 void setPOD(bool POD) { PlainOldData = POD; } 585 586 /// isEmpty - Whether this class is empty (C++0x [meta.unary.prop]), which 587 /// means it has a virtual function, virtual base, data member (other than 588 /// 0-width bit-field) or inherits from a non-empty class. Does NOT include 589 /// a check for union-ness. 590 bool isEmpty() const { return Empty; } 591 592 /// Set whether this class is empty (C++0x [meta.unary.prop]) 593 void setEmpty(bool Emp) { Empty = Emp; } 594 595 /// isPolymorphic - Whether this class is polymorphic (C++ [class.virtual]), 596 /// which means that the class contains or inherits a virtual function. 597 bool isPolymorphic() const { return Polymorphic; } 598 599 /// setPolymorphic - Set whether this class is polymorphic (C++ 600 /// [class.virtual]). 601 void setPolymorphic(bool Poly) { Polymorphic = Poly; } 602 603 /// isAbstract - Whether this class is abstract (C++ [class.abstract]), 604 /// which means that the class contains or inherits a pure virtual function. 605 bool isAbstract() const { return Abstract; } 606 607 /// setAbstract - Set whether this class is abstract (C++ [class.abstract]) 608 void setAbstract(bool Abs) { Abstract = Abs; } 609 610 // hasTrivialConstructor - Whether this class has a trivial constructor 611 // (C++ [class.ctor]p5) 612 bool hasTrivialConstructor() const { return HasTrivialConstructor; } 613 614 // setHasTrivialConstructor - Set whether this class has a trivial constructor 615 // (C++ [class.ctor]p5) 616 void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; } 617 618 // hasTrivialCopyConstructor - Whether this class has a trivial copy 619 // constructor (C++ [class.copy]p6) 620 bool hasTrivialCopyConstructor() const { return HasTrivialCopyConstructor; } 621 622 // setHasTrivialCopyConstructor - Set whether this class has a trivial 623 // copy constructor (C++ [class.copy]p6) 624 void setHasTrivialCopyConstructor(bool TC) { HasTrivialCopyConstructor = TC; } 625 626 // hasTrivialCopyAssignment - Whether this class has a trivial copy 627 // assignment operator (C++ [class.copy]p11) 628 bool hasTrivialCopyAssignment() const { return HasTrivialCopyAssignment; } 629 630 // setHasTrivialCopyAssignment - Set whether this class has a 631 // trivial copy assignment operator (C++ [class.copy]p11) 632 void setHasTrivialCopyAssignment(bool TC) { HasTrivialCopyAssignment = TC; } 633 634 // hasTrivialDestructor - Whether this class has a trivial destructor 635 // (C++ [class.dtor]p3) 636 bool hasTrivialDestructor() const { return HasTrivialDestructor; } 637 638 // setHasTrivialDestructor - Set whether this class has a trivial destructor 639 // (C++ [class.dtor]p3) 640 void setHasTrivialDestructor(bool TC) { HasTrivialDestructor = TC; } 641 642 /// \brief If this record is an instantiation of a member class, 643 /// retrieves the member class from which it was instantiated. 644 /// 645 /// This routine will return non-NULL for (non-templated) member 646 /// classes of class templates. For example, given: 647 /// 648 /// \code 649 /// template<typename T> 650 /// struct X { 651 /// struct A { }; 652 /// }; 653 /// \endcode 654 /// 655 /// The declaration for X<int>::A is a (non-templated) CXXRecordDecl 656 /// whose parent is the class template specialization X<int>. For 657 /// this declaration, getInstantiatedFromMemberClass() will return 658 /// the CXXRecordDecl X<T>::A. When a complete definition of 659 /// X<int>::A is required, it will be instantiated from the 660 /// declaration returned by getInstantiatedFromMemberClass(). 661 CXXRecordDecl *getInstantiatedFromMemberClass() const { 662 return TemplateOrInstantiation.dyn_cast<CXXRecordDecl*>(); 663 } 664 665 /// \brief Specify that this record is an instantiation of the 666 /// member class RD. 667 void setInstantiationOfMemberClass(CXXRecordDecl *RD) { 668 TemplateOrInstantiation = RD; 669 } 670 671 /// \brief Retrieves the class template that is described by this 672 /// class declaration. 673 /// 674 /// Every class template is represented as a ClassTemplateDecl and a 675 /// CXXRecordDecl. The former contains template properties (such as 676 /// the template parameter lists) while the latter contains the 677 /// actual description of the template's 678 /// contents. ClassTemplateDecl::getTemplatedDecl() retrieves the 679 /// CXXRecordDecl that from a ClassTemplateDecl, while 680 /// getDescribedClassTemplate() retrieves the ClassTemplateDecl from 681 /// a CXXRecordDecl. 682 ClassTemplateDecl *getDescribedClassTemplate() const { 683 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl*>(); 684 } 685 686 void setDescribedClassTemplate(ClassTemplateDecl *Template) { 687 TemplateOrInstantiation = Template; 688 } 689 690 /// getDefaultConstructor - Returns the default constructor for this class 691 CXXConstructorDecl *getDefaultConstructor(ASTContext &Context); 692 693 /// getDestructor - Returns the destructor decl for this class. 694 const CXXDestructorDecl *getDestructor(ASTContext &Context); 695 696 /// isLocalClass - If the class is a local class [class.local], returns 697 /// the enclosing function declaration. 698 const FunctionDecl *isLocalClass() const { 699 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(getDeclContext())) 700 return RD->isLocalClass(); 701 702 return dyn_cast<FunctionDecl>(getDeclContext()); 703 } 704 705 /// viewInheritance - Renders and displays an inheritance diagram 706 /// for this C++ class and all of its base classes (transitively) using 707 /// GraphViz. 708 void viewInheritance(ASTContext& Context) const; 709 710 static bool classof(const Decl *D) { 711 return D->getKind() == CXXRecord || 712 D->getKind() == ClassTemplateSpecialization || 713 D->getKind() == ClassTemplatePartialSpecialization; 714 } 715 static bool classof(const CXXRecordDecl *D) { return true; } 716 static bool classof(const ClassTemplateSpecializationDecl *D) { 717 return true; 718 } 719}; 720 721/// CXXMethodDecl - Represents a static or instance method of a 722/// struct/union/class. 723class CXXMethodDecl : public FunctionDecl { 724protected: 725 CXXMethodDecl(Kind DK, CXXRecordDecl *RD, SourceLocation L, 726 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 727 bool isStatic, bool isInline) 728 : FunctionDecl(DK, RD, L, N, T, DInfo, (isStatic ? Static : None), 729 isInline) {} 730 731public: 732 static CXXMethodDecl *Create(ASTContext &C, CXXRecordDecl *RD, 733 SourceLocation L, DeclarationName N, 734 QualType T, DeclaratorInfo *DInfo, 735 bool isStatic = false, 736 bool isInline = false); 737 738 bool isStatic() const { return getStorageClass() == Static; } 739 bool isInstance() const { return !isStatic(); } 740 741 bool isVirtual() const { 742 return isVirtualAsWritten() || 743 (begin_overridden_methods() != end_overridden_methods()); 744 } 745 746 /// 747 void addOverriddenMethod(const CXXMethodDecl *MD); 748 749 typedef const CXXMethodDecl ** method_iterator; 750 751 method_iterator begin_overridden_methods() const; 752 method_iterator end_overridden_methods() const; 753 754 /// getParent - Returns the parent of this method declaration, which 755 /// is the class in which this method is defined. 756 const CXXRecordDecl *getParent() const { 757 return cast<CXXRecordDecl>(FunctionDecl::getParent()); 758 } 759 760 /// getParent - Returns the parent of this method declaration, which 761 /// is the class in which this method is defined. 762 CXXRecordDecl *getParent() { 763 return const_cast<CXXRecordDecl *>( 764 cast<CXXRecordDecl>(FunctionDecl::getParent())); 765 } 766 767 /// getThisType - Returns the type of 'this' pointer. 768 /// Should only be called for instance methods. 769 QualType getThisType(ASTContext &C) const; 770 771 unsigned getTypeQualifiers() const { 772 return getType()->getAsFunctionProtoType()->getTypeQuals(); 773 } 774 775 // Implement isa/cast/dyncast/etc. 776 static bool classof(const Decl *D) { 777 return D->getKind() >= CXXMethod && D->getKind() <= CXXConversion; 778 } 779 static bool classof(const CXXMethodDecl *D) { return true; } 780}; 781 782/// CXXBaseOrMemberInitializer - Represents a C++ base or member 783/// initializer, which is part of a constructor initializer that 784/// initializes one non-static member variable or one base class. For 785/// example, in the following, both 'A(a)' and 'f(3.14159)' are member 786/// initializers: 787/// 788/// @code 789/// class A { }; 790/// class B : public A { 791/// float f; 792/// public: 793/// B(A& a) : A(a), f(3.14159) { } 794/// }; 795/// @endcode 796class CXXBaseOrMemberInitializer { 797 /// BaseOrMember - This points to the entity being initialized, 798 /// which is either a base class (a Type) or a non-static data 799 /// member. When the low bit is 1, it's a base 800 /// class; when the low bit is 0, it's a member. 801 uintptr_t BaseOrMember; 802 803 /// Args - The arguments used to initialize the base or member. 804 Stmt **Args; 805 unsigned NumArgs; 806 807 union { 808 /// CtorToCall - For a base or member needing a constructor for their 809 /// initialization, this is the constructor to call. 810 CXXConstructorDecl *CtorToCall; 811 812 /// AnonUnionMember - When 'BaseOrMember' is class's anonymous union 813 /// data member, this field holds the FieldDecl for the member of the 814 /// anonymous union being initialized. 815 /// @code 816 /// struct X { 817 /// X() : au_i1(123) {} 818 /// union { 819 /// int au_i1; 820 /// float au_f1; 821 /// }; 822 /// }; 823 /// @endcode 824 /// In above example, BaseOrMember holds the field decl. for anonymous union 825 /// and AnonUnionMember holds field decl for au_i1. 826 /// 827 FieldDecl *AnonUnionMember; 828 }; 829 830 /// IdLoc - Location of the id in ctor-initializer list. 831 SourceLocation IdLoc; 832 833public: 834 /// CXXBaseOrMemberInitializer - Creates a new base-class initializer. 835 explicit 836 CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs, 837 CXXConstructorDecl *C, 838 SourceLocation L); 839 840 /// CXXBaseOrMemberInitializer - Creates a new member initializer. 841 explicit 842 CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs, 843 CXXConstructorDecl *C, 844 SourceLocation L); 845 846 /// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer. 847 ~CXXBaseOrMemberInitializer(); 848 849 /// arg_iterator - Iterates through the member initialization 850 /// arguments. 851 typedef ExprIterator arg_iterator; 852 853 /// arg_const_iterator - Iterates through the member initialization 854 /// arguments. 855 typedef ConstExprIterator const_arg_iterator; 856 857 /// getBaseOrMember - get the generic 'member' representing either the field 858 /// or a base class. 859 void* getBaseOrMember() const { return reinterpret_cast<void*>(BaseOrMember); } 860 861 /// isBaseInitializer - Returns true when this initializer is 862 /// initializing a base class. 863 bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; } 864 865 /// isMemberInitializer - Returns true when this initializer is 866 /// initializing a non-static data member. 867 bool isMemberInitializer() const { return (BaseOrMember & 0x1) == 0; } 868 869 /// getBaseClass - If this is a base class initializer, returns the 870 /// type used to specify the initializer. The resulting type will be 871 /// a class type or a typedef of a class type. If this is not a base 872 /// class initializer, returns NULL. 873 Type *getBaseClass() { 874 if (isBaseInitializer()) 875 return reinterpret_cast<Type*>(BaseOrMember & ~0x01); 876 else 877 return 0; 878 } 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 const Type *getBaseClass() const { 885 if (isBaseInitializer()) 886 return reinterpret_cast<const Type*>(BaseOrMember & ~0x01); 887 else 888 return 0; 889 } 890 891 /// getMember - If this is a member initializer, returns the 892 /// declaration of the non-static data member being 893 /// initialized. Otherwise, returns NULL. 894 FieldDecl *getMember() { 895 if (isMemberInitializer()) 896 return reinterpret_cast<FieldDecl *>(BaseOrMember); 897 else 898 return 0; 899 } 900 901 void setMember(FieldDecl * anonUnionField) { 902 BaseOrMember = reinterpret_cast<uintptr_t>(anonUnionField); 903 } 904 905 FieldDecl *getAnonUnionMember() const { 906 return AnonUnionMember; 907 } 908 void setAnonUnionMember(FieldDecl *anonMember) { 909 AnonUnionMember = anonMember; 910 } 911 912 const CXXConstructorDecl *getConstructor() const { return CtorToCall; } 913 914 SourceLocation getSourceLocation() const { return IdLoc; } 915 916 /// arg_begin() - Retrieve an iterator to the first initializer argument. 917 arg_iterator arg_begin() { return Args; } 918 /// arg_begin() - Retrieve an iterator to the first initializer argument. 919 const_arg_iterator const_arg_begin() const { return Args; } 920 921 /// arg_end() - Retrieve an iterator past the last initializer argument. 922 arg_iterator arg_end() { return Args + NumArgs; } 923 /// arg_end() - Retrieve an iterator past the last initializer argument. 924 const_arg_iterator const_arg_end() const { return Args + NumArgs; } 925 926 /// getNumArgs - Determine the number of arguments used to 927 /// initialize the member or base. 928 unsigned getNumArgs() const { return NumArgs; } 929}; 930 931/// CXXConstructorDecl - Represents a C++ constructor within a 932/// class. For example: 933/// 934/// @code 935/// class X { 936/// public: 937/// explicit X(int); // represented by a CXXConstructorDecl. 938/// }; 939/// @endcode 940class CXXConstructorDecl : public CXXMethodDecl { 941 /// Explicit - Whether this constructor is explicit. 942 bool Explicit : 1; 943 944 /// ImplicitlyDefined - Whether this constructor was implicitly 945 /// defined by the compiler. When false, the constructor was defined 946 /// by the user. In C++03, this flag will have the same value as 947 /// Implicit. In C++0x, however, a constructor that is 948 /// explicitly defaulted (i.e., defined with " = default") will have 949 /// @c !Implicit && ImplicitlyDefined. 950 bool ImplicitlyDefined : 1; 951 952 /// Support for base and member initializers. 953 /// BaseOrMemberInitializers - The arguments used to initialize the base 954 /// or member. 955 CXXBaseOrMemberInitializer **BaseOrMemberInitializers; 956 unsigned NumBaseOrMemberInitializers; 957 958 CXXConstructorDecl(CXXRecordDecl *RD, SourceLocation L, 959 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 960 bool isExplicit, bool isInline, bool isImplicitlyDeclared) 961 : CXXMethodDecl(CXXConstructor, RD, L, N, T, DInfo, false, isInline), 962 Explicit(isExplicit), ImplicitlyDefined(false), 963 BaseOrMemberInitializers(0), NumBaseOrMemberInitializers(0) { 964 setImplicit(isImplicitlyDeclared); 965 } 966 virtual void Destroy(ASTContext& C); 967 968public: 969 static CXXConstructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 970 SourceLocation L, DeclarationName N, 971 QualType T, DeclaratorInfo *DInfo, 972 bool isExplicit, 973 bool isInline, bool isImplicitlyDeclared); 974 975 /// isExplicit - Whether this constructor was marked "explicit" or not. 976 bool isExplicit() const { return Explicit; } 977 978 /// isImplicitlyDefined - Whether this constructor was implicitly 979 /// defined. If false, then this constructor was defined by the 980 /// user. This operation can only be invoked if the constructor has 981 /// already been defined. 982 bool isImplicitlyDefined(ASTContext &C) const { 983 assert(isThisDeclarationADefinition() && 984 "Can only get the implicit-definition flag once the " 985 "constructor has been defined"); 986 return ImplicitlyDefined; 987 } 988 989 /// setImplicitlyDefined - Set whether this constructor was 990 /// implicitly defined or not. 991 void setImplicitlyDefined(bool ID) { 992 assert(isThisDeclarationADefinition() && 993 "Can only set the implicit-definition flag once the constructor " 994 "has been defined"); 995 ImplicitlyDefined = ID; 996 } 997 998 /// init_iterator - Iterates through the member/base initializer list. 999 typedef CXXBaseOrMemberInitializer **init_iterator; 1000 1001 /// init_const_iterator - Iterates through the memberbase initializer list. 1002 typedef CXXBaseOrMemberInitializer * const * init_const_iterator; 1003 1004 /// init_begin() - Retrieve an iterator to the first initializer. 1005 init_iterator init_begin() { return BaseOrMemberInitializers; } 1006 /// begin() - Retrieve an iterator to the first initializer. 1007 init_const_iterator init_begin() const { return BaseOrMemberInitializers; } 1008 1009 /// init_end() - Retrieve an iterator past the last initializer. 1010 init_iterator init_end() { 1011 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1012 } 1013 /// end() - Retrieve an iterator past the last initializer. 1014 init_const_iterator init_end() const { 1015 return BaseOrMemberInitializers + NumBaseOrMemberInitializers; 1016 } 1017 1018 /// getNumArgs - Determine the number of arguments used to 1019 /// initialize the member or base. 1020 unsigned getNumBaseOrMemberInitializers() const { 1021 return NumBaseOrMemberInitializers; 1022 } 1023 1024 void setBaseOrMemberInitializers(ASTContext &C, 1025 CXXBaseOrMemberInitializer **Initializers, 1026 unsigned NumInitializers, 1027 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases, 1028 llvm::SmallVectorImpl<FieldDecl *>&Members); 1029 1030 /// isDefaultConstructor - Whether this constructor is a default 1031 /// constructor (C++ [class.ctor]p5), which can be used to 1032 /// default-initialize a class of this type. 1033 bool isDefaultConstructor() const; 1034 1035 /// isCopyConstructor - Whether this constructor is a copy 1036 /// constructor (C++ [class.copy]p2, which can be used to copy the 1037 /// class. @p TypeQuals will be set to the qualifiers on the 1038 /// argument type. For example, @p TypeQuals would be set to @c 1039 /// QualType::Const for the following copy constructor: 1040 /// 1041 /// @code 1042 /// class X { 1043 /// public: 1044 /// X(const X&); 1045 /// }; 1046 /// @endcode 1047 bool isCopyConstructor(ASTContext &Context, unsigned &TypeQuals) const; 1048 1049 /// isCopyConstructor - Whether this constructor is a copy 1050 /// constructor (C++ [class.copy]p2, which can be used to copy the 1051 /// class. 1052 bool isCopyConstructor(ASTContext &Context) const { 1053 unsigned TypeQuals = 0; 1054 return isCopyConstructor(Context, TypeQuals); 1055 } 1056 1057 /// isConvertingConstructor - Whether this constructor is a 1058 /// converting constructor (C++ [class.conv.ctor]), which can be 1059 /// used for user-defined conversions. 1060 bool isConvertingConstructor() const; 1061 1062 // Implement isa/cast/dyncast/etc. 1063 static bool classof(const Decl *D) { 1064 return D->getKind() == CXXConstructor; 1065 } 1066 static bool classof(const CXXConstructorDecl *D) { return true; } 1067}; 1068 1069/// CXXDestructorDecl - Represents a C++ destructor within a 1070/// class. For example: 1071/// 1072/// @code 1073/// class X { 1074/// public: 1075/// ~X(); // represented by a CXXDestructorDecl. 1076/// }; 1077/// @endcode 1078class CXXDestructorDecl : public CXXMethodDecl { 1079 enum KindOfObjectToDestroy { 1080 VBASE = 0x1, 1081 DRCTNONVBASE = 0x2, 1082 ANYBASE = 0x3 1083 }; 1084 1085 /// ImplicitlyDefined - Whether this destructor was implicitly 1086 /// defined by the compiler. When false, the destructor was defined 1087 /// by the user. In C++03, this flag will have the same value as 1088 /// Implicit. In C++0x, however, a destructor that is 1089 /// explicitly defaulted (i.e., defined with " = default") will have 1090 /// @c !Implicit && ImplicitlyDefined. 1091 bool ImplicitlyDefined : 1; 1092 1093 /// Support for base and member destruction. 1094 /// BaseOrMemberDestructions - The arguments used to destruct the base 1095 /// or member. Each uintptr_t value represents one of base classes (either 1096 /// virtual or direct non-virtual base), or non-static data member 1097 /// to be destroyed. The low two bits encode the kind of object 1098 /// being destroyed. 1099 uintptr_t *BaseOrMemberDestructions; 1100 unsigned NumBaseOrMemberDestructions; 1101 1102 CXXDestructorDecl(CXXRecordDecl *RD, SourceLocation L, 1103 DeclarationName N, QualType T, 1104 bool isInline, bool isImplicitlyDeclared) 1105 : CXXMethodDecl(CXXDestructor, RD, L, N, T, /*DInfo=*/0, false, isInline), 1106 ImplicitlyDefined(false), 1107 BaseOrMemberDestructions(0), NumBaseOrMemberDestructions(0) { 1108 setImplicit(isImplicitlyDeclared); 1109 } 1110 virtual void Destroy(ASTContext& C); 1111 1112public: 1113 static CXXDestructorDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1114 SourceLocation L, DeclarationName N, 1115 QualType T, bool isInline, 1116 bool isImplicitlyDeclared); 1117 1118 /// isImplicitlyDefined - Whether this destructor was implicitly 1119 /// defined. If false, then this destructor was defined by the 1120 /// user. This operation can only be invoked if the destructor has 1121 /// already been defined. 1122 bool isImplicitlyDefined() const { 1123 assert(isThisDeclarationADefinition() && 1124 "Can only get the implicit-definition flag once the destructor has been defined"); 1125 return ImplicitlyDefined; 1126 } 1127 1128 /// setImplicitlyDefined - Set whether this destructor was 1129 /// implicitly defined or not. 1130 void setImplicitlyDefined(bool ID) { 1131 assert(isThisDeclarationADefinition() && 1132 "Can only set the implicit-definition flag once the destructor has been defined"); 1133 ImplicitlyDefined = ID; 1134 } 1135 1136 /// destr_iterator - Iterates through the member/base destruction list. 1137 1138 /// destr_const_iterator - Iterates through the member/base destruction list. 1139 typedef uintptr_t const destr_const_iterator; 1140 1141 /// destr_begin() - Retrieve an iterator to the first destructed member/base. 1142 uintptr_t* destr_begin() { 1143 return BaseOrMemberDestructions; 1144 } 1145 /// destr_begin() - Retrieve an iterator to the first destructed member/base. 1146 uintptr_t* destr_begin() const { 1147 return BaseOrMemberDestructions; 1148 } 1149 1150 /// destr_end() - Retrieve an iterator past the last destructed member/base. 1151 uintptr_t* destr_end() { 1152 return BaseOrMemberDestructions + NumBaseOrMemberDestructions; 1153 } 1154 /// destr_end() - Retrieve an iterator past the last destructed member/base. 1155 uintptr_t* destr_end() const { 1156 return BaseOrMemberDestructions + NumBaseOrMemberDestructions; 1157 } 1158 1159 /// getNumBaseOrMemberDestructions - Number of base and non-static members 1160 /// to destroy. 1161 unsigned getNumBaseOrMemberDestructions() const { 1162 return NumBaseOrMemberDestructions; 1163 } 1164 1165 /// getBaseOrMember - get the generic 'member' representing either the field 1166 /// or a base class. 1167 uintptr_t* getBaseOrMemberToDestroy() const { 1168 return BaseOrMemberDestructions; 1169 } 1170 1171 /// isVbaseToDestroy - returns true, if object is virtual base. 1172 bool isVbaseToDestroy(uintptr_t Vbase) const { 1173 return (Vbase & VBASE) != 0; 1174 } 1175 /// isDirectNonVBaseToDestroy - returns true, if object is direct non-virtual 1176 /// base. 1177 bool isDirectNonVBaseToDestroy(uintptr_t DrctNonVbase) const { 1178 return (DrctNonVbase & DRCTNONVBASE) != 0; 1179 } 1180 /// isAnyBaseToDestroy - returns true, if object is any base (virtual or 1181 /// direct non-virtual) 1182 bool isAnyBaseToDestroy(uintptr_t AnyBase) const { 1183 return (AnyBase & ANYBASE) != 0; 1184 } 1185 /// isMemberToDestroy - returns true if object is a non-static data member. 1186 bool isMemberToDestroy(uintptr_t Member) const { 1187 return (Member & ANYBASE) == 0; 1188 } 1189 /// getAnyBaseClassToDestroy - Get the type for the given base class object. 1190 Type *getAnyBaseClassToDestroy(uintptr_t Base) const { 1191 if (isAnyBaseToDestroy(Base)) 1192 return reinterpret_cast<Type*>(Base & ~0x03); 1193 return 0; 1194 } 1195 /// getMemberToDestroy - Get the member for the given object. 1196 FieldDecl *getMemberToDestroy(uintptr_t Member) const { 1197 if (isMemberToDestroy(Member)) 1198 return reinterpret_cast<FieldDecl *>(Member); 1199 return 0; 1200 } 1201 /// getVbaseClassToDestroy - Get the virtual base. 1202 Type *getVbaseClassToDestroy(uintptr_t Vbase) const { 1203 if (isVbaseToDestroy(Vbase)) 1204 return reinterpret_cast<Type*>(Vbase & ~0x01); 1205 return 0; 1206 } 1207 /// getDirectNonVBaseClassToDestroy - Get the virtual base. 1208 Type *getDirectNonVBaseClassToDestroy(uintptr_t Base) const { 1209 if (isDirectNonVBaseToDestroy(Base)) 1210 return reinterpret_cast<Type*>(Base & ~0x02); 1211 return 0; 1212 } 1213 1214 /// computeBaseOrMembersToDestroy - Compute information in current 1215 /// destructor decl's AST of bases and non-static data members which will be 1216 /// implicitly destroyed. We are storing the destruction in the order that 1217 /// they should occur (which is the reverse of construction order). 1218 void computeBaseOrMembersToDestroy(ASTContext &C); 1219 1220 // Implement isa/cast/dyncast/etc. 1221 static bool classof(const Decl *D) { 1222 return D->getKind() == CXXDestructor; 1223 } 1224 static bool classof(const CXXDestructorDecl *D) { return true; } 1225}; 1226 1227/// CXXConversionDecl - Represents a C++ conversion function within a 1228/// class. For example: 1229/// 1230/// @code 1231/// class X { 1232/// public: 1233/// operator bool(); 1234/// }; 1235/// @endcode 1236class CXXConversionDecl : public CXXMethodDecl { 1237 /// Explicit - Whether this conversion function is marked 1238 /// "explicit", meaning that it can only be applied when the user 1239 /// explicitly wrote a cast. This is a C++0x feature. 1240 bool Explicit : 1; 1241 1242 CXXConversionDecl(CXXRecordDecl *RD, SourceLocation L, 1243 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 1244 bool isInline, bool isExplicit) 1245 : CXXMethodDecl(CXXConversion, RD, L, N, T, DInfo, false, isInline), 1246 Explicit(isExplicit) { } 1247 1248public: 1249 static CXXConversionDecl *Create(ASTContext &C, CXXRecordDecl *RD, 1250 SourceLocation L, DeclarationName N, 1251 QualType T, DeclaratorInfo *DInfo, 1252 bool isInline, bool isExplicit); 1253 1254 /// isExplicit - Whether this is an explicit conversion operator 1255 /// (C++0x only). Explicit conversion operators are only considered 1256 /// when the user has explicitly written a cast. 1257 bool isExplicit() const { return Explicit; } 1258 1259 /// getConversionType - Returns the type that this conversion 1260 /// function is converting to. 1261 QualType getConversionType() const { 1262 return getType()->getAsFunctionType()->getResultType(); 1263 } 1264 1265 // Implement isa/cast/dyncast/etc. 1266 static bool classof(const Decl *D) { 1267 return D->getKind() == CXXConversion; 1268 } 1269 static bool classof(const CXXConversionDecl *D) { return true; } 1270}; 1271 1272/// FriendFunctionDecl - Represents the declaration (and possibly 1273/// the definition) of a friend function. For example: 1274/// 1275/// @code 1276/// class A { 1277/// friend int foo(int); 1278/// }; 1279/// @endcode 1280class FriendFunctionDecl : public FunctionDecl { 1281 // Location of the 'friend' specifier. 1282 const SourceLocation FriendLoc; 1283 1284 FriendFunctionDecl(DeclContext *DC, SourceLocation L, 1285 DeclarationName N, QualType T, DeclaratorInfo *DInfo, 1286 bool isInline, SourceLocation FriendL) 1287 : FunctionDecl(FriendFunction, DC, L, N, T, DInfo, None, isInline), 1288 FriendLoc(FriendL) 1289 {} 1290 1291public: 1292 static FriendFunctionDecl *Create(ASTContext &C, DeclContext *DC, 1293 SourceLocation L, DeclarationName N, 1294 QualType T, DeclaratorInfo *DInfo, 1295 bool isInline, SourceLocation FriendL); 1296 1297 SourceLocation getFriendLoc() const { 1298 return FriendLoc; 1299 } 1300 1301 // Implement isa/cast/dyncast/etc. 1302 static bool classof(const Decl *D) { 1303 return D->getKind() == FriendFunction; 1304 } 1305 static bool classof(const FriendFunctionDecl *D) { return true; } 1306}; 1307 1308/// FriendClassDecl - Represents the declaration of a friend class. 1309/// For example: 1310/// 1311/// @code 1312/// class X { 1313/// friend class Y; 1314/// }; 1315/// @endcode 1316class FriendClassDecl : public Decl { 1317 // The friended type. In C++0x, this can be an arbitrary type, 1318 // which we simply ignore if it's not a record type. 1319 QualType FriendType; 1320 1321 // Location of the 'friend' specifier. 1322 SourceLocation FriendLoc; 1323 1324 FriendClassDecl(DeclContext *DC, SourceLocation L, 1325 QualType T, SourceLocation FriendL) 1326 : Decl(FriendClass, DC, L), 1327 FriendType(T), 1328 FriendLoc(FriendL) 1329 {} 1330 1331public: 1332 static FriendClassDecl *Create(ASTContext &C, DeclContext *DC, 1333 SourceLocation L, QualType T, 1334 SourceLocation FriendL); 1335 1336 QualType getFriendType() const { 1337 return FriendType; 1338 } 1339 1340 SourceLocation getFriendLoc() const { 1341 return FriendLoc; 1342 } 1343 1344 // Implement isa/cast/dyncast/etc. 1345 static bool classof(const Decl *D) { 1346 return D->getKind() == FriendClass; 1347 } 1348 static bool classof(const FriendClassDecl *D) { return true; } 1349}; 1350 1351/// LinkageSpecDecl - This represents a linkage specification. For example: 1352/// extern "C" void foo(); 1353/// 1354class LinkageSpecDecl : public Decl, public DeclContext { 1355public: 1356 /// LanguageIDs - Used to represent the language in a linkage 1357 /// specification. The values are part of the serialization abi for 1358 /// ASTs and cannot be changed without altering that abi. To help 1359 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 1360 /// from the dwarf standard. 1361 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 1362 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 1363private: 1364 /// Language - The language for this linkage specification. 1365 LanguageIDs Language; 1366 1367 /// HadBraces - Whether this linkage specification had curly braces or not. 1368 bool HadBraces : 1; 1369 1370 LinkageSpecDecl(DeclContext *DC, SourceLocation L, LanguageIDs lang, 1371 bool Braces) 1372 : Decl(LinkageSpec, DC, L), 1373 DeclContext(LinkageSpec), Language(lang), HadBraces(Braces) { } 1374 1375public: 1376 static LinkageSpecDecl *Create(ASTContext &C, DeclContext *DC, 1377 SourceLocation L, LanguageIDs Lang, 1378 bool Braces); 1379 1380 LanguageIDs getLanguage() const { return Language; } 1381 1382 /// hasBraces - Determines whether this linkage specification had 1383 /// braces in its syntactic form. 1384 bool hasBraces() const { return HadBraces; } 1385 1386 static bool classof(const Decl *D) { 1387 return D->getKind() == LinkageSpec; 1388 } 1389 static bool classof(const LinkageSpecDecl *D) { return true; } 1390 static DeclContext *castToDeclContext(const LinkageSpecDecl *D) { 1391 return static_cast<DeclContext *>(const_cast<LinkageSpecDecl*>(D)); 1392 } 1393 static LinkageSpecDecl *castFromDeclContext(const DeclContext *DC) { 1394 return static_cast<LinkageSpecDecl *>(const_cast<DeclContext*>(DC)); 1395 } 1396}; 1397 1398/// UsingDirectiveDecl - Represents C++ using-directive. For example: 1399/// 1400/// using namespace std; 1401/// 1402// NB: UsingDirectiveDecl should be Decl not NamedDecl, but we provide 1403// artificial name, for all using-directives in order to store 1404// them in DeclContext effectively. 1405class UsingDirectiveDecl : public NamedDecl { 1406 1407 /// SourceLocation - Location of 'namespace' token. 1408 SourceLocation NamespaceLoc; 1409 1410 /// \brief The source range that covers the nested-name-specifier 1411 /// preceding the namespace name. 1412 SourceRange QualifierRange; 1413 1414 /// \brief The nested-name-specifier that precedes the namespace 1415 /// name, if any. 1416 NestedNameSpecifier *Qualifier; 1417 1418 /// IdentLoc - Location of nominated namespace-name identifier. 1419 // FIXME: We don't store location of scope specifier. 1420 SourceLocation IdentLoc; 1421 1422 /// NominatedNamespace - Namespace nominated by using-directive. 1423 NamespaceDecl *NominatedNamespace; 1424 1425 /// Enclosing context containing both using-directive and nomintated 1426 /// namespace. 1427 DeclContext *CommonAncestor; 1428 1429 /// getUsingDirectiveName - Returns special DeclarationName used by 1430 /// using-directives. This is only used by DeclContext for storing 1431 /// UsingDirectiveDecls in its lookup structure. 1432 static DeclarationName getName() { 1433 return DeclarationName::getUsingDirectiveName(); 1434 } 1435 1436 UsingDirectiveDecl(DeclContext *DC, SourceLocation L, 1437 SourceLocation NamespcLoc, 1438 SourceRange QualifierRange, 1439 NestedNameSpecifier *Qualifier, 1440 SourceLocation IdentLoc, 1441 NamespaceDecl *Nominated, 1442 DeclContext *CommonAncestor) 1443 : NamedDecl(Decl::UsingDirective, DC, L, getName()), 1444 NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange), 1445 Qualifier(Qualifier), IdentLoc(IdentLoc), 1446 NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0), 1447 CommonAncestor(CommonAncestor) { 1448 } 1449 1450public: 1451 /// \brief Retrieve the source range of the nested-name-specifier 1452 /// that qualifiers the namespace name. 1453 SourceRange getQualifierRange() const { return QualifierRange; } 1454 1455 /// \brief Retrieve the nested-name-specifier that qualifies the 1456 /// name of the namespace. 1457 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1458 1459 /// getNominatedNamespace - Returns namespace nominated by using-directive. 1460 NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; } 1461 1462 const NamespaceDecl *getNominatedNamespace() const { 1463 return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace(); 1464 } 1465 1466 /// getCommonAncestor - returns common ancestor context of using-directive, 1467 /// and nominated by it namespace. 1468 DeclContext *getCommonAncestor() { return CommonAncestor; } 1469 const DeclContext *getCommonAncestor() const { return CommonAncestor; } 1470 1471 /// getNamespaceKeyLocation - Returns location of namespace keyword. 1472 SourceLocation getNamespaceKeyLocation() const { return NamespaceLoc; } 1473 1474 /// getIdentLocation - Returns location of identifier. 1475 SourceLocation getIdentLocation() const { return IdentLoc; } 1476 1477 static UsingDirectiveDecl *Create(ASTContext &C, DeclContext *DC, 1478 SourceLocation L, 1479 SourceLocation NamespaceLoc, 1480 SourceRange QualifierRange, 1481 NestedNameSpecifier *Qualifier, 1482 SourceLocation IdentLoc, 1483 NamespaceDecl *Nominated, 1484 DeclContext *CommonAncestor); 1485 1486 static bool classof(const Decl *D) { 1487 return D->getKind() == Decl::UsingDirective; 1488 } 1489 static bool classof(const UsingDirectiveDecl *D) { return true; } 1490 1491 // Friend for getUsingDirectiveName. 1492 friend class DeclContext; 1493}; 1494 1495/// NamespaceAliasDecl - Represents a C++ namespace alias. For example: 1496/// 1497/// @code 1498/// namespace Foo = Bar; 1499/// @endcode 1500class NamespaceAliasDecl : public NamedDecl { 1501 SourceLocation AliasLoc; 1502 1503 /// \brief The source range that covers the nested-name-specifier 1504 /// preceding the namespace name. 1505 SourceRange QualifierRange; 1506 1507 /// \brief The nested-name-specifier that precedes the namespace 1508 /// name, if any. 1509 NestedNameSpecifier *Qualifier; 1510 1511 /// IdentLoc - Location of namespace identifier. 1512 SourceLocation IdentLoc; 1513 1514 /// Namespace - The Decl that this alias points to. Can either be a 1515 /// NamespaceDecl or a NamespaceAliasDecl. 1516 NamedDecl *Namespace; 1517 1518 NamespaceAliasDecl(DeclContext *DC, SourceLocation L, 1519 SourceLocation AliasLoc, IdentifierInfo *Alias, 1520 SourceRange QualifierRange, 1521 NestedNameSpecifier *Qualifier, 1522 SourceLocation IdentLoc, NamedDecl *Namespace) 1523 : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), 1524 QualifierRange(QualifierRange), Qualifier(Qualifier), 1525 IdentLoc(IdentLoc), Namespace(Namespace) { } 1526 1527public: 1528 /// \brief Retrieve the source range of the nested-name-specifier 1529 /// that qualifiers the namespace name. 1530 SourceRange getQualifierRange() const { return QualifierRange; } 1531 1532 /// \brief Retrieve the nested-name-specifier that qualifies the 1533 /// name of the namespace. 1534 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1535 1536 NamespaceDecl *getNamespace() { 1537 if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(Namespace)) 1538 return AD->getNamespace(); 1539 1540 return cast<NamespaceDecl>(Namespace); 1541 } 1542 1543 const NamespaceDecl *getNamespace() const { 1544 return const_cast<NamespaceAliasDecl*>(this)->getNamespace(); 1545 } 1546 1547 /// \brief Retrieve the namespace that this alias refers to, which 1548 /// may either be a NamespaceDecl or a NamespaceAliasDecl. 1549 NamedDecl *getAliasedNamespace() const { return Namespace; } 1550 1551 static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, 1552 SourceLocation L, SourceLocation AliasLoc, 1553 IdentifierInfo *Alias, 1554 SourceRange QualifierRange, 1555 NestedNameSpecifier *Qualifier, 1556 SourceLocation IdentLoc, 1557 NamedDecl *Namespace); 1558 1559 static bool classof(const Decl *D) { 1560 return D->getKind() == Decl::NamespaceAlias; 1561 } 1562 static bool classof(const NamespaceAliasDecl *D) { return true; } 1563}; 1564 1565/// UsingDecl - Represents a C++ using-declaration. For example: 1566/// using someNameSpace::someIdentifier; 1567class UsingDecl : public NamedDecl { 1568 1569 /// \brief The source range that covers the nested-name-specifier 1570 /// preceding the declaration name. 1571 SourceRange NestedNameRange; 1572 /// \brief The source location of the target declaration name. 1573 SourceLocation TargetNameLocation; 1574 /// \brief The source location of the "using" location itself. 1575 SourceLocation UsingLocation; 1576 /// \brief Target declaration. 1577 NamedDecl* TargetDecl; 1578 /// \brief Target declaration. 1579 NestedNameSpecifier* TargetNestedNameDecl; 1580 1581 // Had 'typename' keyword. 1582 bool IsTypeName; 1583 1584 UsingDecl(DeclContext *DC, SourceLocation L, SourceRange NNR, 1585 SourceLocation TargetNL, SourceLocation UL, NamedDecl* Target, 1586 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) 1587 : NamedDecl(Decl::Using, DC, L, Target->getDeclName()), 1588 NestedNameRange(NNR), TargetNameLocation(TargetNL), 1589 UsingLocation(UL), TargetDecl(Target), 1590 TargetNestedNameDecl(TargetNNS), IsTypeName(IsTypeNameArg) { 1591 this->IdentifierNamespace = TargetDecl->getIdentifierNamespace(); 1592 } 1593 1594public: 1595 /// \brief Returns the source range that covers the nested-name-specifier 1596 /// preceding the namespace name. 1597 SourceRange getNestedNameRange() { return NestedNameRange; } 1598 1599 /// \brief Returns the source location of the target declaration name. 1600 SourceLocation getTargetNameLocation() { return TargetNameLocation; } 1601 1602 /// \brief Returns the source location of the "using" location itself. 1603 SourceLocation getUsingLocation() { return UsingLocation; } 1604 1605 /// \brief getTargetDecl - Returns target specified by using-decl. 1606 NamedDecl *getTargetDecl() { return TargetDecl; } 1607 const NamedDecl *getTargetDecl() const { return TargetDecl; } 1608 1609 /// \brief Get target nested name declaration. 1610 NestedNameSpecifier* getTargetNestedNameDecl() { 1611 return TargetNestedNameDecl; 1612 } 1613 1614 /// isTypeName - Return true if using decl had 'typename'. 1615 bool isTypeName() const { return IsTypeName; } 1616 1617 static UsingDecl *Create(ASTContext &C, DeclContext *DC, 1618 SourceLocation L, SourceRange NNR, SourceLocation TargetNL, 1619 SourceLocation UL, NamedDecl* Target, 1620 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg); 1621 1622 static bool classof(const Decl *D) { 1623 return D->getKind() == Decl::Using; 1624 } 1625 static bool classof(const UsingDecl *D) { return true; } 1626}; 1627 1628/// StaticAssertDecl - Represents a C++0x static_assert declaration. 1629class StaticAssertDecl : public Decl { 1630 Expr *AssertExpr; 1631 StringLiteral *Message; 1632 1633 StaticAssertDecl(DeclContext *DC, SourceLocation L, 1634 Expr *assertexpr, StringLiteral *message) 1635 : Decl(StaticAssert, DC, L), AssertExpr(assertexpr), Message(message) { } 1636 1637public: 1638 static StaticAssertDecl *Create(ASTContext &C, DeclContext *DC, 1639 SourceLocation L, Expr *AssertExpr, 1640 StringLiteral *Message); 1641 1642 Expr *getAssertExpr() { return AssertExpr; } 1643 const Expr *getAssertExpr() const { return AssertExpr; } 1644 1645 StringLiteral *getMessage() { return Message; } 1646 const StringLiteral *getMessage() const { return Message; } 1647 1648 virtual ~StaticAssertDecl(); 1649 virtual void Destroy(ASTContext& C); 1650 1651 static bool classof(const Decl *D) { 1652 return D->getKind() == Decl::StaticAssert; 1653 } 1654 static bool classof(StaticAssertDecl *D) { return true; } 1655}; 1656 1657/// Insertion operator for diagnostics. This allows sending AccessSpecifier's 1658/// into a diagnostic with <<. 1659const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1660 AccessSpecifier AS); 1661 1662} // end namespace clang 1663 1664#endif 1665