DeclTemplate.h revision 87bcee88d9b49de8214aa23d07c96f7bec3198e0
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 RedeclarableTemplateDecl::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 RedeclarableTemplateDecl::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 = CXXRecordDecl::getMostRecentDecl(); 1453 while (!isa<ClassTemplateSpecializationDecl>(Recent)) { 1454 // FIXME: Does injected class name need to be in the redeclarations chain? 1455 assert(Recent->isInjectedClassName() && Recent->getPreviousDecl()); 1456 Recent = Recent->getPreviousDecl(); 1457 } 1458 return cast<ClassTemplateSpecializationDecl>(Recent); 1459 } 1460 1461 /// \brief Retrieve the template that this specialization specializes. 1462 ClassTemplateDecl *getSpecializedTemplate() const; 1463 1464 /// \brief Retrieve the template arguments of the class template 1465 /// specialization. 1466 const TemplateArgumentList &getTemplateArgs() const { 1467 return *TemplateArgs; 1468 } 1469 1470 /// \brief Determine the kind of specialization that this 1471 /// declaration represents. 1472 TemplateSpecializationKind getSpecializationKind() const { 1473 return static_cast<TemplateSpecializationKind>(SpecializationKind); 1474 } 1475 1476 bool isExplicitSpecialization() const { 1477 return getSpecializationKind() == TSK_ExplicitSpecialization; 1478 } 1479 1480 /// \brief True if this declaration is an explicit specialization, 1481 /// explicit instantiation declaration, or explicit instantiation 1482 /// definition. 1483 bool isExplicitInstantiationOrSpecialization() const { 1484 switch (getTemplateSpecializationKind()) { 1485 case TSK_ExplicitSpecialization: 1486 case TSK_ExplicitInstantiationDeclaration: 1487 case TSK_ExplicitInstantiationDefinition: 1488 return true; 1489 1490 case TSK_Undeclared: 1491 case TSK_ImplicitInstantiation: 1492 return false; 1493 } 1494 llvm_unreachable("bad template specialization kind"); 1495 } 1496 1497 void setSpecializationKind(TemplateSpecializationKind TSK) { 1498 SpecializationKind = TSK; 1499 } 1500 1501 /// \brief Get the point of instantiation (if any), or null if none. 1502 SourceLocation getPointOfInstantiation() const { 1503 return PointOfInstantiation; 1504 } 1505 1506 void setPointOfInstantiation(SourceLocation Loc) { 1507 assert(Loc.isValid() && "point of instantiation must be valid!"); 1508 PointOfInstantiation = Loc; 1509 } 1510 1511 /// \brief If this class template specialization is an instantiation of 1512 /// a template (rather than an explicit specialization), return the 1513 /// class template or class template partial specialization from which it 1514 /// was instantiated. 1515 llvm::PointerUnion<ClassTemplateDecl *, 1516 ClassTemplatePartialSpecializationDecl *> 1517 getInstantiatedFrom() const { 1518 if (getSpecializationKind() != TSK_ImplicitInstantiation && 1519 getSpecializationKind() != TSK_ExplicitInstantiationDefinition && 1520 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration) 1521 return llvm::PointerUnion<ClassTemplateDecl *, 1522 ClassTemplatePartialSpecializationDecl *>(); 1523 1524 if (SpecializedPartialSpecialization *PartialSpec 1525 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1526 return PartialSpec->PartialSpecialization; 1527 1528 return SpecializedTemplate.get<ClassTemplateDecl*>(); 1529 } 1530 1531 /// \brief Retrieve the class template or class template partial 1532 /// specialization which was specialized by this. 1533 llvm::PointerUnion<ClassTemplateDecl *, 1534 ClassTemplatePartialSpecializationDecl *> 1535 getSpecializedTemplateOrPartial() const { 1536 if (SpecializedPartialSpecialization *PartialSpec 1537 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1538 return PartialSpec->PartialSpecialization; 1539 1540 return SpecializedTemplate.get<ClassTemplateDecl*>(); 1541 } 1542 1543 /// \brief Retrieve the set of template arguments that should be used 1544 /// to instantiate members of the class template or class template partial 1545 /// specialization from which this class template specialization was 1546 /// instantiated. 1547 /// 1548 /// \returns For a class template specialization instantiated from the primary 1549 /// template, this function will return the same template arguments as 1550 /// getTemplateArgs(). For a class template specialization instantiated from 1551 /// a class template partial specialization, this function will return the 1552 /// deduced template arguments for the class template partial specialization 1553 /// itself. 1554 const TemplateArgumentList &getTemplateInstantiationArgs() const { 1555 if (SpecializedPartialSpecialization *PartialSpec 1556 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1557 return *PartialSpec->TemplateArgs; 1558 1559 return getTemplateArgs(); 1560 } 1561 1562 /// \brief Note that this class template specialization is actually an 1563 /// instantiation of the given class template partial specialization whose 1564 /// template arguments have been deduced. 1565 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, 1566 const TemplateArgumentList *TemplateArgs) { 1567 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && 1568 "Already set to a class template partial specialization!"); 1569 SpecializedPartialSpecialization *PS 1570 = new (getASTContext()) SpecializedPartialSpecialization(); 1571 PS->PartialSpecialization = PartialSpec; 1572 PS->TemplateArgs = TemplateArgs; 1573 SpecializedTemplate = PS; 1574 } 1575 1576 /// \brief Note that this class template specialization is an instantiation 1577 /// of the given class template. 1578 void setInstantiationOf(ClassTemplateDecl *TemplDecl) { 1579 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && 1580 "Previously set to a class template partial specialization!"); 1581 SpecializedTemplate = TemplDecl; 1582 } 1583 1584 /// \brief Sets the type of this specialization as it was written by 1585 /// the user. This will be a class template specialization type. 1586 void setTypeAsWritten(TypeSourceInfo *T) { 1587 if (!ExplicitInfo) 1588 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1589 ExplicitInfo->TypeAsWritten = T; 1590 } 1591 /// \brief Gets the type of this specialization as it was written by 1592 /// the user, if it was so written. 1593 TypeSourceInfo *getTypeAsWritten() const { 1594 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0; 1595 } 1596 1597 /// \brief Gets the location of the extern keyword, if present. 1598 SourceLocation getExternLoc() const { 1599 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); 1600 } 1601 /// \brief Sets the location of the extern keyword. 1602 void setExternLoc(SourceLocation Loc) { 1603 if (!ExplicitInfo) 1604 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1605 ExplicitInfo->ExternLoc = Loc; 1606 } 1607 1608 /// \brief Sets the location of the template keyword. 1609 void setTemplateKeywordLoc(SourceLocation Loc) { 1610 if (!ExplicitInfo) 1611 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 1612 ExplicitInfo->TemplateKeywordLoc = Loc; 1613 } 1614 /// \brief Gets the location of the template keyword, if present. 1615 SourceLocation getTemplateKeywordLoc() const { 1616 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); 1617 } 1618 1619 SourceRange getSourceRange() const LLVM_READONLY; 1620 1621 void Profile(llvm::FoldingSetNodeID &ID) const { 1622 Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext()); 1623 } 1624 1625 static void 1626 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs, 1627 unsigned NumTemplateArgs, ASTContext &Context) { 1628 ID.AddInteger(NumTemplateArgs); 1629 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 1630 TemplateArgs[Arg].Profile(ID, Context); 1631 } 1632 1633 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1634 static bool classofKind(Kind K) { 1635 return K >= firstClassTemplateSpecialization && 1636 K <= lastClassTemplateSpecialization; 1637 } 1638 1639 friend class ASTDeclReader; 1640 friend class ASTDeclWriter; 1641}; 1642 1643class ClassTemplatePartialSpecializationDecl 1644 : public ClassTemplateSpecializationDecl { 1645 virtual void anchor(); 1646 1647 /// \brief The list of template parameters 1648 TemplateParameterList* TemplateParams; 1649 1650 /// \brief The source info for the template arguments as written. 1651 /// FIXME: redundant with TypeAsWritten? 1652 const ASTTemplateArgumentListInfo *ArgsAsWritten; 1653 1654 /// \brief The class template partial specialization from which this 1655 /// class template partial specialization was instantiated. 1656 /// 1657 /// The boolean value will be true to indicate that this class template 1658 /// partial specialization was specialized at this level. 1659 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool> 1660 InstantiatedFromMember; 1661 1662 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, 1663 DeclContext *DC, 1664 SourceLocation StartLoc, 1665 SourceLocation IdLoc, 1666 TemplateParameterList *Params, 1667 ClassTemplateDecl *SpecializedTemplate, 1668 const TemplateArgument *Args, 1669 unsigned NumArgs, 1670 const ASTTemplateArgumentListInfo *ArgsAsWritten, 1671 ClassTemplatePartialSpecializationDecl *PrevDecl); 1672 1673 ClassTemplatePartialSpecializationDecl() 1674 : ClassTemplateSpecializationDecl(ClassTemplatePartialSpecialization), 1675 TemplateParams(0), ArgsAsWritten(0), InstantiatedFromMember(0, false) { } 1676 1677public: 1678 static ClassTemplatePartialSpecializationDecl * 1679 Create(ASTContext &Context, TagKind TK, DeclContext *DC, 1680 SourceLocation StartLoc, SourceLocation IdLoc, 1681 TemplateParameterList *Params, 1682 ClassTemplateDecl *SpecializedTemplate, 1683 const TemplateArgument *Args, 1684 unsigned NumArgs, 1685 const TemplateArgumentListInfo &ArgInfos, 1686 QualType CanonInjectedType, 1687 ClassTemplatePartialSpecializationDecl *PrevDecl); 1688 1689 static ClassTemplatePartialSpecializationDecl * 1690 CreateDeserialized(ASTContext &C, unsigned ID); 1691 1692 ClassTemplatePartialSpecializationDecl *getMostRecentDecl() { 1693 return cast<ClassTemplatePartialSpecializationDecl>( 1694 ClassTemplateSpecializationDecl::getMostRecentDecl()); 1695 } 1696 1697 /// Get the list of template parameters 1698 TemplateParameterList *getTemplateParameters() const { 1699 return TemplateParams; 1700 } 1701 1702 /// Get the template arguments as written. 1703 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { 1704 return ArgsAsWritten; 1705 } 1706 1707 /// \brief Retrieve the member class template partial specialization from 1708 /// which this particular class template partial specialization was 1709 /// instantiated. 1710 /// 1711 /// \code 1712 /// template<typename T> 1713 /// struct Outer { 1714 /// template<typename U> struct Inner; 1715 /// template<typename U> struct Inner<U*> { }; // #1 1716 /// }; 1717 /// 1718 /// Outer<float>::Inner<int*> ii; 1719 /// \endcode 1720 /// 1721 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will 1722 /// end up instantiating the partial specialization 1723 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class 1724 /// template partial specialization \c Outer<T>::Inner<U*>. Given 1725 /// \c Outer<float>::Inner<U*>, this function would return 1726 /// \c Outer<T>::Inner<U*>. 1727 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() { 1728 ClassTemplatePartialSpecializationDecl *First = 1729 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); 1730 return First->InstantiatedFromMember.getPointer(); 1731 } 1732 1733 void setInstantiatedFromMember( 1734 ClassTemplatePartialSpecializationDecl *PartialSpec) { 1735 ClassTemplatePartialSpecializationDecl *First = 1736 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); 1737 First->InstantiatedFromMember.setPointer(PartialSpec); 1738 } 1739 1740 /// \brief Determines whether this class template partial specialization 1741 /// template was a specialization of a member partial specialization. 1742 /// 1743 /// In the following example, the member template partial specialization 1744 /// \c X<int>::Inner<T*> is a member specialization. 1745 /// 1746 /// \code 1747 /// template<typename T> 1748 /// struct X { 1749 /// template<typename U> struct Inner; 1750 /// template<typename U> struct Inner<U*>; 1751 /// }; 1752 /// 1753 /// template<> template<typename T> 1754 /// struct X<int>::Inner<T*> { /* ... */ }; 1755 /// \endcode 1756 bool isMemberSpecialization() { 1757 ClassTemplatePartialSpecializationDecl *First = 1758 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); 1759 return First->InstantiatedFromMember.getInt(); 1760 } 1761 1762 /// \brief Note that this member template is a specialization. 1763 void setMemberSpecialization() { 1764 ClassTemplatePartialSpecializationDecl *First = 1765 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); 1766 assert(First->InstantiatedFromMember.getPointer() && 1767 "Only member templates can be member template specializations"); 1768 return First->InstantiatedFromMember.setInt(true); 1769 } 1770 1771 /// Retrieves the injected specialization type for this partial 1772 /// specialization. This is not the same as the type-decl-type for 1773 /// this partial specialization, which is an InjectedClassNameType. 1774 QualType getInjectedSpecializationType() const { 1775 assert(getTypeForDecl() && "partial specialization has no type set!"); 1776 return cast<InjectedClassNameType>(getTypeForDecl()) 1777 ->getInjectedSpecializationType(); 1778 } 1779 1780 // FIXME: Add Profile support! 1781 1782 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1783 static bool classofKind(Kind K) { 1784 return K == ClassTemplatePartialSpecialization; 1785 } 1786 1787 friend class ASTDeclReader; 1788 friend class ASTDeclWriter; 1789}; 1790 1791/// Declaration of a class template. 1792class ClassTemplateDecl : public RedeclarableTemplateDecl { 1793 static void DeallocateCommon(void *Ptr); 1794 1795protected: 1796 /// \brief Data that is common to all of the declarations of a given 1797 /// class template. 1798 struct Common : CommonBase { 1799 Common() : LazySpecializations() { } 1800 1801 /// \brief The class template specializations for this class 1802 /// template, including explicit specializations and instantiations. 1803 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations; 1804 1805 /// \brief The class template partial specializations for this class 1806 /// template. 1807 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> 1808 PartialSpecializations; 1809 1810 /// \brief The injected-class-name type for this class template. 1811 QualType InjectedClassNameType; 1812 1813 /// \brief If non-null, points to an array of specializations (including 1814 /// partial specializations) known only by their external declaration IDs. 1815 /// 1816 /// The first value in the array is the number of of specializations/ 1817 /// partial specializations that follow. 1818 uint32_t *LazySpecializations; 1819 }; 1820 1821 /// \brief Load any lazily-loaded specializations from the external source. 1822 void LoadLazySpecializations() const; 1823 1824 /// \brief Retrieve the set of specializations of this class template. 1825 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & 1826 getSpecializations() const; 1827 1828 /// \brief Retrieve the set of partial specializations of this class 1829 /// template. 1830 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & 1831 getPartialSpecializations(); 1832 1833 ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 1834 TemplateParameterList *Params, NamedDecl *Decl) 1835 : RedeclarableTemplateDecl(ClassTemplate, DC, L, Name, Params, Decl) { } 1836 1837 ClassTemplateDecl(EmptyShell Empty) 1838 : RedeclarableTemplateDecl(ClassTemplate, 0, SourceLocation(), 1839 DeclarationName(), 0, 0) { } 1840 1841 CommonBase *newCommon(ASTContext &C) const; 1842 1843 Common *getCommonPtr() const { 1844 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 1845 } 1846 1847public: 1848 /// \brief Get the underlying class declarations of the template. 1849 CXXRecordDecl *getTemplatedDecl() const { 1850 return static_cast<CXXRecordDecl *>(TemplatedDecl); 1851 } 1852 1853 /// \brief Returns whether this template declaration defines the primary 1854 /// class pattern. 1855 bool isThisDeclarationADefinition() const { 1856 return getTemplatedDecl()->isThisDeclarationADefinition(); 1857 } 1858 1859 /// \brief Create a class template node. 1860 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC, 1861 SourceLocation L, 1862 DeclarationName Name, 1863 TemplateParameterList *Params, 1864 NamedDecl *Decl, 1865 ClassTemplateDecl *PrevDecl); 1866 1867 /// \brief Create an empty class template node. 1868 static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1869 1870 /// \brief Return the specialization with the provided arguments if it exists, 1871 /// otherwise return the insertion point. 1872 ClassTemplateSpecializationDecl * 1873 findSpecialization(const TemplateArgument *Args, unsigned NumArgs, 1874 void *&InsertPos); 1875 1876 /// \brief Insert the specified specialization knowing that it is not already 1877 /// in. InsertPos must be obtained from findSpecialization. 1878 void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos); 1879 1880 ClassTemplateDecl *getCanonicalDecl() { 1881 return cast<ClassTemplateDecl>( 1882 RedeclarableTemplateDecl::getCanonicalDecl()); 1883 } 1884 const ClassTemplateDecl *getCanonicalDecl() const { 1885 return cast<ClassTemplateDecl>( 1886 RedeclarableTemplateDecl::getCanonicalDecl()); 1887 } 1888 1889 /// \brief Retrieve the previous declaration of this class template, or 1890 /// NULL if no such declaration exists. 1891 ClassTemplateDecl *getPreviousDecl() { 1892 return cast_or_null<ClassTemplateDecl>( 1893 RedeclarableTemplateDecl::getPreviousDecl()); 1894 } 1895 1896 /// \brief Retrieve the previous declaration of this class template, or 1897 /// NULL if no such declaration exists. 1898 const ClassTemplateDecl *getPreviousDecl() const { 1899 return cast_or_null<ClassTemplateDecl>( 1900 RedeclarableTemplateDecl::getPreviousDecl()); 1901 } 1902 1903 ClassTemplateDecl *getInstantiatedFromMemberTemplate() { 1904 return cast_or_null<ClassTemplateDecl>( 1905 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 1906 } 1907 1908 /// \brief Return the partial specialization with the provided arguments if it 1909 /// exists, otherwise return the insertion point. 1910 ClassTemplatePartialSpecializationDecl * 1911 findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs, 1912 void *&InsertPos); 1913 1914 /// \brief Insert the specified partial specialization knowing that it is not 1915 /// already in. InsertPos must be obtained from findPartialSpecialization. 1916 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, 1917 void *InsertPos); 1918 1919 /// \brief Retrieve the partial specializations as an ordered list. 1920 void getPartialSpecializations( 1921 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS); 1922 1923 /// \brief Find a class template partial specialization with the given 1924 /// type T. 1925 /// 1926 /// \param T a dependent type that names a specialization of this class 1927 /// template. 1928 /// 1929 /// \returns the class template partial specialization that exactly matches 1930 /// the type \p T, or NULL if no such partial specialization exists. 1931 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T); 1932 1933 /// \brief Find a class template partial specialization which was instantiated 1934 /// from the given member partial specialization. 1935 /// 1936 /// \param D a member class template partial specialization. 1937 /// 1938 /// \returns the class template partial specialization which was instantiated 1939 /// from the given member partial specialization, or NULL if no such partial 1940 /// specialization exists. 1941 ClassTemplatePartialSpecializationDecl * 1942 findPartialSpecInstantiatedFromMember( 1943 ClassTemplatePartialSpecializationDecl *D); 1944 1945 /// \brief Retrieve the template specialization type of the 1946 /// injected-class-name for this class template. 1947 /// 1948 /// The injected-class-name for a class template \c X is \c 1949 /// X<template-args>, where \c template-args is formed from the 1950 /// template arguments that correspond to the template parameters of 1951 /// \c X. For example: 1952 /// 1953 /// \code 1954 /// template<typename T, int N> 1955 /// struct array { 1956 /// typedef array this_type; // "array" is equivalent to "array<T, N>" 1957 /// }; 1958 /// \endcode 1959 QualType getInjectedClassNameSpecialization(); 1960 1961 typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator; 1962 1963 spec_iterator spec_begin() const { 1964 return makeSpecIterator(getSpecializations(), false); 1965 } 1966 1967 spec_iterator spec_end() const { 1968 return makeSpecIterator(getSpecializations(), true); 1969 } 1970 1971 typedef SpecIterator<ClassTemplatePartialSpecializationDecl> 1972 partial_spec_iterator; 1973 1974 partial_spec_iterator partial_spec_begin() { 1975 return makeSpecIterator(getPartialSpecializations(), false); 1976 } 1977 1978 partial_spec_iterator partial_spec_end() { 1979 return makeSpecIterator(getPartialSpecializations(), true); 1980 } 1981 1982 // Implement isa/cast/dyncast support 1983 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1984 static bool classofKind(Kind K) { return K == ClassTemplate; } 1985 1986 friend class ASTDeclReader; 1987 friend class ASTDeclWriter; 1988}; 1989 1990/// \brief Declaration of a friend template. 1991/// 1992/// For example: 1993/// \code 1994/// template \<typename T> class A { 1995/// friend class MyVector<T>; // not a friend template 1996/// template \<typename U> friend class B; // not a friend template 1997/// template \<typename U> friend class Foo<T>::Nested; // friend template 1998/// }; 1999/// \endcode 2000/// 2001/// \note This class is not currently in use. All of the above 2002/// will yield a FriendDecl, not a FriendTemplateDecl. 2003class FriendTemplateDecl : public Decl { 2004 virtual void anchor(); 2005public: 2006 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion; 2007 2008private: 2009 // The number of template parameters; always non-zero. 2010 unsigned NumParams; 2011 2012 // The parameter list. 2013 TemplateParameterList **Params; 2014 2015 // The declaration that's a friend of this class. 2016 FriendUnion Friend; 2017 2018 // Location of the 'friend' specifier. 2019 SourceLocation FriendLoc; 2020 2021 2022 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc, 2023 unsigned NParams, 2024 TemplateParameterList **Params, 2025 FriendUnion Friend, 2026 SourceLocation FriendLoc) 2027 : Decl(Decl::FriendTemplate, DC, Loc), 2028 NumParams(NParams), 2029 Params(Params), 2030 Friend(Friend), 2031 FriendLoc(FriendLoc) 2032 {} 2033 2034 FriendTemplateDecl(EmptyShell Empty) 2035 : Decl(Decl::FriendTemplate, Empty), 2036 NumParams(0), 2037 Params(0) 2038 {} 2039 2040public: 2041 static FriendTemplateDecl *Create(ASTContext &Context, 2042 DeclContext *DC, SourceLocation Loc, 2043 unsigned NParams, 2044 TemplateParameterList **Params, 2045 FriendUnion Friend, 2046 SourceLocation FriendLoc); 2047 2048 static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2049 2050 /// If this friend declaration names a templated type (or 2051 /// a dependent member type of a templated type), return that 2052 /// type; otherwise return null. 2053 TypeSourceInfo *getFriendType() const { 2054 return Friend.dyn_cast<TypeSourceInfo*>(); 2055 } 2056 2057 /// If this friend declaration names a templated function (or 2058 /// a member function of a templated type), return that type; 2059 /// otherwise return null. 2060 NamedDecl *getFriendDecl() const { 2061 return Friend.dyn_cast<NamedDecl*>(); 2062 } 2063 2064 /// \brief Retrieves the location of the 'friend' keyword. 2065 SourceLocation getFriendLoc() const { 2066 return FriendLoc; 2067 } 2068 2069 TemplateParameterList *getTemplateParameterList(unsigned i) const { 2070 assert(i <= NumParams); 2071 return Params[i]; 2072 } 2073 2074 unsigned getNumTemplateParameters() const { 2075 return NumParams; 2076 } 2077 2078 // Implement isa/cast/dyncast/etc. 2079 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2080 static bool classofKind(Kind K) { return K == Decl::FriendTemplate; } 2081 2082 friend class ASTDeclReader; 2083}; 2084 2085/// \brief Declaration of an alias template. 2086/// 2087/// For example: 2088/// \code 2089/// template \<typename T> using V = std::map<T*, int, MyCompare<T>>; 2090/// \endcode 2091class TypeAliasTemplateDecl : public RedeclarableTemplateDecl { 2092 static void DeallocateCommon(void *Ptr); 2093 2094protected: 2095 typedef CommonBase Common; 2096 2097 TypeAliasTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 2098 TemplateParameterList *Params, NamedDecl *Decl) 2099 : RedeclarableTemplateDecl(TypeAliasTemplate, DC, L, Name, Params, Decl) { } 2100 2101 CommonBase *newCommon(ASTContext &C) const; 2102 2103 Common *getCommonPtr() { 2104 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 2105 } 2106 2107public: 2108 /// Get the underlying function declaration of the template. 2109 TypeAliasDecl *getTemplatedDecl() const { 2110 return static_cast<TypeAliasDecl*>(TemplatedDecl); 2111 } 2112 2113 2114 TypeAliasTemplateDecl *getCanonicalDecl() { 2115 return cast<TypeAliasTemplateDecl>( 2116 RedeclarableTemplateDecl::getCanonicalDecl()); 2117 } 2118 const TypeAliasTemplateDecl *getCanonicalDecl() const { 2119 return cast<TypeAliasTemplateDecl>( 2120 RedeclarableTemplateDecl::getCanonicalDecl()); 2121 } 2122 2123 /// \brief Retrieve the previous declaration of this function template, or 2124 /// NULL if no such declaration exists. 2125 TypeAliasTemplateDecl *getPreviousDecl() { 2126 return cast_or_null<TypeAliasTemplateDecl>( 2127 RedeclarableTemplateDecl::getPreviousDecl()); 2128 } 2129 2130 /// \brief Retrieve the previous declaration of this function template, or 2131 /// NULL if no such declaration exists. 2132 const TypeAliasTemplateDecl *getPreviousDecl() const { 2133 return cast_or_null<TypeAliasTemplateDecl>( 2134 RedeclarableTemplateDecl::getPreviousDecl()); 2135 } 2136 2137 TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() { 2138 return cast_or_null<TypeAliasTemplateDecl>( 2139 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 2140 } 2141 2142 2143 /// \brief Create a function template node. 2144 static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC, 2145 SourceLocation L, 2146 DeclarationName Name, 2147 TemplateParameterList *Params, 2148 NamedDecl *Decl); 2149 2150 /// \brief Create an empty alias template node. 2151 static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2152 2153 // Implement isa/cast/dyncast support 2154 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2155 static bool classofKind(Kind K) { return K == TypeAliasTemplate; } 2156 2157 friend class ASTDeclReader; 2158 friend class ASTDeclWriter; 2159}; 2160 2161/// \brief Declaration of a function specialization at template class scope. 2162/// 2163/// This is a non standard extension needed to support MSVC. 2164/// 2165/// For example: 2166/// \code 2167/// template <class T> 2168/// class A { 2169/// template <class U> void foo(U a) { } 2170/// template<> void foo(int a) { } 2171/// } 2172/// \endcode 2173/// 2174/// "template<> foo(int a)" will be saved in Specialization as a normal 2175/// CXXMethodDecl. Then during an instantiation of class A, it will be 2176/// transformed into an actual function specialization. 2177class ClassScopeFunctionSpecializationDecl : public Decl { 2178 virtual void anchor(); 2179 2180 ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc, 2181 CXXMethodDecl *FD, bool Args, 2182 TemplateArgumentListInfo TemplArgs) 2183 : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc), 2184 Specialization(FD), HasExplicitTemplateArgs(Args), 2185 TemplateArgs(TemplArgs) {} 2186 2187 ClassScopeFunctionSpecializationDecl(EmptyShell Empty) 2188 : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {} 2189 2190 CXXMethodDecl *Specialization; 2191 bool HasExplicitTemplateArgs; 2192 TemplateArgumentListInfo TemplateArgs; 2193 2194public: 2195 CXXMethodDecl *getSpecialization() const { return Specialization; } 2196 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 2197 const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; } 2198 2199 static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C, 2200 DeclContext *DC, 2201 SourceLocation Loc, 2202 CXXMethodDecl *FD, 2203 bool HasExplicitTemplateArgs, 2204 TemplateArgumentListInfo TemplateArgs) { 2205 return new (C) ClassScopeFunctionSpecializationDecl(DC , Loc, FD, 2206 HasExplicitTemplateArgs, 2207 TemplateArgs); 2208 } 2209 2210 static ClassScopeFunctionSpecializationDecl * 2211 CreateDeserialized(ASTContext &Context, unsigned ID); 2212 2213 // Implement isa/cast/dyncast/etc. 2214 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2215 static bool classofKind(Kind K) { 2216 return K == Decl::ClassScopeFunctionSpecialization; 2217 } 2218 2219 friend class ASTDeclReader; 2220 friend class ASTDeclWriter; 2221}; 2222 2223/// Implementation of inline functions that require the template declarations 2224inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD) 2225 : Function(FTD) { } 2226 2227/// \brief Represents a variable template specialization, which refers to 2228/// a variable template with a given set of template arguments. 2229/// 2230/// Variable template specializations represent both explicit 2231/// specializations of variable templates, as in the example below, and 2232/// implicit instantiations of variable templates. 2233/// 2234/// \code 2235/// template<typename T> constexpr T pi = T(3.1415926535897932385); 2236/// 2237/// template<> 2238/// constexpr float pi<float>; // variable template specialization pi<float> 2239/// \endcode 2240class VarTemplateSpecializationDecl : public VarDecl, 2241 public llvm::FoldingSetNode { 2242 2243 /// \brief Structure that stores information about a variable template 2244 /// specialization that was instantiated from a variable template partial 2245 /// specialization. 2246 struct SpecializedPartialSpecialization { 2247 /// \brief The variable template partial specialization from which this 2248 /// variable template specialization was instantiated. 2249 VarTemplatePartialSpecializationDecl *PartialSpecialization; 2250 2251 /// \brief The template argument list deduced for the variable template 2252 /// partial specialization itself. 2253 const TemplateArgumentList *TemplateArgs; 2254 }; 2255 2256 /// \brief The template that this specialization specializes. 2257 llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *> 2258 SpecializedTemplate; 2259 2260 /// \brief Further info for explicit template specialization/instantiation. 2261 struct ExplicitSpecializationInfo { 2262 /// \brief The type-as-written. 2263 TypeSourceInfo *TypeAsWritten; 2264 /// \brief The location of the extern keyword. 2265 SourceLocation ExternLoc; 2266 /// \brief The location of the template keyword. 2267 SourceLocation TemplateKeywordLoc; 2268 2269 ExplicitSpecializationInfo() 2270 : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {} 2271 }; 2272 2273 /// \brief Further info for explicit template specialization/instantiation. 2274 /// Does not apply to implicit specializations. 2275 ExplicitSpecializationInfo *ExplicitInfo; 2276 2277 /// \brief The template arguments used to describe this specialization. 2278 const TemplateArgumentList *TemplateArgs; 2279 TemplateArgumentListInfo TemplateArgsInfo; 2280 2281 /// \brief The point where this template was instantiated (if any). 2282 SourceLocation PointOfInstantiation; 2283 2284 /// \brief The kind of specialization this declaration refers to. 2285 /// Really a value of type TemplateSpecializationKind. 2286 unsigned SpecializationKind : 3; 2287 2288protected: 2289 VarTemplateSpecializationDecl(ASTContext &Context, Kind DK, DeclContext *DC, 2290 SourceLocation StartLoc, SourceLocation IdLoc, 2291 VarTemplateDecl *SpecializedTemplate, 2292 QualType T, TypeSourceInfo *TInfo, 2293 StorageClass S, const TemplateArgument *Args, 2294 unsigned NumArgs); 2295 2296 explicit VarTemplateSpecializationDecl(Kind DK); 2297 2298public: 2299 static VarTemplateSpecializationDecl * 2300 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 2301 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, 2302 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args, 2303 unsigned NumArgs); 2304 static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C, 2305 unsigned ID); 2306 2307 virtual void getNameForDiagnostic(raw_ostream &OS, 2308 const PrintingPolicy &Policy, 2309 bool Qualified) const; 2310 2311 VarTemplateSpecializationDecl *getMostRecentDecl() { 2312 VarDecl *Recent = VarDecl::getMostRecentDecl(); 2313 return cast<VarTemplateSpecializationDecl>(Recent); 2314 } 2315 2316 /// \brief Retrieve the template that this specialization specializes. 2317 VarTemplateDecl *getSpecializedTemplate() const; 2318 2319 /// \brief Retrieve the template arguments of the variable template 2320 /// specialization. 2321 const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; } 2322 2323 // TODO: Always set this when creating the new specialization? 2324 void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo); 2325 2326 const TemplateArgumentListInfo &getTemplateArgsInfo() const { 2327 return TemplateArgsInfo; 2328 } 2329 2330 /// \brief Determine the kind of specialization that this 2331 /// declaration represents. 2332 TemplateSpecializationKind getSpecializationKind() const { 2333 return static_cast<TemplateSpecializationKind>(SpecializationKind); 2334 } 2335 2336 bool isExplicitSpecialization() const { 2337 return getSpecializationKind() == TSK_ExplicitSpecialization; 2338 } 2339 2340 /// \brief True if this declaration is an explicit specialization, 2341 /// explicit instantiation declaration, or explicit instantiation 2342 /// definition. 2343 bool isExplicitInstantiationOrSpecialization() const { 2344 switch (getTemplateSpecializationKind()) { 2345 case TSK_ExplicitSpecialization: 2346 case TSK_ExplicitInstantiationDeclaration: 2347 case TSK_ExplicitInstantiationDefinition: 2348 return true; 2349 2350 case TSK_Undeclared: 2351 case TSK_ImplicitInstantiation: 2352 return false; 2353 } 2354 llvm_unreachable("bad template specialization kind"); 2355 } 2356 2357 void setSpecializationKind(TemplateSpecializationKind TSK) { 2358 SpecializationKind = TSK; 2359 } 2360 2361 /// \brief Get the point of instantiation (if any), or null if none. 2362 SourceLocation getPointOfInstantiation() const { 2363 return PointOfInstantiation; 2364 } 2365 2366 void setPointOfInstantiation(SourceLocation Loc) { 2367 assert(Loc.isValid() && "point of instantiation must be valid!"); 2368 PointOfInstantiation = Loc; 2369 } 2370 2371 /// \brief If this variable template specialization is an instantiation of 2372 /// a template (rather than an explicit specialization), return the 2373 /// variable template or variable template partial specialization from which 2374 /// it was instantiated. 2375 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> 2376 getInstantiatedFrom() const { 2377 if (getSpecializationKind() != TSK_ImplicitInstantiation && 2378 getSpecializationKind() != TSK_ExplicitInstantiationDefinition && 2379 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration) 2380 return llvm::PointerUnion<VarTemplateDecl *, 2381 VarTemplatePartialSpecializationDecl *>(); 2382 2383 if (SpecializedPartialSpecialization *PartialSpec = 2384 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) 2385 return PartialSpec->PartialSpecialization; 2386 2387 return SpecializedTemplate.get<VarTemplateDecl *>(); 2388 } 2389 2390 /// \brief Retrieve the variable template or variable template partial 2391 /// specialization which was specialized by this. 2392 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> 2393 getSpecializedTemplateOrPartial() const { 2394 if (SpecializedPartialSpecialization *PartialSpec = 2395 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) 2396 return PartialSpec->PartialSpecialization; 2397 2398 return SpecializedTemplate.get<VarTemplateDecl *>(); 2399 } 2400 2401 /// \brief Retrieve the set of template arguments that should be used 2402 /// to instantiate the initializer of the variable template or variable 2403 /// template partial specialization from which this variable template 2404 /// specialization was instantiated. 2405 /// 2406 /// \returns For a variable template specialization instantiated from the 2407 /// primary template, this function will return the same template arguments 2408 /// as getTemplateArgs(). For a variable template specialization instantiated 2409 /// from a variable template partial specialization, this function will the 2410 /// return deduced template arguments for the variable template partial 2411 /// specialization itself. 2412 const TemplateArgumentList &getTemplateInstantiationArgs() const { 2413 if (SpecializedPartialSpecialization *PartialSpec = 2414 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) 2415 return *PartialSpec->TemplateArgs; 2416 2417 return getTemplateArgs(); 2418 } 2419 2420 /// \brief Note that this variable template specialization is actually an 2421 /// instantiation of the given variable template partial specialization whose 2422 /// template arguments have been deduced. 2423 void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec, 2424 const TemplateArgumentList *TemplateArgs) { 2425 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && 2426 "Already set to a variable template partial specialization!"); 2427 SpecializedPartialSpecialization *PS = 2428 new (getASTContext()) SpecializedPartialSpecialization(); 2429 PS->PartialSpecialization = PartialSpec; 2430 PS->TemplateArgs = TemplateArgs; 2431 SpecializedTemplate = PS; 2432 } 2433 2434 /// \brief Note that this variable template specialization is an instantiation 2435 /// of the given variable template. 2436 void setInstantiationOf(VarTemplateDecl *TemplDecl) { 2437 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && 2438 "Previously set to a variable template partial specialization!"); 2439 SpecializedTemplate = TemplDecl; 2440 } 2441 2442 /// \brief Sets the type of this specialization as it was written by 2443 /// the user. 2444 void setTypeAsWritten(TypeSourceInfo *T) { 2445 if (!ExplicitInfo) 2446 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 2447 ExplicitInfo->TypeAsWritten = T; 2448 } 2449 /// \brief Gets the type of this specialization as it was written by 2450 /// the user, if it was so written. 2451 TypeSourceInfo *getTypeAsWritten() const { 2452 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0; 2453 } 2454 2455 /// \brief Gets the location of the extern keyword, if present. 2456 SourceLocation getExternLoc() const { 2457 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); 2458 } 2459 /// \brief Sets the location of the extern keyword. 2460 void setExternLoc(SourceLocation Loc) { 2461 if (!ExplicitInfo) 2462 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 2463 ExplicitInfo->ExternLoc = Loc; 2464 } 2465 2466 /// \brief Sets the location of the template keyword. 2467 void setTemplateKeywordLoc(SourceLocation Loc) { 2468 if (!ExplicitInfo) 2469 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; 2470 ExplicitInfo->TemplateKeywordLoc = Loc; 2471 } 2472 /// \brief Gets the location of the template keyword, if present. 2473 SourceLocation getTemplateKeywordLoc() const { 2474 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); 2475 } 2476 2477 void Profile(llvm::FoldingSetNodeID &ID) const { 2478 Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext()); 2479 } 2480 2481 static void Profile(llvm::FoldingSetNodeID &ID, 2482 const TemplateArgument *TemplateArgs, 2483 unsigned NumTemplateArgs, ASTContext &Context) { 2484 ID.AddInteger(NumTemplateArgs); 2485 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 2486 TemplateArgs[Arg].Profile(ID, Context); 2487 } 2488 2489 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2490 static bool classofKind(Kind K) { 2491 return K >= firstVarTemplateSpecialization && 2492 K <= lastVarTemplateSpecialization; 2493 } 2494 2495 friend class ASTDeclReader; 2496 friend class ASTDeclWriter; 2497}; 2498 2499class VarTemplatePartialSpecializationDecl 2500 : public VarTemplateSpecializationDecl { 2501 virtual void anchor(); 2502 2503 /// \brief The list of template parameters 2504 TemplateParameterList *TemplateParams; 2505 2506 /// \brief The source info for the template arguments as written. 2507 /// FIXME: redundant with TypeAsWritten? 2508 const ASTTemplateArgumentListInfo *ArgsAsWritten; 2509 2510 /// \brief The variable template partial specialization from which this 2511 /// variable template partial specialization was instantiated. 2512 /// 2513 /// The boolean value will be true to indicate that this variable template 2514 /// partial specialization was specialized at this level. 2515 llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool> 2516 InstantiatedFromMember; 2517 2518 VarTemplatePartialSpecializationDecl( 2519 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 2520 SourceLocation IdLoc, TemplateParameterList *Params, 2521 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, 2522 StorageClass S, const TemplateArgument *Args, unsigned NumArgs, 2523 const ASTTemplateArgumentListInfo *ArgInfos); 2524 2525 VarTemplatePartialSpecializationDecl() 2526 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization), 2527 TemplateParams(0), ArgsAsWritten(0), InstantiatedFromMember(0, false) {} 2528 2529public: 2530 static VarTemplatePartialSpecializationDecl * 2531 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 2532 SourceLocation IdLoc, TemplateParameterList *Params, 2533 VarTemplateDecl *SpecializedTemplate, QualType T, 2534 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args, 2535 unsigned NumArgs, const TemplateArgumentListInfo &ArgInfos); 2536 2537 static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C, 2538 unsigned ID); 2539 2540 VarTemplatePartialSpecializationDecl *getMostRecentDecl() { 2541 return cast<VarTemplatePartialSpecializationDecl>( 2542 VarTemplateSpecializationDecl::getMostRecentDecl()); 2543 } 2544 2545 /// Get the list of template parameters 2546 TemplateParameterList *getTemplateParameters() const { 2547 return TemplateParams; 2548 } 2549 2550 /// Get the template arguments as written. 2551 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { 2552 return ArgsAsWritten; 2553 } 2554 2555 /// \brief Retrieve the member variable template partial specialization from 2556 /// which this particular variable template partial specialization was 2557 /// instantiated. 2558 /// 2559 /// \code 2560 /// template<typename T> 2561 /// struct Outer { 2562 /// template<typename U> U Inner; 2563 /// template<typename U> U* Inner<U*> = (U*)(0); // #1 2564 /// }; 2565 /// 2566 /// template int* Outer<float>::Inner<int*>; 2567 /// \endcode 2568 /// 2569 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will 2570 /// end up instantiating the partial specialization 2571 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the 2572 /// variable template partial specialization \c Outer<T>::Inner<U*>. Given 2573 /// \c Outer<float>::Inner<U*>, this function would return 2574 /// \c Outer<T>::Inner<U*>. 2575 VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() { 2576 VarTemplatePartialSpecializationDecl *First = 2577 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); 2578 return First->InstantiatedFromMember.getPointer(); 2579 } 2580 2581 void 2582 setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) { 2583 VarTemplatePartialSpecializationDecl *First = 2584 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); 2585 First->InstantiatedFromMember.setPointer(PartialSpec); 2586 } 2587 2588 /// \brief Determines whether this variable template partial specialization 2589 /// was a specialization of a member partial specialization. 2590 /// 2591 /// In the following example, the member template partial specialization 2592 /// \c X<int>::Inner<T*> is a member specialization. 2593 /// 2594 /// \code 2595 /// template<typename T> 2596 /// struct X { 2597 /// template<typename U> U Inner; 2598 /// template<typename U> U* Inner<U*> = (U*)(0); 2599 /// }; 2600 /// 2601 /// template<> template<typename T> 2602 /// U* X<int>::Inner<T*> = (T*)(0) + 1; 2603 /// \endcode 2604 bool isMemberSpecialization() { 2605 VarTemplatePartialSpecializationDecl *First = 2606 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); 2607 return First->InstantiatedFromMember.getInt(); 2608 } 2609 2610 /// \brief Note that this member template is a specialization. 2611 void setMemberSpecialization() { 2612 VarTemplatePartialSpecializationDecl *First = 2613 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); 2614 assert(First->InstantiatedFromMember.getPointer() && 2615 "Only member templates can be member template specializations"); 2616 return First->InstantiatedFromMember.setInt(true); 2617 } 2618 2619 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2620 static bool classofKind(Kind K) { 2621 return K == VarTemplatePartialSpecialization; 2622 } 2623 2624 friend class ASTDeclReader; 2625 friend class ASTDeclWriter; 2626}; 2627 2628/// Declaration of a variable template. 2629class VarTemplateDecl : public RedeclarableTemplateDecl { 2630 static void DeallocateCommon(void *Ptr); 2631 2632protected: 2633 /// \brief Data that is common to all of the declarations of a given 2634 /// variable template. 2635 struct Common : CommonBase { 2636 Common() : LazySpecializations() {} 2637 2638 /// \brief The variable template specializations for this variable 2639 /// template, including explicit specializations and instantiations. 2640 llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations; 2641 2642 /// \brief The variable template partial specializations for this variable 2643 /// template. 2644 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> 2645 PartialSpecializations; 2646 2647 /// \brief If non-null, points to an array of specializations (including 2648 /// partial specializations) known ownly by their external declaration IDs. 2649 /// 2650 /// The first value in the array is the number of of specializations/ 2651 /// partial specializations that follow. 2652 uint32_t *LazySpecializations; 2653 }; 2654 2655 /// \brief Load any lazily-loaded specializations from the external source. 2656 void LoadLazySpecializations() const; 2657 2658 /// \brief Retrieve the set of specializations of this variable template. 2659 llvm::FoldingSetVector<VarTemplateSpecializationDecl> & 2660 getSpecializations() const; 2661 2662 /// \brief Retrieve the set of partial specializations of this class 2663 /// template. 2664 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & 2665 getPartialSpecializations(); 2666 2667 VarTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 2668 TemplateParameterList *Params, NamedDecl *Decl) 2669 : RedeclarableTemplateDecl(VarTemplate, DC, L, Name, Params, Decl) {} 2670 2671 VarTemplateDecl(EmptyShell Empty) 2672 : RedeclarableTemplateDecl(VarTemplate, 0, SourceLocation(), 2673 DeclarationName(), 0, 0) {} 2674 2675 CommonBase *newCommon(ASTContext &C) const; 2676 2677 Common *getCommonPtr() const { 2678 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); 2679 } 2680 2681public: 2682 /// \brief Get the underlying variable declarations of the template. 2683 VarDecl *getTemplatedDecl() const { 2684 return static_cast<VarDecl *>(TemplatedDecl); 2685 } 2686 2687 /// \brief Returns whether this template declaration defines the primary 2688 /// variable pattern. 2689 bool isThisDeclarationADefinition() const { 2690 return getTemplatedDecl()->isThisDeclarationADefinition(); 2691 } 2692 2693 VarTemplateDecl *getDefinition(); 2694 2695 /// \brief Create a variable template node. 2696 static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC, 2697 SourceLocation L, DeclarationName Name, 2698 TemplateParameterList *Params, NamedDecl *Decl, 2699 VarTemplateDecl *PrevDecl); 2700 2701 /// \brief Create an empty variable template node. 2702 static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2703 2704 /// \brief Return the specialization with the provided arguments if it exists, 2705 /// otherwise return the insertion point. 2706 VarTemplateSpecializationDecl * 2707 findSpecialization(const TemplateArgument *Args, unsigned NumArgs, 2708 void *&InsertPos); 2709 2710 /// \brief Insert the specified specialization knowing that it is not already 2711 /// in. InsertPos must be obtained from findSpecialization. 2712 void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos); 2713 2714 VarTemplateDecl *getCanonicalDecl() { 2715 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); 2716 } 2717 const VarTemplateDecl *getCanonicalDecl() const { 2718 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); 2719 } 2720 2721 /// \brief Retrieve the previous declaration of this variable template, or 2722 /// NULL if no such declaration exists. 2723 VarTemplateDecl *getPreviousDecl() { 2724 return cast_or_null<VarTemplateDecl>( 2725 RedeclarableTemplateDecl::getPreviousDecl()); 2726 } 2727 2728 /// \brief Retrieve the previous declaration of this variable template, or 2729 /// NULL if no such declaration exists. 2730 const VarTemplateDecl *getPreviousDecl() const { 2731 return cast_or_null<VarTemplateDecl>( 2732 RedeclarableTemplateDecl::getPreviousDecl()); 2733 } 2734 2735 VarTemplateDecl *getInstantiatedFromMemberTemplate() { 2736 return cast_or_null<VarTemplateDecl>( 2737 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); 2738 } 2739 2740 /// \brief Return the partial specialization with the provided arguments if it 2741 /// exists, otherwise return the insertion point. 2742 VarTemplatePartialSpecializationDecl * 2743 findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs, 2744 void *&InsertPos); 2745 2746 /// \brief Insert the specified partial specialization knowing that it is not 2747 /// already in. InsertPos must be obtained from findPartialSpecialization. 2748 void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D, 2749 void *InsertPos); 2750 2751 /// \brief Retrieve the partial specializations as an ordered list. 2752 void getPartialSpecializations( 2753 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS); 2754 2755 /// \brief Find a variable template partial specialization which was 2756 /// instantiated 2757 /// from the given member partial specialization. 2758 /// 2759 /// \param D a member variable template partial specialization. 2760 /// 2761 /// \returns the variable template partial specialization which was 2762 /// instantiated 2763 /// from the given member partial specialization, or NULL if no such partial 2764 /// specialization exists. 2765 VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember( 2766 VarTemplatePartialSpecializationDecl *D); 2767 2768 typedef SpecIterator<VarTemplateSpecializationDecl> spec_iterator; 2769 2770 spec_iterator spec_begin() const { 2771 return makeSpecIterator(getSpecializations(), false); 2772 } 2773 2774 spec_iterator spec_end() const { 2775 return makeSpecIterator(getSpecializations(), true); 2776 } 2777 2778 typedef SpecIterator<VarTemplatePartialSpecializationDecl> 2779 partial_spec_iterator; 2780 2781 partial_spec_iterator partial_spec_begin() { 2782 return makeSpecIterator(getPartialSpecializations(), false); 2783 } 2784 2785 partial_spec_iterator partial_spec_end() { 2786 return makeSpecIterator(getPartialSpecializations(), true); 2787 } 2788 2789 // Implement isa/cast/dyncast support 2790 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2791 static bool classofKind(Kind K) { return K == VarTemplate; } 2792 2793 friend class ASTDeclReader; 2794 friend class ASTDeclWriter; 2795}; 2796 2797} /* end of namespace clang */ 2798 2799#endif 2800