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