DeclTemplate.h revision 6982bf4d77bc57a85ee173b631729fce673f16ef
1//===-- DeclTemplate.h - Classes for representing C++ templates -*- 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/// \file 11/// \brief Defines the C++ template declaration subclasses. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_AST_DECLTEMPLATE_H 16#define LLVM_CLANG_AST_DECLTEMPLATE_H 17 18#include "clang/AST/DeclCXX.h" 19#include "clang/AST/Redeclarable.h" 20#include "clang/AST/TemplateBase.h" 21#include "llvm/ADT/PointerUnion.h" 22#include "llvm/Support/Compiler.h" 23#include <limits> 24 25namespace clang { 26 27class TemplateParameterList; 28class TemplateDecl; 29class RedeclarableTemplateDecl; 30class FunctionTemplateDecl; 31class ClassTemplateDecl; 32class ClassTemplatePartialSpecializationDecl; 33class TemplateTypeParmDecl; 34class NonTypeTemplateParmDecl; 35class TemplateTemplateParmDecl; 36class TypeAliasTemplateDecl; 37 38/// \brief Stores a template parameter of any kind. 39typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*, 40 TemplateTemplateParmDecl*> TemplateParameter; 41 42/// \brief Stores a list of template parameters for a TemplateDecl and its 43/// derived classes. 44class TemplateParameterList { 45 /// The location of the 'template' keyword. 46 SourceLocation TemplateLoc; 47 48 /// The locations of the '<' and '>' angle brackets. 49 SourceLocation LAngleLoc, RAngleLoc; 50 51 /// The number of template parameters in this template 52 /// parameter list. 53 unsigned NumParams : 31; 54 55 /// Whether this template parameter list contains an unexpanded parameter 56 /// pack. 57 unsigned ContainsUnexpandedParameterPack : 1; 58 59protected: 60 TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc, 61 NamedDecl **Params, unsigned NumParams, 62 SourceLocation RAngleLoc); 63 64public: 65 static TemplateParameterList *Create(const ASTContext &C, 66 SourceLocation TemplateLoc, 67 SourceLocation LAngleLoc, 68 NamedDecl **Params, 69 unsigned NumParams, 70 SourceLocation RAngleLoc); 71 72 /// \brief Iterates through the template parameters in this list. 73 typedef NamedDecl** iterator; 74 75 /// \brief Iterates through the template parameters in this list. 76 typedef NamedDecl* const* const_iterator; 77 78 iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); } 79 const_iterator begin() const { 80 return reinterpret_cast<NamedDecl * const *>(this + 1); 81 } 82 iterator end() { return begin() + NumParams; } 83 const_iterator end() const { return begin() + NumParams; } 84 85 unsigned size() const { return NumParams; } 86 87 llvm::ArrayRef<NamedDecl*> asArray() { 88 return llvm::ArrayRef<NamedDecl*>(begin(), size()); 89 } 90 llvm::ArrayRef<const NamedDecl*> asArray() const { 91 return llvm::ArrayRef<const NamedDecl*>(begin(), size()); 92 } 93 94 NamedDecl* getParam(unsigned Idx) { 95 assert(Idx < size() && "Template parameter index out-of-range"); 96 return begin()[Idx]; 97 } 98 99 const NamedDecl* getParam(unsigned Idx) const { 100 assert(Idx < size() && "Template parameter index out-of-range"); 101 return begin()[Idx]; 102 } 103 104 /// \brief Returns the minimum number of arguments needed to form a 105 /// template specialization. 106 /// 107 /// This may be fewer than the number of template parameters, if some of 108 /// the parameters have default arguments or if there is a parameter pack. 109 unsigned getMinRequiredArguments() const; 110 111 /// \brief Get the depth of this template parameter list in the set of 112 /// template parameter lists. 113 /// 114 /// The first template parameter list in a declaration will have depth 0, 115 /// the second template parameter list will have depth 1, etc. 116 unsigned getDepth() const; 117 118 /// \brief Determine whether this template parameter list contains an 119 /// unexpanded parameter pack. 120 bool containsUnexpandedParameterPack() const { 121 return ContainsUnexpandedParameterPack; 122 } 123 124 SourceLocation getTemplateLoc() const { return TemplateLoc; } 125 SourceLocation getLAngleLoc() const { return LAngleLoc; } 126 SourceLocation getRAngleLoc() const { return RAngleLoc; } 127 128 SourceRange getSourceRange() const LLVM_READONLY { 129 return SourceRange(TemplateLoc, RAngleLoc); 130 } 131}; 132 133/// \brief Stores a list of template parameters for a TemplateDecl and its 134/// derived classes. Suitable for creating on the stack. 135template<size_t N> 136class FixedSizeTemplateParameterList : public TemplateParameterList { 137 NamedDecl *Params[N]; 138 139public: 140 FixedSizeTemplateParameterList(SourceLocation TemplateLoc, 141 SourceLocation LAngleLoc, 142 NamedDecl **Params, SourceLocation RAngleLoc) : 143 TemplateParameterList(TemplateLoc, LAngleLoc, Params, N, RAngleLoc) { 144 } 145}; 146 147/// \brief A template argument list. 148class TemplateArgumentList { 149 /// \brief The template argument list. 150 /// 151 /// The integer value will be non-zero to indicate that this 152 /// template argument list does own the pointer. 153 llvm::PointerIntPair<const TemplateArgument *, 1> Arguments; 154 155 /// \brief The number of template arguments in this template 156 /// argument list. 157 unsigned NumArguments; 158 159 TemplateArgumentList(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION; 160 void operator=(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION; 161 162 TemplateArgumentList(const TemplateArgument *Args, unsigned NumArgs, 163 bool Owned) 164 : Arguments(Args, Owned), NumArguments(NumArgs) { } 165 166public: 167 /// \brief Type used to indicate that the template argument list itself is a 168 /// stack object. It does not own its template arguments. 169 enum OnStackType { OnStack }; 170 171 /// \brief Create a new template argument list that copies the given set of 172 /// template arguments. 173 static TemplateArgumentList *CreateCopy(ASTContext &Context, 174 const TemplateArgument *Args, 175 unsigned NumArgs); 176 177 /// \brief Construct a new, temporary template argument list on the stack. 178 /// 179 /// The template argument list does not own the template arguments 180 /// provided. 181 explicit TemplateArgumentList(OnStackType, 182 const TemplateArgument *Args, unsigned NumArgs) 183 : Arguments(Args, false), NumArguments(NumArgs) { } 184 185 /// \brief Produces a shallow copy of the given template argument list. 186 /// 187 /// This operation assumes that the input argument list outlives it. 188 /// This takes the list as a pointer to avoid looking like a copy 189 /// constructor, since this really really isn't safe to use that 190 /// way. 191 explicit TemplateArgumentList(const TemplateArgumentList *Other) 192 : Arguments(Other->data(), false), NumArguments(Other->size()) { } 193 194 /// \brief Retrieve the template argument at a given index. 195 const TemplateArgument &get(unsigned Idx) const { 196 assert(Idx < NumArguments && "Invalid template argument index"); 197 return data()[Idx]; 198 } 199 200 /// \brief Retrieve the template argument at a given index. 201 const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); } 202 203 /// \brief Produce this as an array ref. 204 llvm::ArrayRef<TemplateArgument> asArray() const { 205 return llvm::ArrayRef<TemplateArgument>(data(), size()); 206 } 207 208 /// \brief Retrieve the number of template arguments in this 209 /// template argument list. 210 unsigned size() const { return NumArguments; } 211 212 /// \brief Retrieve a pointer to the template argument list. 213 const TemplateArgument *data() const { 214 return Arguments.getPointer(); 215 } 216}; 217 218//===----------------------------------------------------------------------===// 219// Kinds of Templates 220//===----------------------------------------------------------------------===// 221 222/// \brief The base class of all kinds of template declarations (e.g., 223/// class, function, etc.). 224/// 225/// The TemplateDecl class stores the list of template parameters and a 226/// reference to the templated scoped declaration: the underlying AST node. 227class TemplateDecl : public NamedDecl { 228 virtual void anchor(); 229protected: 230 // This is probably never used. 231 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 232 DeclarationName Name) 233 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { } 234 235 // Construct a template decl with the given name and parameters. 236 // Used when there is not templated element (tt-params, alias?). 237 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 238 DeclarationName Name, TemplateParameterList *Params) 239 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { } 240 241 // Construct a template decl with name, parameters, and templated element. 242 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 243 DeclarationName Name, TemplateParameterList *Params, 244 NamedDecl *Decl) 245 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), 246 TemplateParams(Params) { } 247public: 248 /// Get the list of template parameters 249 TemplateParameterList *getTemplateParameters() const { 250 return TemplateParams; 251 } 252 253 /// Get the underlying, templated declaration. 254 NamedDecl *getTemplatedDecl() const { return TemplatedDecl; } 255 256 // Implement isa/cast/dyncast/etc. 257 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 258 static bool classofKind(Kind K) { 259 return K >= firstTemplate && K <= lastTemplate; 260 } 261 262 SourceRange getSourceRange() const LLVM_READONLY { 263 return SourceRange(TemplateParams->getTemplateLoc(), 264 TemplatedDecl->getSourceRange().getEnd()); 265 } 266 267protected: 268 NamedDecl *TemplatedDecl; 269 TemplateParameterList* TemplateParams; 270 271public: 272 /// \brief Initialize the underlying templated declaration and 273 /// template parameters. 274 void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) { 275 assert(TemplatedDecl == 0 && "TemplatedDecl already set!"); 276 assert(TemplateParams == 0 && "TemplateParams already set!"); 277 TemplatedDecl = templatedDecl; 278 TemplateParams = templateParams; 279 } 280}; 281 282/// \brief Provides information about a function template specialization, 283/// which is a FunctionDecl that has been explicitly specialization or 284/// instantiated from a function template. 285class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode { 286 FunctionTemplateSpecializationInfo(FunctionDecl *FD, 287 FunctionTemplateDecl *Template, 288 TemplateSpecializationKind TSK, 289 const TemplateArgumentList *TemplateArgs, 290 const ASTTemplateArgumentListInfo *TemplateArgsAsWritten, 291 SourceLocation POI) 292 : Function(FD), 293 Template(Template, TSK - 1), 294 TemplateArguments(TemplateArgs), 295 TemplateArgumentsAsWritten(TemplateArgsAsWritten), 296 PointOfInstantiation(POI) { } 297 298public: 299 static FunctionTemplateSpecializationInfo * 300 Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template, 301 TemplateSpecializationKind TSK, 302 const TemplateArgumentList *TemplateArgs, 303 const TemplateArgumentListInfo *TemplateArgsAsWritten, 304 SourceLocation POI); 305 306 /// \brief The function template specialization that this structure 307 /// describes. 308 FunctionDecl *Function; 309 310 /// \brief The function template from which this function template 311 /// specialization was generated. 312 /// 313 /// The two bits are contain the top 4 values of TemplateSpecializationKind. 314 llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template; 315 316 /// \brief The template arguments used to produce the function template 317 /// specialization from the function template. 318 const TemplateArgumentList *TemplateArguments; 319 320 /// \brief The template arguments as written in the sources, if provided. 321 const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten; 322 323 /// \brief The point at which this function template specialization was 324 /// first instantiated. 325 SourceLocation PointOfInstantiation; 326 327 /// \brief Retrieve the template from which this function was specialized. 328 FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); } 329 330 /// \brief Determine what kind of template specialization this is. 331 TemplateSpecializationKind getTemplateSpecializationKind() const { 332 return (TemplateSpecializationKind)(Template.getInt() + 1); 333 } 334 335 bool isExplicitSpecialization() const { 336 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization; 337 } 338 339 /// \brief True if this declaration is an explicit specialization, 340 /// explicit instantiation declaration, or explicit instantiation 341 /// definition. 342 bool isExplicitInstantiationOrSpecialization() const { 343 switch (getTemplateSpecializationKind()) { 344 case TSK_ExplicitSpecialization: 345 case TSK_ExplicitInstantiationDeclaration: 346 case TSK_ExplicitInstantiationDefinition: 347 return true; 348 349 case TSK_Undeclared: 350 case TSK_ImplicitInstantiation: 351 return false; 352 } 353 llvm_unreachable("bad template specialization kind"); 354 } 355 356 /// \brief Set the template specialization kind. 357 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 358 assert(TSK != TSK_Undeclared && 359 "Cannot encode TSK_Undeclared for a function template specialization"); 360 Template.setInt(TSK - 1); 361 } 362 363 /// \brief Retrieve the first point of instantiation of this function 364 /// template specialization. 365 /// 366 /// The point of instantiation may be an invalid source location if this 367 /// function has yet to be instantiated. 368 SourceLocation getPointOfInstantiation() const { 369 return PointOfInstantiation; 370 } 371 372 /// \brief Set the (first) point of instantiation of this function template 373 /// specialization. 374 void setPointOfInstantiation(SourceLocation POI) { 375 PointOfInstantiation = POI; 376 } 377 378 void Profile(llvm::FoldingSetNodeID &ID) { 379 Profile(ID, TemplateArguments->data(), 380 TemplateArguments->size(), 381 Function->getASTContext()); 382 } 383 384 static void 385 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs, 386 unsigned NumTemplateArgs, ASTContext &Context) { 387 ID.AddInteger(NumTemplateArgs); 388 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 389 TemplateArgs[Arg].Profile(ID, Context); 390 } 391}; 392 393/// \brief Provides information a specialization of a member of a class 394/// template, which may be a member function, static data member, 395/// member class or member enumeration. 396class MemberSpecializationInfo { 397 // The member declaration from which this member was instantiated, and the 398 // manner in which the instantiation occurred (in the lower two bits). 399 llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK; 400 401 // The point at which this member was first instantiated. 402 SourceLocation PointOfInstantiation; 403 404public: 405 explicit 406 MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK, 407 SourceLocation POI = SourceLocation()) 408 : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) { 409 assert(TSK != TSK_Undeclared && 410 "Cannot encode undeclared template specializations for members"); 411 } 412 413 /// \brief Retrieve the member declaration from which this member was 414 /// instantiated. 415 NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); } 416 417 /// \brief Determine what kind of template specialization this is. 418 TemplateSpecializationKind getTemplateSpecializationKind() const { 419 return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1); 420 } 421 422 bool isExplicitSpecialization() const { 423 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization; 424 } 425 426 /// \brief Set the template specialization kind. 427 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 428 assert(TSK != TSK_Undeclared && 429 "Cannot encode undeclared template specializations for members"); 430 MemberAndTSK.setInt(TSK - 1); 431 } 432 433 /// \brief Retrieve the first point of instantiation of this member. 434 /// If the point of instantiation is an invalid location, then this member 435 /// has not yet been instantiated. 436 SourceLocation getPointOfInstantiation() const { 437 return PointOfInstantiation; 438 } 439 440 /// \brief Set the first point of instantiation. 441 void setPointOfInstantiation(SourceLocation POI) { 442 PointOfInstantiation = POI; 443 } 444}; 445 446/// \brief Provides information about a dependent function-template 447/// specialization declaration. 448/// 449/// Since explicit function template specialization and instantiation 450/// declarations can only appear in namespace scope, and you can only 451/// specialize a member of a fully-specialized class, the only way to 452/// get one of these is in a friend declaration like the following: 453/// 454/// \code 455/// template \<class T> void foo(T); 456/// template \<class T> class A { 457/// friend void foo<>(T); 458/// }; 459/// \endcode 460class DependentFunctionTemplateSpecializationInfo { 461 struct CA { 462 /// The number of potential template candidates. 463 unsigned NumTemplates; 464 465 /// The number of template arguments. 466 unsigned NumArgs; 467 }; 468 469 union { 470 // Force sizeof to be a multiple of sizeof(void*) so that the 471 // trailing data is aligned. 472 void *Aligner; 473 struct CA d; 474 }; 475 476 /// The locations of the left and right angle brackets. 477 SourceRange AngleLocs; 478 479 FunctionTemplateDecl * const *getTemplates() const { 480 return reinterpret_cast<FunctionTemplateDecl*const*>(this+1); 481 } 482 483public: 484 DependentFunctionTemplateSpecializationInfo( 485 const UnresolvedSetImpl &Templates, 486 const TemplateArgumentListInfo &TemplateArgs); 487 488 /// \brief Returns the number of function templates that this might 489 /// be a specialization of. 490 unsigned getNumTemplates() const { 491 return d.NumTemplates; 492 } 493 494 /// \brief Returns the i'th template candidate. 495 FunctionTemplateDecl *getTemplate(unsigned I) const { 496 assert(I < getNumTemplates() && "template index out of range"); 497 return getTemplates()[I]; 498 } 499 500 /// \brief Returns the explicit template arguments that were given. 501 const TemplateArgumentLoc *getTemplateArgs() const { 502 return reinterpret_cast<const TemplateArgumentLoc*>( 503 &getTemplates()[getNumTemplates()]); 504 } 505 506 /// \brief Returns the number of explicit template arguments that were given. 507 unsigned getNumTemplateArgs() const { 508 return d.NumArgs; 509 } 510 511 /// \brief Returns the nth template argument. 512 const TemplateArgumentLoc &getTemplateArg(unsigned I) const { 513 assert(I < getNumTemplateArgs() && "template arg index out of range"); 514 return getTemplateArgs()[I]; 515 } 516 517 SourceLocation getLAngleLoc() const { 518 return AngleLocs.getBegin(); 519 } 520 521 SourceLocation getRAngleLoc() const { 522 return AngleLocs.getEnd(); 523 } 524}; 525 526/// Declaration of a redeclarable template. 527class RedeclarableTemplateDecl : public TemplateDecl, 528 public Redeclarable<RedeclarableTemplateDecl> 529{ 530 typedef Redeclarable<RedeclarableTemplateDecl> redeclarable_base; 531 virtual RedeclarableTemplateDecl *getNextRedeclaration() { 532 return RedeclLink.getNext(); 533 } 534 virtual RedeclarableTemplateDecl *getPreviousDeclImpl() { 535 return getPreviousDecl(); 536 } 537 virtual RedeclarableTemplateDecl *getMostRecentDeclImpl() { 538 return getMostRecentDecl(); 539 } 540 541protected: 542 template <typename EntryType> struct SpecEntryTraits { 543 typedef EntryType DeclType; 544 545 static DeclType *getMostRecentDecl(EntryType *D) { 546 return D->getMostRecentDecl(); 547 } 548 }; 549 550 template <typename EntryType, 551 typename _SETraits = SpecEntryTraits<EntryType>, 552 typename _DeclType = typename _SETraits::DeclType> 553 class SpecIterator : public std::iterator<std::forward_iterator_tag, 554 _DeclType*, ptrdiff_t, 555 _DeclType*, _DeclType*> { 556 typedef _SETraits SETraits; 557 typedef _DeclType DeclType; 558 559 typedef typename llvm::FoldingSetVector<EntryType>::iterator 560 SetIteratorType; 561 562 SetIteratorType SetIter; 563 564 public: 565 SpecIterator() : SetIter() {} 566 SpecIterator(SetIteratorType SetIter) : SetIter(SetIter) {} 567 568 DeclType *operator*() const { 569 return SETraits::getMostRecentDecl(&*SetIter); 570 } 571 DeclType *operator->() const { return **this; } 572 573 SpecIterator &operator++() { ++SetIter; return *this; } 574 SpecIterator operator++(int) { 575 SpecIterator tmp(*this); 576 ++(*this); 577 return tmp; 578 } 579 580 bool operator==(SpecIterator Other) const { 581 return SetIter == Other.SetIter; 582 } 583 bool operator!=(SpecIterator Other) const { 584 return SetIter != Other.SetIter; 585 } 586 }; 587 588 template <typename EntryType> 589 static SpecIterator<EntryType> 590 makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) { 591 return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin()); 592 } 593 594 template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType* 595 findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs, 596 const TemplateArgument *Args, unsigned NumArgs, 597 void *&InsertPos); 598 599 struct CommonBase { 600 CommonBase() : InstantiatedFromMember(0, false) { } 601 602 /// \brief The template from which this was most 603 /// directly instantiated (or null). 604 /// 605 /// The boolean value indicates whether this template 606 /// was explicitly specialized. 607 llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool> 608 InstantiatedFromMember; 609 }; 610 611 /// \brief Pointer to the common data shared by all declarations of this 612 /// template. 613 mutable CommonBase *Common; 614 615 /// \brief Retrieves the "common" pointer shared by all (re-)declarations of 616 /// the same template. Calling this routine may implicitly allocate memory 617 /// for the common pointer. 618 CommonBase *getCommonPtr() const; 619 620 virtual CommonBase *newCommon(ASTContext &C) const = 0; 621 622 // Construct a template decl with name, parameters, and templated element. 623 RedeclarableTemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 624 DeclarationName Name, TemplateParameterList *Params, 625 NamedDecl *Decl) 626 : TemplateDecl(DK, DC, L, Name, Params, Decl), Common() { } 627 628public: 629 template <class decl_type> friend class RedeclarableTemplate; 630 631 /// \brief Retrieves the canonical declaration of this template. 632 RedeclarableTemplateDecl *getCanonicalDecl() { return getFirstDeclaration(); } 633 const RedeclarableTemplateDecl *getCanonicalDecl() const { 634 return getFirstDeclaration(); 635 } 636 637 /// \brief Determines whether this template was a specialization of a 638 /// member template. 639 /// 640 /// In the following example, the function template \c X<int>::f and the 641 /// member template \c X<int>::Inner are member specializations. 642 /// 643 /// \code 644 /// template<typename T> 645 /// struct X { 646 /// template<typename U> void f(T, U); 647 /// template<typename U> struct Inner; 648 /// }; 649 /// 650 /// template<> template<typename T> 651 /// void X<int>::f(int, T); 652 /// template<> template<typename T> 653 /// struct X<int>::Inner { /* ... */ }; 654 /// \endcode 655 bool isMemberSpecialization() const { 656 return getCommonPtr()->InstantiatedFromMember.getInt(); 657 } 658 659 /// \brief Note that this member template is a specialization. 660 void setMemberSpecialization() { 661 assert(getCommonPtr()->InstantiatedFromMember.getPointer() && 662 "Only member templates can be member template specializations"); 663 getCommonPtr()->InstantiatedFromMember.setInt(true); 664 } 665 666 /// \brief Retrieve the member template from which this template was 667 /// instantiated, or NULL if this template was not instantiated from a 668 /// member template. 669 /// 670 /// A template is instantiated from a member template when the member 671 /// template itself is part of a class template (or member thereof). For 672 /// example, given 673 /// 674 /// \code 675 /// template<typename T> 676 /// struct X { 677 /// template<typename U> void f(T, U); 678 /// }; 679 /// 680 /// void test(X<int> x) { 681 /// x.f(1, 'a'); 682 /// }; 683 /// \endcode 684 /// 685 /// \c X<int>::f is a FunctionTemplateDecl that describes the function 686 /// template 687 /// 688 /// \code 689 /// template<typename U> void X<int>::f(int, U); 690 /// \endcode 691 /// 692 /// which was itself created during the instantiation of \c X<int>. Calling 693 /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will 694 /// retrieve the FunctionTemplateDecl for the original template \c f within 695 /// the class template \c X<T>, i.e., 696 /// 697 /// \code 698 /// template<typename T> 699 /// template<typename U> 700 /// void X<T>::f(T, U); 701 /// \endcode 702 RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const { 703 return getCommonPtr()->InstantiatedFromMember.getPointer(); 704 } 705 706 void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) { 707 assert(!getCommonPtr()->InstantiatedFromMember.getPointer()); 708 getCommonPtr()->InstantiatedFromMember.setPointer(TD); 709 } 710 711 typedef redeclarable_base::redecl_iterator redecl_iterator; 712 using redeclarable_base::redecls_begin; 713 using redeclarable_base::redecls_end; 714 using redeclarable_base::getPreviousDecl; 715 using redeclarable_base::getMostRecentDecl; 716 717 // Implement isa/cast/dyncast/etc. 718 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 719 static bool classofKind(Kind K) { 720 return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate; 721 } 722 723 friend class ASTReader; 724 friend class ASTDeclReader; 725 friend class ASTDeclWriter; 726}; 727 728template <> struct RedeclarableTemplateDecl:: 729SpecEntryTraits<FunctionTemplateSpecializationInfo> { 730 typedef FunctionDecl DeclType; 731 732 static DeclType * 733 getMostRecentDecl(FunctionTemplateSpecializationInfo *I) { 734 return I->Function->getMostRecentDecl(); 735 } 736}; 737 738/// Declaration of a template function. 739class FunctionTemplateDecl : public RedeclarableTemplateDecl { 740 static void DeallocateCommon(void *Ptr); 741 742protected: 743 /// \brief Data that is common to all of the declarations of a given 744 /// function template. 745 struct Common : CommonBase { 746 Common() : InjectedArgs(), LazySpecializations() { } 747 748 /// \brief The function template specializations for this function 749 /// template, including explicit specializations and instantiations. 750 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations; 751 752 /// \brief The set of "injected" template arguments used within this 753 /// function template. 754 /// 755 /// This pointer refers to the template arguments (there are as 756 /// many template arguments as template parameaters) for the function 757 /// template, and is allocated lazily, since most function templates do not 758 /// require the use of this information. 759 TemplateArgument *InjectedArgs; 760 761 /// \brief If non-null, points to an array of specializations known only 762 /// by their external declaration IDs. 763 /// 764 /// The first value in the array is the number of of specializations 765 /// that follow. 766 uint32_t *LazySpecializations; 767 }; 768 769 FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 770 TemplateParameterList *Params, NamedDecl *Decl) 771 : RedeclarableTemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl) { } 772 773 CommonBase *newCommon(ASTContext &C) const; 774 775 Common *getCommonPtr() const { 776 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 777 } 778 779 friend class FunctionDecl; 780 781 /// \brief Load any lazily-loaded specializations from the external source. 782 void LoadLazySpecializations() const; 783 784 /// \brief Retrieve the set of function template specializations of this 785 /// function template. 786 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> & 787 getSpecializations() const; 788 789 /// \brief Add a specialization of this function template. 790 /// 791 /// \param InsertPos Insert position in the FoldingSetVector, must have been 792 /// retrieved by an earlier call to findSpecialization(). 793 void addSpecialization(FunctionTemplateSpecializationInfo* Info, 794 void *InsertPos); 795 796public: 797 /// Get the underlying function declaration of the template. 798 FunctionDecl *getTemplatedDecl() const { 799 return static_cast<FunctionDecl*>(TemplatedDecl); 800 } 801 802 /// Returns whether this template declaration defines the primary 803 /// pattern. 804 bool isThisDeclarationADefinition() const { 805 return getTemplatedDecl()->isThisDeclarationADefinition(); 806 } 807 808 /// \brief Return the specialization with the provided arguments if it exists, 809 /// otherwise return the insertion point. 810 FunctionDecl *findSpecialization(const TemplateArgument *Args, 811 unsigned NumArgs, void *&InsertPos); 812 813 FunctionTemplateDecl *getCanonicalDecl() { 814 return cast<FunctionTemplateDecl>( 815 RedeclarableTemplateDecl::getCanonicalDecl()); 816 } 817 const FunctionTemplateDecl *getCanonicalDecl() const { 818 return cast<FunctionTemplateDecl>( 819 RedeclarableTemplateDecl::getCanonicalDecl()); 820 } 821 822 /// \brief Retrieve the previous declaration of this function template, or 823 /// NULL if no such declaration exists. 824 FunctionTemplateDecl *getPreviousDecl() { 825 return cast_or_null<FunctionTemplateDecl>( 826 RedeclarableTemplateDecl::getPreviousDecl()); 827 } 828 829 /// \brief Retrieve the previous declaration of this function template, or 830 /// NULL if no such declaration exists. 831 const FunctionTemplateDecl *getPreviousDecl() const { 832 return cast_or_null<FunctionTemplateDecl>( 833 RedeclarableTemplateDecl::getPreviousDecl()); 834 } 835 836 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() { 837 return cast_or_null<FunctionTemplateDecl>( 838 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 839 } 840 841 typedef SpecIterator<FunctionTemplateSpecializationInfo> spec_iterator; 842 843 spec_iterator spec_begin() const { 844 return makeSpecIterator(getSpecializations(), false); 845 } 846 847 spec_iterator spec_end() const { 848 return makeSpecIterator(getSpecializations(), true); 849 } 850 851 /// \brief Retrieve the "injected" template arguments that correspond to the 852 /// template parameters of this function template. 853 /// 854 /// Although the C++ standard has no notion of the "injected" template 855 /// arguments for a function template, the notion is convenient when 856 /// we need to perform substitutions inside the definition of a function 857 /// template. 858 ArrayRef<TemplateArgument> getInjectedTemplateArgs(); 859 860 /// \brief Create a function template node. 861 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC, 862 SourceLocation L, 863 DeclarationName Name, 864 TemplateParameterList *Params, 865 NamedDecl *Decl); 866 867 /// \brief Create an empty function template node. 868 static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 869 870 // Implement isa/cast/dyncast support 871 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 872 static bool classofKind(Kind K) { return K == FunctionTemplate; } 873 874 friend class ASTDeclReader; 875 friend class ASTDeclWriter; 876}; 877 878//===----------------------------------------------------------------------===// 879// Kinds of Template Parameters 880//===----------------------------------------------------------------------===// 881 882/// \brief Defines the position of a template parameter within a template 883/// parameter list. 884/// 885/// Because template parameter can be listed 886/// sequentially for out-of-line template members, each template parameter is 887/// given a Depth - the nesting of template parameter scopes - and a Position - 888/// the occurrence within the parameter list. 889/// This class is inheritedly privately by different kinds of template 890/// parameters and is not part of the Decl hierarchy. Just a facility. 891class TemplateParmPosition { 892protected: 893 // FIXME: This should probably never be called, but it's here as 894 TemplateParmPosition() 895 : Depth(0), Position(0) 896 { /* llvm_unreachable("Cannot create positionless template parameter"); */ } 897 898 TemplateParmPosition(unsigned D, unsigned P) 899 : Depth(D), Position(P) 900 { } 901 902 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for 903 // position? Maybe? 904 unsigned Depth; 905 unsigned Position; 906 907public: 908 /// Get the nesting depth of the template parameter. 909 unsigned getDepth() const { return Depth; } 910 void setDepth(unsigned D) { Depth = D; } 911 912 /// Get the position of the template parameter within its parameter list. 913 unsigned getPosition() const { return Position; } 914 void setPosition(unsigned P) { Position = P; } 915 916 /// Get the index of the template parameter within its parameter list. 917 unsigned getIndex() const { return Position; } 918}; 919 920/// \brief Declaration of a template type parameter. 921/// 922/// For example, "T" in 923/// \code 924/// template<typename T> class vector; 925/// \endcode 926class TemplateTypeParmDecl : public TypeDecl { 927 /// \brief Whether this template type parameter was declaration with 928 /// the 'typename' keyword. 929 /// 930 /// If false, it was declared with the 'class' keyword. 931 bool Typename : 1; 932 933 /// \brief Whether this template type parameter inherited its 934 /// default argument. 935 bool InheritedDefault : 1; 936 937 /// \brief The default template argument, if any. 938 TypeSourceInfo *DefaultArgument; 939 940 TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc, 941 SourceLocation IdLoc, IdentifierInfo *Id, 942 bool Typename) 943 : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename), 944 InheritedDefault(false), DefaultArgument() { } 945 946 /// Sema creates these on the stack during auto type deduction. 947 friend class Sema; 948 949public: 950 static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC, 951 SourceLocation KeyLoc, 952 SourceLocation NameLoc, 953 unsigned D, unsigned P, 954 IdentifierInfo *Id, bool Typename, 955 bool ParameterPack); 956 static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C, 957 unsigned ID); 958 959 /// \brief Whether this template type parameter was declared with 960 /// the 'typename' keyword. 961 /// 962 /// If not, it was declared with the 'class' keyword. 963 bool wasDeclaredWithTypename() const { return Typename; } 964 965 /// \brief Determine whether this template parameter has a default 966 /// argument. 967 bool hasDefaultArgument() const { return DefaultArgument != 0; } 968 969 /// \brief Retrieve the default argument, if any. 970 QualType getDefaultArgument() const { return DefaultArgument->getType(); } 971 972 /// \brief Retrieves the default argument's source information, if any. 973 TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; } 974 975 /// \brief Retrieves the location of the default argument declaration. 976 SourceLocation getDefaultArgumentLoc() const; 977 978 /// \brief Determines whether the default argument was inherited 979 /// from a previous declaration of this template. 980 bool defaultArgumentWasInherited() const { return InheritedDefault; } 981 982 /// \brief Set the default argument for this template parameter, and 983 /// whether that default argument was inherited from another 984 /// declaration. 985 void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) { 986 DefaultArgument = DefArg; 987 InheritedDefault = Inherited; 988 } 989 990 /// \brief Removes the default argument of this template parameter. 991 void removeDefaultArgument() { 992 DefaultArgument = 0; 993 InheritedDefault = false; 994 } 995 996 /// \brief Set whether this template type parameter was declared with 997 /// the 'typename' or 'class' keyword. 998 void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; } 999 1000 /// \brief Retrieve the depth of the template parameter. 1001 unsigned getDepth() const; 1002 1003 /// \brief Retrieve the index of the template parameter. 1004 unsigned getIndex() const; 1005 1006 /// \brief Returns whether this is a parameter pack. 1007 bool isParameterPack() const; 1008 1009 SourceRange getSourceRange() const LLVM_READONLY; 1010 1011 // Implement isa/cast/dyncast/etc. 1012 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1013 static bool classofKind(Kind K) { return K == TemplateTypeParm; } 1014}; 1015 1016/// NonTypeTemplateParmDecl - Declares a non-type template parameter, 1017/// e.g., "Size" in 1018/// @code 1019/// template<int Size> class array { }; 1020/// @endcode 1021class NonTypeTemplateParmDecl 1022 : public DeclaratorDecl, protected TemplateParmPosition { 1023 /// \brief The default template argument, if any, and whether or not 1024 /// it was inherited. 1025 llvm::PointerIntPair<Expr*, 1, bool> DefaultArgumentAndInherited; 1026 1027 // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index 1028 // down here to save memory. 1029 1030 /// \brief Whether this non-type template parameter is a parameter pack. 1031 bool ParameterPack; 1032 1033 /// \brief Whether this non-type template parameter is an "expanded" 1034 /// parameter pack, meaning that its type is a pack expansion and we 1035 /// already know the set of types that expansion expands to. 1036 bool ExpandedParameterPack; 1037 1038 /// \brief The number of types in an expanded parameter pack. 1039 unsigned NumExpandedTypes; 1040 1041 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc, 1042 SourceLocation IdLoc, unsigned D, unsigned P, 1043 IdentifierInfo *Id, QualType T, 1044 bool ParameterPack, TypeSourceInfo *TInfo) 1045 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), 1046 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false), 1047 ParameterPack(ParameterPack), ExpandedParameterPack(false), 1048 NumExpandedTypes(0) 1049 { } 1050 1051 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc, 1052 SourceLocation IdLoc, unsigned D, unsigned P, 1053 IdentifierInfo *Id, QualType T, 1054 TypeSourceInfo *TInfo, 1055 const QualType *ExpandedTypes, 1056 unsigned NumExpandedTypes, 1057 TypeSourceInfo **ExpandedTInfos); 1058 1059 friend class ASTDeclReader; 1060 1061public: 1062 static NonTypeTemplateParmDecl * 1063 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 1064 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, 1065 QualType T, bool ParameterPack, TypeSourceInfo *TInfo); 1066 1067 static NonTypeTemplateParmDecl * 1068 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 1069 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, 1070 QualType T, TypeSourceInfo *TInfo, 1071 const QualType *ExpandedTypes, unsigned NumExpandedTypes, 1072 TypeSourceInfo **ExpandedTInfos); 1073 1074 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, 1075 unsigned ID); 1076 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, 1077 unsigned ID, 1078 unsigned NumExpandedTypes); 1079 1080 using TemplateParmPosition::getDepth; 1081 using TemplateParmPosition::setDepth; 1082 using TemplateParmPosition::getPosition; 1083 using TemplateParmPosition::setPosition; 1084 using TemplateParmPosition::getIndex; 1085 1086 SourceRange getSourceRange() const LLVM_READONLY; 1087 1088 /// \brief Determine whether this template parameter has a default 1089 /// argument. 1090 bool hasDefaultArgument() const { 1091 return DefaultArgumentAndInherited.getPointer() != 0; 1092 } 1093 1094 /// \brief Retrieve the default argument, if any. 1095 Expr *getDefaultArgument() const { 1096 return DefaultArgumentAndInherited.getPointer(); 1097 } 1098 1099 /// \brief Retrieve the location of the default argument, if any. 1100 SourceLocation getDefaultArgumentLoc() const; 1101 1102 /// \brief Determines whether the default argument was inherited 1103 /// from a previous declaration of this template. 1104 bool defaultArgumentWasInherited() const { 1105 return DefaultArgumentAndInherited.getInt(); 1106 } 1107 1108 /// \brief Set the default argument for this template parameter, and 1109 /// whether that default argument was inherited from another 1110 /// declaration. 1111 void setDefaultArgument(Expr *DefArg, bool Inherited) { 1112 DefaultArgumentAndInherited.setPointer(DefArg); 1113 DefaultArgumentAndInherited.setInt(Inherited); 1114 } 1115 1116 /// \brief Removes the default argument of this template parameter. 1117 void removeDefaultArgument() { 1118 DefaultArgumentAndInherited.setPointer(0); 1119 DefaultArgumentAndInherited.setInt(false); 1120 } 1121 1122 /// \brief Whether this parameter is a non-type template parameter pack. 1123 /// 1124 /// If the parameter is a parameter pack, the type may be a 1125 /// \c PackExpansionType. In the following example, the \c Dims parameter 1126 /// is a parameter pack (whose type is 'unsigned'). 1127 /// 1128 /// \code 1129 /// template<typename T, unsigned ...Dims> struct multi_array; 1130 /// \endcode 1131 bool isParameterPack() const { return ParameterPack; } 1132 1133 /// \brief Whether this parameter pack is a pack expansion. 1134 /// 1135 /// A non-type template parameter pack is a pack expansion if its type 1136 /// contains an unexpanded parameter pack. In this case, we will have 1137 /// built a PackExpansionType wrapping the type. 1138 bool isPackExpansion() const { 1139 return ParameterPack && getType()->getAs<PackExpansionType>(); 1140 } 1141 1142 /// \brief Whether this parameter is a non-type template parameter pack 1143 /// that has a known list of different types at different positions. 1144 /// 1145 /// A parameter pack is an expanded parameter pack when the original 1146 /// parameter pack's type was itself a pack expansion, and that expansion 1147 /// has already been expanded. For example, given: 1148 /// 1149 /// \code 1150 /// template<typename ...Types> 1151 /// struct X { 1152 /// template<Types ...Values> 1153 /// struct Y { /* ... */ }; 1154 /// }; 1155 /// \endcode 1156 /// 1157 /// The parameter pack \c Values has a \c PackExpansionType as its type, 1158 /// which expands \c Types. When \c Types is supplied with template arguments 1159 /// by instantiating \c X, the instantiation of \c Values becomes an 1160 /// expanded parameter pack. For example, instantiating 1161 /// \c X<int, unsigned int> results in \c Values being an expanded parameter 1162 /// pack with expansion types \c int and \c unsigned int. 1163 /// 1164 /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions 1165 /// return the expansion types. 1166 bool isExpandedParameterPack() const { return ExpandedParameterPack; } 1167 1168 /// \brief Retrieves the number of expansion types in an expanded parameter 1169 /// pack. 1170 unsigned getNumExpansionTypes() const { 1171 assert(ExpandedParameterPack && "Not an expansion parameter pack"); 1172 return NumExpandedTypes; 1173 } 1174 1175 /// \brief Retrieve a particular expansion type within an expanded parameter 1176 /// pack. 1177 QualType getExpansionType(unsigned I) const { 1178 assert(I < NumExpandedTypes && "Out-of-range expansion type index"); 1179 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1); 1180 return QualType::getFromOpaquePtr(TypesAndInfos[2*I]); 1181 } 1182 1183 /// \brief Retrieve a particular expansion type source info within an 1184 /// expanded parameter pack. 1185 TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const { 1186 assert(I < NumExpandedTypes && "Out-of-range expansion type index"); 1187 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1); 1188 return static_cast<TypeSourceInfo *>(TypesAndInfos[2*I+1]); 1189 } 1190 1191 // Implement isa/cast/dyncast/etc. 1192 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1193 static bool classofKind(Kind K) { return K == NonTypeTemplateParm; } 1194}; 1195 1196/// TemplateTemplateParmDecl - Declares a template template parameter, 1197/// e.g., "T" in 1198/// @code 1199/// template <template <typename> class T> class container { }; 1200/// @endcode 1201/// A template template parameter is a TemplateDecl because it defines the 1202/// name of a template and the template parameters allowable for substitution. 1203class TemplateTemplateParmDecl : public TemplateDecl, 1204 protected TemplateParmPosition 1205{ 1206 virtual void anchor(); 1207 1208 /// DefaultArgument - The default template argument, if any. 1209 TemplateArgumentLoc DefaultArgument; 1210 /// Whether or not the default argument was inherited. 1211 bool DefaultArgumentWasInherited; 1212 1213 /// \brief Whether this parameter is a parameter pack. 1214 bool ParameterPack; 1215 1216 /// \brief Whether this template template parameter is an "expanded" 1217 /// parameter pack, meaning that it is a pack expansion and we 1218 /// already know the set of template parameters that expansion expands to. 1219 bool ExpandedParameterPack; 1220 1221 /// \brief The number of parameters in an expanded parameter pack. 1222 unsigned NumExpandedParams; 1223 1224 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, 1225 unsigned D, unsigned P, bool ParameterPack, 1226 IdentifierInfo *Id, TemplateParameterList *Params) 1227 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), 1228 TemplateParmPosition(D, P), DefaultArgument(), 1229 DefaultArgumentWasInherited(false), ParameterPack(ParameterPack), 1230 ExpandedParameterPack(false), NumExpandedParams(0) 1231 { } 1232 1233 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, 1234 unsigned D, unsigned P, 1235 IdentifierInfo *Id, TemplateParameterList *Params, 1236 unsigned NumExpansions, 1237 TemplateParameterList * const *Expansions); 1238 1239public: 1240 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC, 1241 SourceLocation L, unsigned D, 1242 unsigned P, bool ParameterPack, 1243 IdentifierInfo *Id, 1244 TemplateParameterList *Params); 1245 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC, 1246 SourceLocation L, unsigned D, 1247 unsigned P, 1248 IdentifierInfo *Id, 1249 TemplateParameterList *Params, 1250 ArrayRef<TemplateParameterList *> Expansions); 1251 1252 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, 1253 unsigned ID); 1254 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, 1255 unsigned ID, 1256 unsigned NumExpansions); 1257 1258 using TemplateParmPosition::getDepth; 1259 using TemplateParmPosition::getPosition; 1260 using TemplateParmPosition::getIndex; 1261 1262 /// \brief Whether this template template parameter is a template 1263 /// parameter pack. 1264 /// 1265 /// \code 1266 /// template<template <class T> ...MetaFunctions> struct Apply; 1267 /// \endcode 1268 bool isParameterPack() const { return ParameterPack; } 1269 1270 /// \brief Whether this parameter pack is a pack expansion. 1271 /// 1272 /// A template template parameter pack is a pack expansion if its template 1273 /// parameter list contains an unexpanded parameter pack. 1274 bool isPackExpansion() const { 1275 return ParameterPack && 1276 getTemplateParameters()->containsUnexpandedParameterPack(); 1277 } 1278 1279 /// \brief Whether this parameter is a template template parameter pack that 1280 /// has a known list of different template parameter lists at different 1281 /// positions. 1282 /// 1283 /// A parameter pack is an expanded parameter pack when the original parameter 1284 /// pack's template parameter list was itself a pack expansion, and that 1285 /// expansion has already been expanded. For exampe, given: 1286 /// 1287 /// \code 1288 /// template<typename...Types> struct Outer { 1289 /// template<template<Types> class...Templates> struct Inner; 1290 /// }; 1291 /// \endcode 1292 /// 1293 /// The parameter pack \c Templates is a pack expansion, which expands the 1294 /// pack \c Types. When \c Types is supplied with template arguments by 1295 /// instantiating \c Outer, the instantiation of \c Templates is an expanded 1296 /// parameter pack. 1297 bool isExpandedParameterPack() const { return ExpandedParameterPack; } 1298 1299 /// \brief Retrieves the number of expansion template parameters in 1300 /// an expanded parameter pack. 1301 unsigned getNumExpansionTemplateParameters() const { 1302 assert(ExpandedParameterPack && "Not an expansion parameter pack"); 1303 return NumExpandedParams; 1304 } 1305 1306 /// \brief Retrieve a particular expansion type within an expanded parameter 1307 /// pack. 1308 TemplateParameterList *getExpansionTemplateParameters(unsigned I) const { 1309 assert(I < NumExpandedParams && "Out-of-range expansion type index"); 1310 return reinterpret_cast<TemplateParameterList *const *>(this + 1)[I]; 1311 } 1312 1313 /// \brief Determine whether this template parameter has a default 1314 /// argument. 1315 bool hasDefaultArgument() const { 1316 return !DefaultArgument.getArgument().isNull(); 1317 } 1318 1319 /// \brief Retrieve the default argument, if any. 1320 const TemplateArgumentLoc &getDefaultArgument() const { 1321 return DefaultArgument; 1322 } 1323 1324 /// \brief Retrieve the location of the default argument, if any. 1325 SourceLocation getDefaultArgumentLoc() const; 1326 1327 /// \brief Determines whether the default argument was inherited 1328 /// from a previous declaration of this template. 1329 bool defaultArgumentWasInherited() const { 1330 return DefaultArgumentWasInherited; 1331 } 1332 1333 /// \brief Set the default argument for this template parameter, and 1334 /// whether that default argument was inherited from another 1335 /// declaration. 1336 void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) { 1337 DefaultArgument = DefArg; 1338 DefaultArgumentWasInherited = Inherited; 1339 } 1340 1341 /// \brief Removes the default argument of this template parameter. 1342 void removeDefaultArgument() { 1343 DefaultArgument = TemplateArgumentLoc(); 1344 DefaultArgumentWasInherited = false; 1345 } 1346 1347 SourceRange getSourceRange() const LLVM_READONLY { 1348 SourceLocation End = getLocation(); 1349 if (hasDefaultArgument() && !defaultArgumentWasInherited()) 1350 End = getDefaultArgument().getSourceRange().getEnd(); 1351 return SourceRange(getTemplateParameters()->getTemplateLoc(), End); 1352 } 1353 1354 // Implement isa/cast/dyncast/etc. 1355 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1356 static bool classofKind(Kind K) { return K == TemplateTemplateParm; } 1357 1358 friend class ASTDeclReader; 1359 friend class ASTDeclWriter; 1360}; 1361 1362/// \brief Represents a class template specialization, which refers to 1363/// a class template with a given set of template arguments. 1364/// 1365/// Class template specializations represent both explicit 1366/// specialization of class templates, as in the example below, and 1367/// implicit instantiations of class templates. 1368/// 1369/// \code 1370/// template<typename T> class array; 1371/// 1372/// template<> 1373/// class array<bool> { }; // class template specialization array<bool> 1374/// \endcode 1375class ClassTemplateSpecializationDecl 1376 : public CXXRecordDecl, public llvm::FoldingSetNode { 1377 1378 /// \brief Structure that stores information about a class template 1379 /// specialization that was instantiated from a class template partial 1380 /// specialization. 1381 struct SpecializedPartialSpecialization { 1382 /// \brief The class template partial specialization from which this 1383 /// class template specialization was instantiated. 1384 ClassTemplatePartialSpecializationDecl *PartialSpecialization; 1385 1386 /// \brief The template argument list deduced for the class template 1387 /// partial specialization itself. 1388 TemplateArgumentList *TemplateArgs; 1389 }; 1390 1391 /// \brief The template that this specialization specializes 1392 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *> 1393 SpecializedTemplate; 1394 1395 /// \brief Further info for explicit template specialization/instantiation. 1396 struct ExplicitSpecializationInfo { 1397 /// \brief The type-as-written. 1398 TypeSourceInfo *TypeAsWritten; 1399 /// \brief The location of the extern keyword. 1400 SourceLocation ExternLoc; 1401 /// \brief The location of the template keyword. 1402 SourceLocation TemplateKeywordLoc; 1403 1404 ExplicitSpecializationInfo() 1405 : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {} 1406 }; 1407 1408 /// \brief Further info for explicit template specialization/instantiation. 1409 /// Does not apply to implicit specializations. 1410 ExplicitSpecializationInfo *ExplicitInfo; 1411 1412 /// \brief The template arguments used to describe this specialization. 1413 TemplateArgumentList *TemplateArgs; 1414 1415 /// \brief The point where this template was instantiated (if any) 1416 SourceLocation PointOfInstantiation; 1417 1418 /// \brief The kind of specialization this declaration refers to. 1419 /// Really a value of type TemplateSpecializationKind. 1420 unsigned SpecializationKind : 3; 1421 1422protected: 1423 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, 1424 DeclContext *DC, SourceLocation StartLoc, 1425 SourceLocation IdLoc, 1426 ClassTemplateDecl *SpecializedTemplate, 1427 const TemplateArgument *Args, 1428 unsigned NumArgs, 1429 ClassTemplateSpecializationDecl *PrevDecl); 1430 1431 explicit ClassTemplateSpecializationDecl(Kind DK); 1432 1433public: 1434 static ClassTemplateSpecializationDecl * 1435 Create(ASTContext &Context, TagKind TK, DeclContext *DC, 1436 SourceLocation StartLoc, SourceLocation IdLoc, 1437 ClassTemplateDecl *SpecializedTemplate, 1438 const TemplateArgument *Args, 1439 unsigned NumArgs, 1440 ClassTemplateSpecializationDecl *PrevDecl); 1441 static ClassTemplateSpecializationDecl * 1442 CreateDeserialized(ASTContext &C, unsigned ID); 1443 1444 virtual void getNameForDiagnostic(raw_ostream &OS, 1445 const PrintingPolicy &Policy, 1446 bool Qualified) const; 1447 1448 ClassTemplateSpecializationDecl *getMostRecentDecl() { 1449 CXXRecordDecl *Recent 1450 = cast<CXXRecordDecl>(CXXRecordDecl::getMostRecentDecl()); 1451 if (!isa<ClassTemplateSpecializationDecl>(Recent)) { 1452 // FIXME: Does injected class name need to be in the redeclarations chain? 1453 assert(Recent->isInjectedClassName() && Recent->getPreviousDecl()); 1454 Recent = Recent->getPreviousDecl(); 1455 } 1456 return cast<ClassTemplateSpecializationDecl>(Recent); 1457 } 1458 1459 /// \brief Retrieve the template that this specialization specializes. 1460 ClassTemplateDecl *getSpecializedTemplate() const; 1461 1462 /// \brief Retrieve the template arguments of the class template 1463 /// specialization. 1464 const TemplateArgumentList &getTemplateArgs() const { 1465 return *TemplateArgs; 1466 } 1467 1468 /// \brief Determine the kind of specialization that this 1469 /// declaration represents. 1470 TemplateSpecializationKind getSpecializationKind() const { 1471 return static_cast<TemplateSpecializationKind>(SpecializationKind); 1472 } 1473 1474 bool isExplicitSpecialization() const { 1475 return getSpecializationKind() == TSK_ExplicitSpecialization; 1476 } 1477 1478 /// \brief True if this declaration is an explicit specialization, 1479 /// explicit instantiation declaration, or explicit instantiation 1480 /// definition. 1481 bool isExplicitInstantiationOrSpecialization() const { 1482 switch (getTemplateSpecializationKind()) { 1483 case TSK_ExplicitSpecialization: 1484 case TSK_ExplicitInstantiationDeclaration: 1485 case TSK_ExplicitInstantiationDefinition: 1486 return true; 1487 1488 case TSK_Undeclared: 1489 case TSK_ImplicitInstantiation: 1490 return false; 1491 } 1492 llvm_unreachable("bad template specialization kind"); 1493 } 1494 1495 void setSpecializationKind(TemplateSpecializationKind TSK) { 1496 SpecializationKind = TSK; 1497 } 1498 1499 /// \brief Get the point of instantiation (if any), or null if none. 1500 SourceLocation getPointOfInstantiation() const { 1501 return PointOfInstantiation; 1502 } 1503 1504 void setPointOfInstantiation(SourceLocation Loc) { 1505 assert(Loc.isValid() && "point of instantiation must be valid!"); 1506 PointOfInstantiation = Loc; 1507 } 1508 1509 /// \brief If this class template specialization is an instantiation of 1510 /// a template (rather than an explicit specialization), return the 1511 /// class template or class template partial specialization from which it 1512 /// was instantiated. 1513 llvm::PointerUnion<ClassTemplateDecl *, 1514 ClassTemplatePartialSpecializationDecl *> 1515 getInstantiatedFrom() const { 1516 if (getSpecializationKind() != TSK_ImplicitInstantiation && 1517 getSpecializationKind() != TSK_ExplicitInstantiationDefinition && 1518 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration) 1519 return llvm::PointerUnion<ClassTemplateDecl *, 1520 ClassTemplatePartialSpecializationDecl *>(); 1521 1522 if (SpecializedPartialSpecialization *PartialSpec 1523 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1524 return PartialSpec->PartialSpecialization; 1525 1526 return SpecializedTemplate.get<ClassTemplateDecl*>(); 1527 } 1528 1529 /// \brief Retrieve the class template or class template partial 1530 /// specialization which was specialized by this. 1531 llvm::PointerUnion<ClassTemplateDecl *, 1532 ClassTemplatePartialSpecializationDecl *> 1533 getSpecializedTemplateOrPartial() const { 1534 if (SpecializedPartialSpecialization *PartialSpec 1535 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1536 return PartialSpec->PartialSpecialization; 1537 1538 return SpecializedTemplate.get<ClassTemplateDecl*>(); 1539 } 1540 1541 /// \brief Retrieve the set of template arguments that should be used 1542 /// to instantiate members of the class template or class template partial 1543 /// specialization from which this class template specialization was 1544 /// instantiated. 1545 /// 1546 /// \returns For a class template specialization instantiated from the primary 1547 /// template, this function will return the same template arguments as 1548 /// getTemplateArgs(). For a class template specialization instantiated from 1549 /// a class template partial specialization, this function will return the 1550 /// deduced template arguments for the class template partial specialization 1551 /// itself. 1552 const TemplateArgumentList &getTemplateInstantiationArgs() const { 1553 if (SpecializedPartialSpecialization *PartialSpec 1554 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1555 return *PartialSpec->TemplateArgs; 1556 1557 return getTemplateArgs(); 1558 } 1559 1560 /// \brief Note that this class template specialization is actually an 1561 /// instantiation of the given class template partial specialization whose 1562 /// template arguments have been deduced. 1563 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, 1564 TemplateArgumentList *TemplateArgs) { 1565 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && 1566 "Already set to a class template partial specialization!"); 1567 SpecializedPartialSpecialization *PS 1568 = new (getASTContext()) SpecializedPartialSpecialization(); 1569 PS->PartialSpecialization = PartialSpec; 1570 PS->TemplateArgs = TemplateArgs; 1571 SpecializedTemplate = PS; 1572 } 1573 1574 /// \brief Note that this class template specialization is an instantiation 1575 /// of the given class template. 1576 void setInstantiationOf(ClassTemplateDecl *TemplDecl) { 1577 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && 1578 "Previously set to a class template partial specialization!"); 1579 SpecializedTemplate = TemplDecl; 1580 } 1581 1582 /// \brief Sets the type of this specialization as it was written by 1583 /// the user. This will be a class template specialization type. 1584 void setTypeAsWritten(TypeSourceInfo *T) { 1585 if (!ExplicitInfo) 1586 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1587 ExplicitInfo->TypeAsWritten = T; 1588 } 1589 /// \brief Gets the type of this specialization as it was written by 1590 /// the user, if it was so written. 1591 TypeSourceInfo *getTypeAsWritten() const { 1592 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0; 1593 } 1594 1595 /// \brief Gets the location of the extern keyword, if present. 1596 SourceLocation getExternLoc() const { 1597 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); 1598 } 1599 /// \brief Sets the location of the extern keyword. 1600 void setExternLoc(SourceLocation Loc) { 1601 if (!ExplicitInfo) 1602 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1603 ExplicitInfo->ExternLoc = Loc; 1604 } 1605 1606 /// \brief Sets the location of the template keyword. 1607 void setTemplateKeywordLoc(SourceLocation Loc) { 1608 if (!ExplicitInfo) 1609 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1610 ExplicitInfo->TemplateKeywordLoc = Loc; 1611 } 1612 /// \brief Gets the location of the template keyword, if present. 1613 SourceLocation getTemplateKeywordLoc() const { 1614 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); 1615 } 1616 1617 SourceRange getSourceRange() const LLVM_READONLY; 1618 1619 void Profile(llvm::FoldingSetNodeID &ID) const { 1620 Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext()); 1621 } 1622 1623 static void 1624 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs, 1625 unsigned NumTemplateArgs, ASTContext &Context) { 1626 ID.AddInteger(NumTemplateArgs); 1627 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 1628 TemplateArgs[Arg].Profile(ID, Context); 1629 } 1630 1631 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1632 static bool classofKind(Kind K) { 1633 return K >= firstClassTemplateSpecialization && 1634 K <= lastClassTemplateSpecialization; 1635 } 1636 1637 friend class ASTDeclReader; 1638 friend class ASTDeclWriter; 1639}; 1640 1641class ClassTemplatePartialSpecializationDecl 1642 : public ClassTemplateSpecializationDecl { 1643 virtual void anchor(); 1644 1645 /// \brief The list of template parameters 1646 TemplateParameterList* TemplateParams; 1647 1648 /// \brief The source info for the template arguments as written. 1649 /// FIXME: redundant with TypeAsWritten? 1650 TemplateArgumentLoc *ArgsAsWritten; 1651 unsigned NumArgsAsWritten; 1652 1653 /// \brief Sequence number indicating when this class template partial 1654 /// specialization was added to the set of partial specializations for 1655 /// its owning class template. 1656 unsigned SequenceNumber; 1657 1658 /// \brief The class template partial specialization from which this 1659 /// class template partial specialization was instantiated. 1660 /// 1661 /// The boolean value will be true to indicate that this class template 1662 /// partial specialization was specialized at this level. 1663 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool> 1664 InstantiatedFromMember; 1665 1666 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, 1667 DeclContext *DC, 1668 SourceLocation StartLoc, 1669 SourceLocation IdLoc, 1670 TemplateParameterList *Params, 1671 ClassTemplateDecl *SpecializedTemplate, 1672 const TemplateArgument *Args, 1673 unsigned NumArgs, 1674 TemplateArgumentLoc *ArgInfos, 1675 unsigned NumArgInfos, 1676 ClassTemplatePartialSpecializationDecl *PrevDecl, 1677 unsigned SequenceNumber); 1678 1679 ClassTemplatePartialSpecializationDecl() 1680 : ClassTemplateSpecializationDecl(ClassTemplatePartialSpecialization), 1681 TemplateParams(0), ArgsAsWritten(0), 1682 NumArgsAsWritten(0), SequenceNumber(0), 1683 InstantiatedFromMember(0, false) { } 1684 1685public: 1686 static ClassTemplatePartialSpecializationDecl * 1687 Create(ASTContext &Context, TagKind TK, DeclContext *DC, 1688 SourceLocation StartLoc, SourceLocation IdLoc, 1689 TemplateParameterList *Params, 1690 ClassTemplateDecl *SpecializedTemplate, 1691 const TemplateArgument *Args, 1692 unsigned NumArgs, 1693 const TemplateArgumentListInfo &ArgInfos, 1694 QualType CanonInjectedType, 1695 ClassTemplatePartialSpecializationDecl *PrevDecl, 1696 unsigned SequenceNumber); 1697 1698 static ClassTemplatePartialSpecializationDecl * 1699 CreateDeserialized(ASTContext &C, unsigned ID); 1700 1701 ClassTemplatePartialSpecializationDecl *getMostRecentDecl() { 1702 return cast<ClassTemplatePartialSpecializationDecl>( 1703 ClassTemplateSpecializationDecl::getMostRecentDecl()); 1704 } 1705 1706 /// Get the list of template parameters 1707 TemplateParameterList *getTemplateParameters() const { 1708 return TemplateParams; 1709 } 1710 1711 /// Get the template arguments as written. 1712 TemplateArgumentLoc *getTemplateArgsAsWritten() const { 1713 return ArgsAsWritten; 1714 } 1715 1716 /// Get the number of template arguments as written. 1717 unsigned getNumTemplateArgsAsWritten() const { 1718 return NumArgsAsWritten; 1719 } 1720 1721 /// \brief Get the sequence number for this class template partial 1722 /// specialization. Internal, only valid for specializations which 1723 /// are in the specialized class template's folding set. 1724 unsigned getSequenceNumber() const { return SequenceNumber; } 1725 1726 /// \brief Retrieve the member class template partial specialization from 1727 /// which this particular class template partial specialization was 1728 /// instantiated. 1729 /// 1730 /// \code 1731 /// template<typename T> 1732 /// struct Outer { 1733 /// template<typename U> struct Inner; 1734 /// template<typename U> struct Inner<U*> { }; // #1 1735 /// }; 1736 /// 1737 /// Outer<float>::Inner<int*> ii; 1738 /// \endcode 1739 /// 1740 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will 1741 /// end up instantiating the partial specialization 1742 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class 1743 /// template partial specialization \c Outer<T>::Inner<U*>. Given 1744 /// \c Outer<float>::Inner<U*>, this function would return 1745 /// \c Outer<T>::Inner<U*>. 1746 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() { 1747 ClassTemplatePartialSpecializationDecl *First 1748 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration()); 1749 return First->InstantiatedFromMember.getPointer(); 1750 } 1751 1752 void setInstantiatedFromMember( 1753 ClassTemplatePartialSpecializationDecl *PartialSpec) { 1754 ClassTemplatePartialSpecializationDecl *First 1755 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration()); 1756 First->InstantiatedFromMember.setPointer(PartialSpec); 1757 } 1758 1759 /// \brief Determines whether this class template partial specialization 1760 /// template was a specialization of a member partial specialization. 1761 /// 1762 /// In the following example, the member template partial specialization 1763 /// \c X<int>::Inner<T*> is a member specialization. 1764 /// 1765 /// \code 1766 /// template<typename T> 1767 /// struct X { 1768 /// template<typename U> struct Inner; 1769 /// template<typename U> struct Inner<U*>; 1770 /// }; 1771 /// 1772 /// template<> template<typename T> 1773 /// struct X<int>::Inner<T*> { /* ... */ }; 1774 /// \endcode 1775 bool isMemberSpecialization() { 1776 ClassTemplatePartialSpecializationDecl *First 1777 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration()); 1778 return First->InstantiatedFromMember.getInt(); 1779 } 1780 1781 /// \brief Note that this member template is a specialization. 1782 void setMemberSpecialization() { 1783 ClassTemplatePartialSpecializationDecl *First 1784 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration()); 1785 assert(First->InstantiatedFromMember.getPointer() && 1786 "Only member templates can be member template specializations"); 1787 return First->InstantiatedFromMember.setInt(true); 1788 } 1789 1790 /// Retrieves the injected specialization type for this partial 1791 /// specialization. This is not the same as the type-decl-type for 1792 /// this partial specialization, which is an InjectedClassNameType. 1793 QualType getInjectedSpecializationType() const { 1794 assert(getTypeForDecl() && "partial specialization has no type set!"); 1795 return cast<InjectedClassNameType>(getTypeForDecl()) 1796 ->getInjectedSpecializationType(); 1797 } 1798 1799 // FIXME: Add Profile support! 1800 1801 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1802 static bool classofKind(Kind K) { 1803 return K == ClassTemplatePartialSpecialization; 1804 } 1805 1806 friend class ASTDeclReader; 1807 friend class ASTDeclWriter; 1808}; 1809 1810/// Declaration of a class template. 1811class ClassTemplateDecl : public RedeclarableTemplateDecl { 1812 static void DeallocateCommon(void *Ptr); 1813 1814protected: 1815 /// \brief Data that is common to all of the declarations of a given 1816 /// class template. 1817 struct Common : CommonBase { 1818 Common() : LazySpecializations() { } 1819 1820 /// \brief The class template specializations for this class 1821 /// template, including explicit specializations and instantiations. 1822 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations; 1823 1824 /// \brief The class template partial specializations for this class 1825 /// template. 1826 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> 1827 PartialSpecializations; 1828 1829 /// \brief The injected-class-name type for this class template. 1830 QualType InjectedClassNameType; 1831 1832 /// \brief If non-null, points to an array of specializations (including 1833 /// partial specializations) known only by their external declaration IDs. 1834 /// 1835 /// The first value in the array is the number of of specializations/ 1836 /// partial specializations that follow. 1837 uint32_t *LazySpecializations; 1838 }; 1839 1840 /// \brief Load any lazily-loaded specializations from the external source. 1841 void LoadLazySpecializations() const; 1842 1843 /// \brief Retrieve the set of specializations of this class template. 1844 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & 1845 getSpecializations() const; 1846 1847 /// \brief Retrieve the set of partial specializations of this class 1848 /// template. 1849 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & 1850 getPartialSpecializations(); 1851 1852 ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 1853 TemplateParameterList *Params, NamedDecl *Decl) 1854 : RedeclarableTemplateDecl(ClassTemplate, DC, L, Name, Params, Decl) { } 1855 1856 ClassTemplateDecl(EmptyShell Empty) 1857 : RedeclarableTemplateDecl(ClassTemplate, 0, SourceLocation(), 1858 DeclarationName(), 0, 0) { } 1859 1860 CommonBase *newCommon(ASTContext &C) const; 1861 1862 Common *getCommonPtr() const { 1863 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 1864 } 1865 1866public: 1867 /// \brief Get the underlying class declarations of the template. 1868 CXXRecordDecl *getTemplatedDecl() const { 1869 return static_cast<CXXRecordDecl *>(TemplatedDecl); 1870 } 1871 1872 /// \brief Returns whether this template declaration defines the primary 1873 /// class pattern. 1874 bool isThisDeclarationADefinition() const { 1875 return getTemplatedDecl()->isThisDeclarationADefinition(); 1876 } 1877 1878 /// \brief Create a class template node. 1879 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC, 1880 SourceLocation L, 1881 DeclarationName Name, 1882 TemplateParameterList *Params, 1883 NamedDecl *Decl, 1884 ClassTemplateDecl *PrevDecl); 1885 1886 /// \brief Create an empty class template node. 1887 static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1888 1889 /// \brief Return the specialization with the provided arguments if it exists, 1890 /// otherwise return the insertion point. 1891 ClassTemplateSpecializationDecl * 1892 findSpecialization(const TemplateArgument *Args, unsigned NumArgs, 1893 void *&InsertPos); 1894 1895 /// \brief Insert the specified specialization knowing that it is not already 1896 /// in. InsertPos must be obtained from findSpecialization. 1897 void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos); 1898 1899 ClassTemplateDecl *getCanonicalDecl() { 1900 return cast<ClassTemplateDecl>( 1901 RedeclarableTemplateDecl::getCanonicalDecl()); 1902 } 1903 const ClassTemplateDecl *getCanonicalDecl() const { 1904 return cast<ClassTemplateDecl>( 1905 RedeclarableTemplateDecl::getCanonicalDecl()); 1906 } 1907 1908 /// \brief Retrieve the previous declaration of this class template, or 1909 /// NULL if no such declaration exists. 1910 ClassTemplateDecl *getPreviousDecl() { 1911 return cast_or_null<ClassTemplateDecl>( 1912 RedeclarableTemplateDecl::getPreviousDecl()); 1913 } 1914 1915 /// \brief Retrieve the previous declaration of this class template, or 1916 /// NULL if no such declaration exists. 1917 const ClassTemplateDecl *getPreviousDecl() const { 1918 return cast_or_null<ClassTemplateDecl>( 1919 RedeclarableTemplateDecl::getPreviousDecl()); 1920 } 1921 1922 ClassTemplateDecl *getInstantiatedFromMemberTemplate() { 1923 return cast_or_null<ClassTemplateDecl>( 1924 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 1925 } 1926 1927 /// \brief Return the partial specialization with the provided arguments if it 1928 /// exists, otherwise return the insertion point. 1929 ClassTemplatePartialSpecializationDecl * 1930 findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs, 1931 void *&InsertPos); 1932 1933 /// \brief Insert the specified partial specialization knowing that it is not 1934 /// already in. InsertPos must be obtained from findPartialSpecialization. 1935 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, 1936 void *InsertPos); 1937 1938 /// \brief Return the next partial specialization sequence number. 1939 unsigned getNextPartialSpecSequenceNumber() { 1940 // Do not load lazy specializations here. They get numbered as they are 1941 // loaded. 1942 return getCommonPtr()->PartialSpecializations.size(); 1943 } 1944 1945 /// \brief Retrieve the partial specializations as an ordered list. 1946 void getPartialSpecializations( 1947 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS); 1948 1949 /// \brief Find a class template partial specialization with the given 1950 /// type T. 1951 /// 1952 /// \param T a dependent type that names a specialization of this class 1953 /// template. 1954 /// 1955 /// \returns the class template partial specialization that exactly matches 1956 /// the type \p T, or NULL if no such partial specialization exists. 1957 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T); 1958 1959 /// \brief Find a class template partial specialization which was instantiated 1960 /// from the given member partial specialization. 1961 /// 1962 /// \param D a member class template partial specialization. 1963 /// 1964 /// \returns the class template partial specialization which was instantiated 1965 /// from the given member partial specialization, or NULL if no such partial 1966 /// specialization exists. 1967 ClassTemplatePartialSpecializationDecl * 1968 findPartialSpecInstantiatedFromMember( 1969 ClassTemplatePartialSpecializationDecl *D); 1970 1971 /// \brief Retrieve the template specialization type of the 1972 /// injected-class-name for this class template. 1973 /// 1974 /// The injected-class-name for a class template \c X is \c 1975 /// X<template-args>, where \c template-args is formed from the 1976 /// template arguments that correspond to the template parameters of 1977 /// \c X. For example: 1978 /// 1979 /// \code 1980 /// template<typename T, int N> 1981 /// struct array { 1982 /// typedef array this_type; // "array" is equivalent to "array<T, N>" 1983 /// }; 1984 /// \endcode 1985 QualType getInjectedClassNameSpecialization(); 1986 1987 typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator; 1988 1989 spec_iterator spec_begin() const { 1990 return makeSpecIterator(getSpecializations(), false); 1991 } 1992 1993 spec_iterator spec_end() const { 1994 return makeSpecIterator(getSpecializations(), true); 1995 } 1996 1997 typedef SpecIterator<ClassTemplatePartialSpecializationDecl> 1998 partial_spec_iterator; 1999 2000 partial_spec_iterator partial_spec_begin() { 2001 return makeSpecIterator(getPartialSpecializations(), false); 2002 } 2003 2004 partial_spec_iterator partial_spec_end() { 2005 return makeSpecIterator(getPartialSpecializations(), true); 2006 } 2007 2008 // Implement isa/cast/dyncast support 2009 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2010 static bool classofKind(Kind K) { return K == ClassTemplate; } 2011 2012 friend class ASTDeclReader; 2013 friend class ASTDeclWriter; 2014}; 2015 2016/// \brief Declaration of a friend template. 2017/// 2018/// For example: 2019/// \code 2020/// template \<typename T> class A { 2021/// friend class MyVector<T>; // not a friend template 2022/// template \<typename U> friend class B; // not a friend template 2023/// template \<typename U> friend class Foo<T>::Nested; // friend template 2024/// }; 2025/// \endcode 2026/// 2027/// \note This class is not currently in use. All of the above 2028/// will yield a FriendDecl, not a FriendTemplateDecl. 2029class FriendTemplateDecl : public Decl { 2030 virtual void anchor(); 2031public: 2032 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion; 2033 2034private: 2035 // The number of template parameters; always non-zero. 2036 unsigned NumParams; 2037 2038 // The parameter list. 2039 TemplateParameterList **Params; 2040 2041 // The declaration that's a friend of this class. 2042 FriendUnion Friend; 2043 2044 // Location of the 'friend' specifier. 2045 SourceLocation FriendLoc; 2046 2047 2048 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc, 2049 unsigned NParams, 2050 TemplateParameterList **Params, 2051 FriendUnion Friend, 2052 SourceLocation FriendLoc) 2053 : Decl(Decl::FriendTemplate, DC, Loc), 2054 NumParams(NParams), 2055 Params(Params), 2056 Friend(Friend), 2057 FriendLoc(FriendLoc) 2058 {} 2059 2060 FriendTemplateDecl(EmptyShell Empty) 2061 : Decl(Decl::FriendTemplate, Empty), 2062 NumParams(0), 2063 Params(0) 2064 {} 2065 2066public: 2067 static FriendTemplateDecl *Create(ASTContext &Context, 2068 DeclContext *DC, SourceLocation Loc, 2069 unsigned NParams, 2070 TemplateParameterList **Params, 2071 FriendUnion Friend, 2072 SourceLocation FriendLoc); 2073 2074 static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2075 2076 /// If this friend declaration names a templated type (or 2077 /// a dependent member type of a templated type), return that 2078 /// type; otherwise return null. 2079 TypeSourceInfo *getFriendType() const { 2080 return Friend.dyn_cast<TypeSourceInfo*>(); 2081 } 2082 2083 /// If this friend declaration names a templated function (or 2084 /// a member function of a templated type), return that type; 2085 /// otherwise return null. 2086 NamedDecl *getFriendDecl() const { 2087 return Friend.dyn_cast<NamedDecl*>(); 2088 } 2089 2090 /// \brief Retrieves the location of the 'friend' keyword. 2091 SourceLocation getFriendLoc() const { 2092 return FriendLoc; 2093 } 2094 2095 TemplateParameterList *getTemplateParameterList(unsigned i) const { 2096 assert(i <= NumParams); 2097 return Params[i]; 2098 } 2099 2100 unsigned getNumTemplateParameters() const { 2101 return NumParams; 2102 } 2103 2104 // Implement isa/cast/dyncast/etc. 2105 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2106 static bool classofKind(Kind K) { return K == Decl::FriendTemplate; } 2107 2108 friend class ASTDeclReader; 2109}; 2110 2111/// \brief Declaration of an alias template. 2112/// 2113/// For example: 2114/// \code 2115/// template \<typename T> using V = std::map<T*, int, MyCompare<T>>; 2116/// \endcode 2117class TypeAliasTemplateDecl : public RedeclarableTemplateDecl { 2118 static void DeallocateCommon(void *Ptr); 2119 2120protected: 2121 typedef CommonBase Common; 2122 2123 TypeAliasTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 2124 TemplateParameterList *Params, NamedDecl *Decl) 2125 : RedeclarableTemplateDecl(TypeAliasTemplate, DC, L, Name, Params, Decl) { } 2126 2127 CommonBase *newCommon(ASTContext &C) const; 2128 2129 Common *getCommonPtr() { 2130 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 2131 } 2132 2133public: 2134 /// Get the underlying function declaration of the template. 2135 TypeAliasDecl *getTemplatedDecl() const { 2136 return static_cast<TypeAliasDecl*>(TemplatedDecl); 2137 } 2138 2139 2140 TypeAliasTemplateDecl *getCanonicalDecl() { 2141 return cast<TypeAliasTemplateDecl>( 2142 RedeclarableTemplateDecl::getCanonicalDecl()); 2143 } 2144 const TypeAliasTemplateDecl *getCanonicalDecl() const { 2145 return cast<TypeAliasTemplateDecl>( 2146 RedeclarableTemplateDecl::getCanonicalDecl()); 2147 } 2148 2149 /// \brief Retrieve the previous declaration of this function template, or 2150 /// NULL if no such declaration exists. 2151 TypeAliasTemplateDecl *getPreviousDecl() { 2152 return cast_or_null<TypeAliasTemplateDecl>( 2153 RedeclarableTemplateDecl::getPreviousDecl()); 2154 } 2155 2156 /// \brief Retrieve the previous declaration of this function template, or 2157 /// NULL if no such declaration exists. 2158 const TypeAliasTemplateDecl *getPreviousDecl() const { 2159 return cast_or_null<TypeAliasTemplateDecl>( 2160 RedeclarableTemplateDecl::getPreviousDecl()); 2161 } 2162 2163 TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() { 2164 return cast_or_null<TypeAliasTemplateDecl>( 2165 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 2166 } 2167 2168 2169 /// \brief Create a function template node. 2170 static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC, 2171 SourceLocation L, 2172 DeclarationName Name, 2173 TemplateParameterList *Params, 2174 NamedDecl *Decl); 2175 2176 /// \brief Create an empty alias template node. 2177 static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2178 2179 // Implement isa/cast/dyncast support 2180 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2181 static bool classofKind(Kind K) { return K == TypeAliasTemplate; } 2182 2183 friend class ASTDeclReader; 2184 friend class ASTDeclWriter; 2185}; 2186 2187/// \brief Declaration of a function specialization at template class scope. 2188/// 2189/// This is a non standard extension needed to support MSVC. 2190/// 2191/// For example: 2192/// \code 2193/// template <class T> 2194/// class A { 2195/// template <class U> void foo(U a) { } 2196/// template<> void foo(int a) { } 2197/// } 2198/// \endcode 2199/// 2200/// "template<> foo(int a)" will be saved in Specialization as a normal 2201/// CXXMethodDecl. Then during an instantiation of class A, it will be 2202/// transformed into an actual function specialization. 2203class ClassScopeFunctionSpecializationDecl : public Decl { 2204 virtual void anchor(); 2205 2206 ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc, 2207 CXXMethodDecl *FD, bool Args, 2208 TemplateArgumentListInfo TemplArgs) 2209 : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc), 2210 Specialization(FD), HasExplicitTemplateArgs(Args), 2211 TemplateArgs(TemplArgs) {} 2212 2213 ClassScopeFunctionSpecializationDecl(EmptyShell Empty) 2214 : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {} 2215 2216 CXXMethodDecl *Specialization; 2217 bool HasExplicitTemplateArgs; 2218 TemplateArgumentListInfo TemplateArgs; 2219 2220public: 2221 CXXMethodDecl *getSpecialization() const { return Specialization; } 2222 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 2223 const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; } 2224 2225 static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C, 2226 DeclContext *DC, 2227 SourceLocation Loc, 2228 CXXMethodDecl *FD, 2229 bool HasExplicitTemplateArgs, 2230 TemplateArgumentListInfo TemplateArgs) { 2231 return new (C) ClassScopeFunctionSpecializationDecl(DC , Loc, FD, 2232 HasExplicitTemplateArgs, 2233 TemplateArgs); 2234 } 2235 2236 static ClassScopeFunctionSpecializationDecl * 2237 CreateDeserialized(ASTContext &Context, unsigned ID); 2238 2239 // Implement isa/cast/dyncast/etc. 2240 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2241 static bool classofKind(Kind K) { 2242 return K == Decl::ClassScopeFunctionSpecialization; 2243 } 2244 2245 friend class ASTDeclReader; 2246 friend class ASTDeclWriter; 2247}; 2248 2249/// Implementation of inline functions that require the template declarations 2250inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD) 2251 : Function(FTD) { } 2252 2253} /* end of namespace clang */ 2254 2255#endif 2256