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