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