DeclTemplate.h revision 198092
1241675Suqs//===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===// 2241675Suqs// 3241675Suqs// The LLVM Compiler Infrastructure 4241675Suqs// 5241675Suqs// This file is distributed under the University of Illinois Open Source 6241675Suqs// License. See LICENSE.TXT for details. 7241675Suqs// 8241675Suqs//===----------------------------------------------------------------------===// 9241675Suqs// 10241675Suqs// This file defines the C++ template declaration subclasses. 11241675Suqs// 12241675Suqs//===----------------------------------------------------------------------===// 13241675Suqs 14241675Suqs#ifndef LLVM_CLANG_AST_DECLTEMPLATE_H 15241675Suqs#define LLVM_CLANG_AST_DECLTEMPLATE_H 16241675Suqs 17241675Suqs#include "clang/AST/DeclCXX.h" 18241675Suqs#include "llvm/ADT/APSInt.h" 19241675Suqs#include "llvm/ADT/FoldingSet.h" 20241675Suqs#include "llvm/ADT/PointerUnion.h" 21241675Suqs#include <limits> 22241675Suqs 23241675Suqsnamespace clang { 24241675Suqs 25241675Suqsclass TemplateParameterList; 26241675Suqsclass TemplateDecl; 27241675Suqsclass FunctionTemplateDecl; 28241675Suqsclass ClassTemplateDecl; 29241675Suqsclass ClassTemplatePartialSpecializationDecl; 30241675Suqsclass TemplateTypeParmDecl; 31241675Suqsclass NonTypeTemplateParmDecl; 32241675Suqsclass TemplateTemplateParmDecl; 33241675Suqs 34241675Suqs/// \brief Stores a template parameter of any kind. 35241675Suqstypedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*, 36241675Suqs TemplateTemplateParmDecl*> TemplateParameter; 37241675Suqs 38241675Suqs/// TemplateParameterList - Stores a list of template parameters for a 39241675Suqs/// TemplateDecl and its derived classes. 40241675Suqsclass TemplateParameterList { 41241675Suqs /// The location of the 'template' keyword. 42241675Suqs SourceLocation TemplateLoc; 43241675Suqs 44241675Suqs /// The locations of the '<' and '>' angle brackets. 45241675Suqs SourceLocation LAngleLoc, RAngleLoc; 46241675Suqs 47241675Suqs /// The number of template parameters in this template 48241675Suqs /// parameter list. 49241675Suqs unsigned NumParams; 50241675Suqs 51241675Suqs TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc, 52241675Suqs NamedDecl **Params, unsigned NumParams, 53241675Suqs SourceLocation RAngleLoc); 54241675Suqs 55241675Suqspublic: 56241675Suqs static TemplateParameterList *Create(ASTContext &C, 57241675Suqs SourceLocation TemplateLoc, 58241675Suqs SourceLocation LAngleLoc, 59241675Suqs NamedDecl **Params, 60241675Suqs unsigned NumParams, 61241675Suqs SourceLocation RAngleLoc); 62241675Suqs 63241675Suqs /// iterator - Iterates through the template parameters in this list. 64241675Suqs typedef NamedDecl** iterator; 65241675Suqs 66241675Suqs /// const_iterator - Iterates through the template parameters in this list. 67241675Suqs typedef NamedDecl* const* const_iterator; 68 69 iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); } 70 const_iterator begin() const { 71 return reinterpret_cast<NamedDecl * const *>(this + 1); 72 } 73 iterator end() { return begin() + NumParams; } 74 const_iterator end() const { return begin() + NumParams; } 75 76 unsigned size() const { return NumParams; } 77 78 NamedDecl* getParam(unsigned Idx) { 79 assert(Idx < size() && "Template parameter index out-of-range"); 80 return begin()[Idx]; 81 } 82 83 const NamedDecl* getParam(unsigned Idx) const { 84 assert(Idx < size() && "Template parameter index out-of-range"); 85 return begin()[Idx]; 86 } 87 88 /// \btief Returns the minimum number of arguments needed to form a 89 /// template specialization. This may be fewer than the number of 90 /// template parameters, if some of the parameters have default 91 /// arguments or if there is a parameter pack. 92 unsigned getMinRequiredArguments() const; 93 94 SourceLocation getTemplateLoc() const { return TemplateLoc; } 95 SourceLocation getLAngleLoc() const { return LAngleLoc; } 96 SourceLocation getRAngleLoc() const { return RAngleLoc; } 97 98 SourceRange getSourceRange() const { 99 return SourceRange(TemplateLoc, RAngleLoc); 100 } 101}; 102 103/// \brief Represents a template argument within a class template 104/// specialization. 105class TemplateArgument { 106 union { 107 uintptr_t TypeOrValue; 108 struct { 109 char Value[sizeof(llvm::APSInt)]; 110 void *Type; 111 } Integer; 112 struct { 113 TemplateArgument *Args; 114 unsigned NumArgs; 115 bool CopyArgs; 116 } Args; 117 }; 118 119 /// \brief Location of the beginning of this template argument. 120 SourceLocation StartLoc; 121 122public: 123 /// \brief The type of template argument we're storing. 124 enum ArgKind { 125 Null = 0, 126 /// The template argument is a type. Its value is stored in the 127 /// TypeOrValue field. 128 Type = 1, 129 /// The template argument is a declaration 130 Declaration = 2, 131 /// The template argument is an integral value stored in an llvm::APSInt. 132 Integral = 3, 133 /// The template argument is a value- or type-dependent expression 134 /// stored in an Expr*. 135 Expression = 4, 136 137 /// The template argument is actually a parameter pack. Arguments are stored 138 /// in the Args struct. 139 Pack = 5 140 } Kind; 141 142 /// \brief Construct an empty, invalid template argument. 143 TemplateArgument() : TypeOrValue(0), StartLoc(), Kind(Null) { } 144 145 /// \brief Construct a template type argument. 146 TemplateArgument(SourceLocation Loc, QualType T) : Kind(Type) { 147 TypeOrValue = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr()); 148 StartLoc = Loc; 149 } 150 151 /// \brief Construct a template argument that refers to a 152 /// declaration, which is either an external declaration or a 153 /// template declaration. 154 TemplateArgument(SourceLocation Loc, Decl *D) : Kind(Declaration) { 155 // FIXME: Need to be sure we have the "canonical" declaration! 156 TypeOrValue = reinterpret_cast<uintptr_t>(D); 157 StartLoc = Loc; 158 } 159 160 /// \brief Construct an integral constant template argument. 161 TemplateArgument(SourceLocation Loc, const llvm::APSInt &Value, 162 QualType Type) 163 : Kind(Integral) { 164 new (Integer.Value) llvm::APSInt(Value); 165 Integer.Type = Type.getAsOpaquePtr(); 166 StartLoc = Loc; 167 } 168 169 /// \brief Construct a template argument that is an expression. 170 /// 171 /// This form of template argument only occurs in template argument 172 /// lists used for dependent types and for expression; it will not 173 /// occur in a non-dependent, canonical template argument list. 174 TemplateArgument(Expr *E); 175 176 /// \brief Copy constructor for a template argument. 177 TemplateArgument(const TemplateArgument &Other) : Kind(Other.Kind) { 178 if (Kind == Integral) { 179 new (Integer.Value) llvm::APSInt(*Other.getAsIntegral()); 180 Integer.Type = Other.Integer.Type; 181 } else if (Kind == Pack) { 182 Args.NumArgs = Other.Args.NumArgs; 183 Args.Args = new TemplateArgument[Args.NumArgs]; 184 for (unsigned I = 0; I != Args.NumArgs; ++I) 185 Args.Args[I] = Other.Args.Args[I]; 186 } 187 else 188 TypeOrValue = Other.TypeOrValue; 189 StartLoc = Other.StartLoc; 190 } 191 192 TemplateArgument& operator=(const TemplateArgument& Other) { 193 // FIXME: Does not provide the strong guarantee for exception 194 // safety. 195 using llvm::APSInt; 196 197 // FIXME: Handle Packs 198 assert(Kind != Pack && "FIXME: Handle packs"); 199 assert(Other.Kind != Pack && "FIXME: Handle packs"); 200 201 if (Kind == Other.Kind && Kind == Integral) { 202 // Copy integral values. 203 *this->getAsIntegral() = *Other.getAsIntegral(); 204 Integer.Type = Other.Integer.Type; 205 } else { 206 // Destroy the current integral value, if that's what we're holding. 207 if (Kind == Integral) 208 getAsIntegral()->~APSInt(); 209 210 Kind = Other.Kind; 211 212 if (Other.Kind == Integral) { 213 new (Integer.Value) llvm::APSInt(*Other.getAsIntegral()); 214 Integer.Type = Other.Integer.Type; 215 } else 216 TypeOrValue = Other.TypeOrValue; 217 } 218 StartLoc = Other.StartLoc; 219 220 return *this; 221 } 222 223 ~TemplateArgument() { 224 using llvm::APSInt; 225 226 if (Kind == Integral) 227 getAsIntegral()->~APSInt(); 228 else if (Kind == Pack && Args.CopyArgs) 229 delete[] Args.Args; 230 } 231 232 /// \brief Return the kind of stored template argument. 233 ArgKind getKind() const { return Kind; } 234 235 /// \brief Determine whether this template argument has no value. 236 bool isNull() const { return Kind == Null; } 237 238 /// \brief Retrieve the template argument as a type. 239 QualType getAsType() const { 240 if (Kind != Type) 241 return QualType(); 242 243 return QualType::getFromOpaquePtr(reinterpret_cast<void*>(TypeOrValue)); 244 } 245 246 /// \brief Retrieve the template argument as a declaration. 247 Decl *getAsDecl() const { 248 if (Kind != Declaration) 249 return 0; 250 return reinterpret_cast<Decl *>(TypeOrValue); 251 } 252 253 /// \brief Retrieve the template argument as an integral value. 254 llvm::APSInt *getAsIntegral() { 255 if (Kind != Integral) 256 return 0; 257 return reinterpret_cast<llvm::APSInt*>(&Integer.Value[0]); 258 } 259 260 const llvm::APSInt *getAsIntegral() const { 261 return const_cast<TemplateArgument*>(this)->getAsIntegral(); 262 } 263 264 /// \brief Retrieve the type of the integral value. 265 QualType getIntegralType() const { 266 if (Kind != Integral) 267 return QualType(); 268 269 return QualType::getFromOpaquePtr(Integer.Type); 270 } 271 272 void setIntegralType(QualType T) { 273 assert(Kind == Integral && 274 "Cannot set the integral type of a non-integral template argument"); 275 Integer.Type = T.getAsOpaquePtr(); 276 }; 277 278 /// \brief Retrieve the template argument as an expression. 279 Expr *getAsExpr() const { 280 if (Kind != Expression) 281 return 0; 282 283 return reinterpret_cast<Expr *>(TypeOrValue); 284 } 285 286 /// \brief Iterator that traverses the elements of a template argument pack. 287 typedef const TemplateArgument * pack_iterator; 288 289 /// \brief Iterator referencing the first argument of a template argument 290 /// pack. 291 pack_iterator pack_begin() const { 292 assert(Kind == Pack); 293 return Args.Args; 294 } 295 296 /// \brief Iterator referencing one past the last argument of a template 297 /// argument pack. 298 pack_iterator pack_end() const { 299 assert(Kind == Pack); 300 return Args.Args + Args.NumArgs; 301 } 302 303 /// \brief The number of template arguments in the given template argument 304 /// pack. 305 unsigned pack_size() const { 306 assert(Kind == Pack); 307 return Args.NumArgs; 308 } 309 310 /// \brief Retrieve the location where the template argument starts. 311 SourceLocation getLocation() const { return StartLoc; } 312 313 /// \brief Construct a template argument pack. 314 void setArgumentPack(TemplateArgument *Args, unsigned NumArgs, bool CopyArgs); 315 316 /// \brief Used to insert TemplateArguments into FoldingSets. 317 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context) const { 318 ID.AddInteger(Kind); 319 switch (Kind) { 320 case Null: 321 break; 322 323 case Type: 324 getAsType().Profile(ID); 325 break; 326 327 case Declaration: 328 ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0); 329 break; 330 331 case Integral: 332 getAsIntegral()->Profile(ID); 333 getIntegralType().Profile(ID); 334 break; 335 336 case Expression: 337 getAsExpr()->Profile(ID, Context, true); 338 break; 339 340 case Pack: 341 ID.AddInteger(Args.NumArgs); 342 for (unsigned I = 0; I != Args.NumArgs; ++I) 343 Args.Args[I].Profile(ID, Context); 344 } 345 } 346}; 347 348/// \brief A helper class for making template argument lists. 349class TemplateArgumentListBuilder { 350 TemplateArgument *StructuredArgs; 351 unsigned MaxStructuredArgs; 352 unsigned NumStructuredArgs; 353 354 TemplateArgument *FlatArgs; 355 unsigned MaxFlatArgs; 356 unsigned NumFlatArgs; 357 358 bool AddingToPack; 359 unsigned PackBeginIndex; 360 361public: 362 TemplateArgumentListBuilder(const TemplateParameterList *Parameters, 363 unsigned NumTemplateArgs) 364 : StructuredArgs(0), MaxStructuredArgs(Parameters->size()), 365 NumStructuredArgs(0), FlatArgs(0), 366 MaxFlatArgs(std::max(MaxStructuredArgs, NumTemplateArgs)), NumFlatArgs(0), 367 AddingToPack(false), PackBeginIndex(0) { } 368 369 void Append(const TemplateArgument& Arg); 370 void BeginPack(); 371 void EndPack(); 372 373 void ReleaseArgs(); 374 375 unsigned flatSize() const { 376 return NumFlatArgs; 377 } 378 const TemplateArgument *getFlatArguments() const { 379 return FlatArgs; 380 } 381 382 unsigned structuredSize() const { 383 // If we don't have any structured args, just reuse the flat size. 384 if (!StructuredArgs) 385 return flatSize(); 386 387 return NumStructuredArgs; 388 } 389 const TemplateArgument *getStructuredArguments() const { 390 // If we don't have any structured args, just reuse the flat args. 391 if (!StructuredArgs) 392 return getFlatArguments(); 393 394 return StructuredArgs; 395 } 396}; 397 398/// \brief A template argument list. 399/// 400/// FIXME: In the future, this class will be extended to support 401/// variadic templates and member templates, which will make some of 402/// the function names below make more sense. 403class TemplateArgumentList { 404 /// \brief The template argument list. 405 /// 406 /// The integer value will be non-zero to indicate that this 407 /// template argument list does not own the pointer. 408 llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments; 409 410 /// \brief The number of template arguments in this template 411 /// argument list. 412 unsigned NumFlatArguments; 413 414 llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments; 415 unsigned NumStructuredArguments; 416 417public: 418 TemplateArgumentList(ASTContext &Context, 419 TemplateArgumentListBuilder &Builder, 420 bool TakeArgs); 421 422 /// \brief Produces a shallow copy of the given template argument list 423 TemplateArgumentList(const TemplateArgumentList &Other); 424 425 ~TemplateArgumentList(); 426 427 /// \brief Retrieve the template argument at a given index. 428 const TemplateArgument &get(unsigned Idx) const { 429 assert(Idx < NumFlatArguments && "Invalid template argument index"); 430 return getFlatArgumentList()[Idx]; 431 } 432 433 /// \brief Retrieve the template argument at a given index. 434 const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); } 435 436 /// \brief Retrieve the number of template arguments in this 437 /// template argument list. 438 unsigned size() const { return NumFlatArguments; } 439 440 /// \brief Retrieve the number of template arguments in the 441 /// flattened template argument list. 442 unsigned flat_size() const { return NumFlatArguments; } 443 444 /// \brief Retrieve the flattened template argument list. 445 const TemplateArgument *getFlatArgumentList() const { 446 return FlatArguments.getPointer(); 447 } 448}; 449 450//===----------------------------------------------------------------------===// 451// Kinds of Templates 452//===----------------------------------------------------------------------===// 453 454/// TemplateDecl - The base class of all kinds of template declarations (e.g., 455/// class, function, etc.). The TemplateDecl class stores the list of template 456/// parameters and a reference to the templated scoped declaration: the 457/// underlying AST node. 458class TemplateDecl : public NamedDecl { 459protected: 460 // This is probably never used. 461 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 462 DeclarationName Name) 463 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { } 464 465 // Construct a template decl with the given name and parameters. 466 // Used when there is not templated element (tt-params, alias?). 467 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 468 DeclarationName Name, TemplateParameterList *Params) 469 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { } 470 471 // Construct a template decl with name, parameters, and templated element. 472 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 473 DeclarationName Name, TemplateParameterList *Params, 474 NamedDecl *Decl) 475 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), 476 TemplateParams(Params) { } 477public: 478 ~TemplateDecl(); 479 480 /// Get the list of template parameters 481 TemplateParameterList *getTemplateParameters() const { 482 return TemplateParams; 483 } 484 485 /// Get the underlying, templated declaration. 486 NamedDecl *getTemplatedDecl() const { return TemplatedDecl; } 487 488 // Implement isa/cast/dyncast/etc. 489 static bool classof(const Decl *D) { 490 return D->getKind() >= TemplateFirst && D->getKind() <= TemplateLast; 491 } 492 static bool classof(const TemplateDecl *D) { return true; } 493 static bool classof(const FunctionTemplateDecl *D) { return true; } 494 static bool classof(const ClassTemplateDecl *D) { return true; } 495 static bool classof(const TemplateTemplateParmDecl *D) { return true; } 496 497protected: 498 NamedDecl *TemplatedDecl; 499 TemplateParameterList* TemplateParams; 500}; 501 502/// \brief Provides information about a function template specialization, 503/// which is a FunctionDecl that has been explicitly specialization or 504/// instantiated from a function template. 505class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode { 506public: 507 /// \brief The function template specialization that this structure 508 /// describes. 509 FunctionDecl *Function; 510 511 /// \brief The function template from which this function template 512 /// specialization was generated. 513 /// 514 /// The two bits are contain the top 4 values of TemplateSpecializationKind. 515 llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template; 516 517 /// \brief The template arguments used to produce the function template 518 /// specialization from the function template. 519 const TemplateArgumentList *TemplateArguments; 520 521 /// \brief The point at which this function template specialization was 522 /// first instantiated. 523 SourceLocation PointOfInstantiation; 524 525 /// \brief Retrieve the template from which this function was specialized. 526 FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); } 527 528 /// \brief Determine what kind of template specialization this is. 529 TemplateSpecializationKind getTemplateSpecializationKind() const { 530 return (TemplateSpecializationKind)(Template.getInt() + 1); 531 } 532 533 /// \brief Set the template specialization kind. 534 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 535 assert(TSK != TSK_Undeclared && 536 "Cannot encode TSK_Undeclared for a function template specialization"); 537 Template.setInt(TSK - 1); 538 } 539 540 /// \brief Retrieve the first point of instantiation of this function 541 /// template specialization. 542 /// 543 /// The point of instantiation may be an invalid source location if this 544 /// function has yet to be instantiated. 545 SourceLocation getPointOfInstantiation() const { 546 return PointOfInstantiation; 547 } 548 549 /// \brief Set the (first) point of instantiation of this function template 550 /// specialization. 551 void setPointOfInstantiation(SourceLocation POI) { 552 PointOfInstantiation = POI; 553 } 554 555 void Profile(llvm::FoldingSetNodeID &ID) { 556 Profile(ID, TemplateArguments->getFlatArgumentList(), 557 TemplateArguments->flat_size(), 558 Function->getASTContext()); 559 } 560 561 static void 562 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs, 563 unsigned NumTemplateArgs, ASTContext &Context) { 564 ID.AddInteger(NumTemplateArgs); 565 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 566 TemplateArgs[Arg].Profile(ID, Context); 567 } 568}; 569 570/// \brief Provides information a specialization of a member of a class 571/// template, which may be a member function, static data member, or 572/// member class. 573class MemberSpecializationInfo { 574 // The member declaration from which this member was instantiated, and the 575 // manner in which the instantiation occurred (in the lower two bits). 576 llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK; 577 578 // The point at which this member was first instantiated. 579 SourceLocation PointOfInstantiation; 580 581public: 582 explicit 583 MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK) 584 : MemberAndTSK(IF, TSK - 1), PointOfInstantiation() { 585 assert(TSK != TSK_Undeclared && 586 "Cannot encode undeclared template specializations for members"); 587 } 588 589 /// \brief Retrieve the member declaration from which this member was 590 /// instantiated. 591 NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); } 592 593 /// \brief Determine what kind of template specialization this is. 594 TemplateSpecializationKind getTemplateSpecializationKind() const { 595 return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1); 596 } 597 598 /// \brief Set the template specialization kind. 599 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 600 assert(TSK != TSK_Undeclared && 601 "Cannot encode undeclared template specializations for members"); 602 MemberAndTSK.setInt(TSK - 1); 603 } 604 605 /// \brief Retrieve the first point of instantiation of this member. 606 /// If the point of instantiation is an invalid location, then this member 607 /// has not yet been instantiated. 608 SourceLocation getPointOfInstantiation() const { 609 return PointOfInstantiation; 610 } 611 612 /// \brief Set the first point of instantiation. 613 void setPointOfInstantiation(SourceLocation POI) { 614 PointOfInstantiation = POI; 615 } 616}; 617 618/// Declaration of a template function. 619class FunctionTemplateDecl : public TemplateDecl { 620protected: 621 /// \brief Data that is common to all of the declarations of a given 622 /// function template. 623 struct Common { 624 Common() : InstantiatedFromMember(0, false) { } 625 626 /// \brief The function template specializations for this function 627 /// template, including explicit specializations and instantiations. 628 llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations; 629 630 /// \brief The member function template from which this was most 631 /// directly instantiated (or null). 632 /// 633 /// The boolean value indicates whether this member function template 634 /// was explicitly specialized. 635 llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember; 636 }; 637 638 /// \brief A pointer to the previous declaration (if this is a redeclaration) 639 /// or to the data that is common to all declarations of this function 640 /// template. 641 llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev; 642 643 /// \brief Retrieves the "common" pointer shared by all 644 /// (re-)declarations of the same function template. Calling this routine 645 /// may implicitly allocate memory for the common pointer. 646 Common *getCommonPtr(); 647 648 FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 649 TemplateParameterList *Params, NamedDecl *Decl) 650 : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl), 651 CommonOrPrev((Common*)0) { } 652 653public: 654 void Destroy(ASTContext &C); 655 656 /// Get the underlying function declaration of the template. 657 FunctionDecl *getTemplatedDecl() const { 658 return static_cast<FunctionDecl*>(TemplatedDecl); 659 } 660 661 /// \brief Retrieve the set of function template specializations of this 662 /// function template. 663 llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() { 664 return getCommonPtr()->Specializations; 665 } 666 667 /// \brief Retrieve the previous declaration of this function template, or 668 /// NULL if no such declaration exists. 669 const FunctionTemplateDecl *getPreviousDeclaration() const { 670 return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>(); 671 } 672 673 /// \brief Retrieve the previous declaration of this function template, or 674 /// NULL if no such declaration exists. 675 FunctionTemplateDecl *getPreviousDeclaration() { 676 return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>(); 677 } 678 679 /// \brief Set the previous declaration of this function template. 680 void setPreviousDeclaration(FunctionTemplateDecl *Prev) { 681 if (Prev) 682 CommonOrPrev = Prev; 683 } 684 685 virtual FunctionTemplateDecl *getCanonicalDecl(); 686 687 /// \brief Retrieve the member function template that this function template 688 /// was instantiated from. 689 /// 690 /// This routine will return non-NULL for member function templates of 691 /// class templates. For example, given: 692 /// 693 /// \code 694 /// template <typename T> 695 /// struct X { 696 /// template <typename U> void f(); 697 /// }; 698 /// \endcode 699 /// 700 /// X<int>::A<float> is a CXXMethodDecl (whose parent is X<int>, a 701 /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will 702 /// return X<int>::f, a FunctionTemplateDecl (whose parent is again 703 /// X<int>) for which getInstantiatedFromMemberTemplate() will return 704 /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a 705 /// ClassTemplateDecl). 706 /// 707 /// \returns NULL if this is not an instantiation of a member function 708 /// template. 709 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() { 710 return getCommonPtr()->InstantiatedFromMember.getPointer(); 711 } 712 713 void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) { 714 assert(!getCommonPtr()->InstantiatedFromMember.getPointer()); 715 getCommonPtr()->InstantiatedFromMember.setPointer(FTD); 716 } 717 718 /// \brief Determines whether this template was a specialization of a 719 /// member template. 720 /// 721 /// In the following example, the function template \c X<int>::f is a 722 /// member specialization. 723 /// 724 /// \code 725 /// template<typename T> 726 /// struct X { 727 /// template<typename U> void f(T, U); 728 /// }; 729 /// 730 /// template<> template<typename T> 731 /// void X<int>::f(int, T); 732 /// \endcode 733 bool isMemberSpecialization() { 734 return getCommonPtr()->InstantiatedFromMember.getInt(); 735 } 736 737 /// \brief Note that this member template is a specialization. 738 void setMemberSpecialization() { 739 assert(getCommonPtr()->InstantiatedFromMember.getPointer() && 740 "Only member templates can be member template specializations"); 741 getCommonPtr()->InstantiatedFromMember.setInt(true); 742 } 743 744 /// Create a template function node. 745 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC, 746 SourceLocation L, 747 DeclarationName Name, 748 TemplateParameterList *Params, 749 NamedDecl *Decl); 750 751 // Implement isa/cast/dyncast support 752 static bool classof(const Decl *D) 753 { return D->getKind() == FunctionTemplate; } 754 static bool classof(const FunctionTemplateDecl *D) 755 { return true; } 756}; 757 758//===----------------------------------------------------------------------===// 759// Kinds of Template Parameters 760//===----------------------------------------------------------------------===// 761 762/// The TemplateParmPosition class defines the position of a template parameter 763/// within a template parameter list. Because template parameter can be listed 764/// sequentially for out-of-line template members, each template parameter is 765/// given a Depth - the nesting of template parameter scopes - and a Position - 766/// the occurrence within the parameter list. 767/// This class is inheritedly privately by different kinds of template 768/// parameters and is not part of the Decl hierarchy. Just a facility. 769class TemplateParmPosition { 770protected: 771 // FIXME: This should probably never be called, but it's here as 772 TemplateParmPosition() 773 : Depth(0), Position(0) 774 { /* assert(0 && "Cannot create positionless template parameter"); */ } 775 776 TemplateParmPosition(unsigned D, unsigned P) 777 : Depth(D), Position(P) 778 { } 779 780 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for 781 // position? Maybe? 782 unsigned Depth; 783 unsigned Position; 784 785public: 786 /// Get the nesting depth of the template parameter. 787 unsigned getDepth() const { return Depth; } 788 789 /// Get the position of the template parameter within its parameter list. 790 unsigned getPosition() const { return Position; } 791 792 /// Get the index of the template parameter within its parameter list. 793 unsigned getIndex() const { return Position; } 794}; 795 796/// TemplateTypeParmDecl - Declaration of a template type parameter, 797/// e.g., "T" in 798/// @code 799/// template<typename T> class vector; 800/// @endcode 801class TemplateTypeParmDecl : public TypeDecl { 802 /// \brief Whether this template type parameter was declaration with 803 /// the 'typename' keyword. If false, it was declared with the 804 /// 'class' keyword. 805 bool Typename : 1; 806 807 /// \brief Whether this template type parameter inherited its 808 /// default argument. 809 bool InheritedDefault : 1; 810 811 /// \brief Whether this is a parameter pack. 812 bool ParameterPack : 1; 813 814 /// \brief The location of the default argument, if any. 815 SourceLocation DefaultArgumentLoc; 816 817 /// \brief The default template argument, if any. 818 QualType DefaultArgument; 819 820 TemplateTypeParmDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 821 bool Typename, QualType Type, bool ParameterPack) 822 : TypeDecl(TemplateTypeParm, DC, L, Id), Typename(Typename), 823 InheritedDefault(false), ParameterPack(ParameterPack), DefaultArgument() { 824 TypeForDecl = Type.getTypePtr(); 825 } 826 827public: 828 static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC, 829 SourceLocation L, unsigned D, unsigned P, 830 IdentifierInfo *Id, bool Typename, 831 bool ParameterPack); 832 833 /// \brief Whether this template type parameter was declared with 834 /// the 'typename' keyword. If not, it was declared with the 'class' 835 /// keyword. 836 bool wasDeclaredWithTypename() const { return Typename; } 837 838 /// \brief Determine whether this template parameter has a default 839 /// argument. 840 bool hasDefaultArgument() const { return !DefaultArgument.isNull(); } 841 842 /// \brief Retrieve the default argument, if any. 843 QualType getDefaultArgument() const { return DefaultArgument; } 844 845 /// \brief Retrieve the location of the default argument, if any. 846 SourceLocation getDefaultArgumentLoc() const { return DefaultArgumentLoc; } 847 848 /// \brief Determines whether the default argument was inherited 849 /// from a previous declaration of this template. 850 bool defaultArgumentWasInherited() const { return InheritedDefault; } 851 852 /// \brief Set the default argument for this template parameter, and 853 /// whether that default argument was inherited from another 854 /// declaration. 855 void setDefaultArgument(QualType DefArg, SourceLocation DefArgLoc, 856 bool Inherited) { 857 DefaultArgument = DefArg; 858 DefaultArgumentLoc = DefArgLoc; 859 InheritedDefault = Inherited; 860 } 861 862 /// \brief Returns whether this is a parameter pack. 863 bool isParameterPack() const { return ParameterPack; } 864 865 // Implement isa/cast/dyncast/etc. 866 static bool classof(const Decl *D) { 867 return D->getKind() == TemplateTypeParm; 868 } 869 static bool classof(const TemplateTypeParmDecl *D) { return true; } 870}; 871 872/// NonTypeTemplateParmDecl - Declares a non-type template parameter, 873/// e.g., "Size" in 874/// @code 875/// template<int Size> class array { }; 876/// @endcode 877class NonTypeTemplateParmDecl 878 : public VarDecl, protected TemplateParmPosition { 879 /// \brief The default template argument, if any. 880 Expr *DefaultArgument; 881 882 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D, 883 unsigned P, IdentifierInfo *Id, QualType T, 884 DeclaratorInfo *DInfo) 885 : VarDecl(NonTypeTemplateParm, DC, L, Id, T, DInfo, VarDecl::None), 886 TemplateParmPosition(D, P), DefaultArgument(0) 887 { } 888 889public: 890 static NonTypeTemplateParmDecl * 891 Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D, 892 unsigned P, IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo); 893 894 using TemplateParmPosition::getDepth; 895 using TemplateParmPosition::getPosition; 896 using TemplateParmPosition::getIndex; 897 898 /// \brief Determine whether this template parameter has a default 899 /// argument. 900 bool hasDefaultArgument() const { return DefaultArgument; } 901 902 /// \brief Retrieve the default argument, if any. 903 Expr *getDefaultArgument() const { return DefaultArgument; } 904 905 /// \brief Retrieve the location of the default argument, if any. 906 SourceLocation getDefaultArgumentLoc() const; 907 908 /// \brief Set the default argument for this template parameter. 909 void setDefaultArgument(Expr *DefArg) { 910 DefaultArgument = DefArg; 911 } 912 913 // Implement isa/cast/dyncast/etc. 914 static bool classof(const Decl *D) { 915 return D->getKind() == NonTypeTemplateParm; 916 } 917 static bool classof(const NonTypeTemplateParmDecl *D) { return true; } 918}; 919 920/// TemplateTemplateParmDecl - Declares a template template parameter, 921/// e.g., "T" in 922/// @code 923/// template <template <typename> class T> class container { }; 924/// @endcode 925/// A template template parameter is a TemplateDecl because it defines the 926/// name of a template and the template parameters allowable for substitution. 927class TemplateTemplateParmDecl 928 : public TemplateDecl, protected TemplateParmPosition { 929 930 /// \brief The default template argument, if any. 931 Expr *DefaultArgument; 932 933 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, 934 unsigned D, unsigned P, 935 IdentifierInfo *Id, TemplateParameterList *Params) 936 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), 937 TemplateParmPosition(D, P), DefaultArgument(0) 938 { } 939 940public: 941 static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC, 942 SourceLocation L, unsigned D, 943 unsigned P, IdentifierInfo *Id, 944 TemplateParameterList *Params); 945 946 using TemplateParmPosition::getDepth; 947 using TemplateParmPosition::getPosition; 948 using TemplateParmPosition::getIndex; 949 950 /// \brief Determine whether this template parameter has a default 951 /// argument. 952 bool hasDefaultArgument() const { return DefaultArgument; } 953 954 /// \brief Retrieve the default argument, if any. 955 Expr *getDefaultArgument() const { return DefaultArgument; } 956 957 /// \brief Retrieve the location of the default argument, if any. 958 SourceLocation getDefaultArgumentLoc() const; 959 960 /// \brief Set the default argument for this template parameter. 961 void setDefaultArgument(Expr *DefArg) { 962 DefaultArgument = DefArg; 963 } 964 965 // Implement isa/cast/dyncast/etc. 966 static bool classof(const Decl *D) { 967 return D->getKind() == TemplateTemplateParm; 968 } 969 static bool classof(const TemplateTemplateParmDecl *D) { return true; } 970}; 971 972/// \brief Represents a class template specialization, which refers to 973/// a class template with a given set of template arguments. 974/// 975/// Class template specializations represent both explicit 976/// specialization of class templates, as in the example below, and 977/// implicit instantiations of class templates. 978/// 979/// \code 980/// template<typename T> class array; 981/// 982/// template<> 983/// class array<bool> { }; // class template specialization array<bool> 984/// \endcode 985class ClassTemplateSpecializationDecl 986 : public CXXRecordDecl, public llvm::FoldingSetNode { 987 988 /// \brief Structure that stores information about a class template 989 /// specialization that was instantiated from a class template partial 990 /// specialization. 991 struct SpecializedPartialSpecialization { 992 /// \brief The class template partial specialization from which this 993 /// class template specialization was instantiated. 994 ClassTemplatePartialSpecializationDecl *PartialSpecialization; 995 996 /// \brief The template argument list deduced for the class template 997 /// partial specialization itself. 998 TemplateArgumentList *TemplateArgs; 999 }; 1000 1001 /// \brief The template that this specialization specializes 1002 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *> 1003 SpecializedTemplate; 1004 1005 /// \brief The template arguments used to describe this specialization. 1006 TemplateArgumentList TemplateArgs; 1007 1008 /// \brief The point where this template was instantiated (if any) 1009 SourceLocation PointOfInstantiation; 1010 1011 /// \brief The kind of specialization this declaration refers to. 1012 /// Really a value of type TemplateSpecializationKind. 1013 unsigned SpecializationKind : 3; 1014 1015protected: 1016 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, 1017 DeclContext *DC, SourceLocation L, 1018 ClassTemplateDecl *SpecializedTemplate, 1019 TemplateArgumentListBuilder &Builder, 1020 ClassTemplateSpecializationDecl *PrevDecl); 1021 1022public: 1023 static ClassTemplateSpecializationDecl * 1024 Create(ASTContext &Context, DeclContext *DC, SourceLocation L, 1025 ClassTemplateDecl *SpecializedTemplate, 1026 TemplateArgumentListBuilder &Builder, 1027 ClassTemplateSpecializationDecl *PrevDecl); 1028 1029 virtual void Destroy(ASTContext& C); 1030 1031 virtual void getNameForDiagnostic(std::string &S, 1032 const PrintingPolicy &Policy, 1033 bool Qualified) const; 1034 1035 /// \brief Retrieve the template that this specialization specializes. 1036 ClassTemplateDecl *getSpecializedTemplate() const; 1037 1038 /// \brief Retrieve the template arguments of the class template 1039 /// specialization. 1040 const TemplateArgumentList &getTemplateArgs() const { 1041 return TemplateArgs; 1042 } 1043 1044 /// \brief Determine the kind of specialization that this 1045 /// declaration represents. 1046 TemplateSpecializationKind getSpecializationKind() const { 1047 return static_cast<TemplateSpecializationKind>(SpecializationKind); 1048 } 1049 1050 void setSpecializationKind(TemplateSpecializationKind TSK) { 1051 SpecializationKind = TSK; 1052 } 1053 1054 /// \brief Get the point of instantiation (if any), or null if none. 1055 SourceLocation getPointOfInstantiation() const { 1056 return PointOfInstantiation; 1057 } 1058 1059 void setPointOfInstantiation(SourceLocation Loc) { 1060 assert(Loc.isValid() && "point of instantiation must be valid!"); 1061 PointOfInstantiation = Loc; 1062 } 1063 1064 /// \brief If this class template specialization is an instantiation of 1065 /// a template (rather than an explicit specialization), return the 1066 /// class template or class template partial specialization from which it 1067 /// was instantiated. 1068 llvm::PointerUnion<ClassTemplateDecl *, 1069 ClassTemplatePartialSpecializationDecl *> 1070 getInstantiatedFrom() const { 1071 if (getSpecializationKind() != TSK_ImplicitInstantiation && 1072 getSpecializationKind() != TSK_ExplicitInstantiationDefinition && 1073 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration) 1074 return (ClassTemplateDecl*)0; 1075 1076 if (SpecializedPartialSpecialization *PartialSpec 1077 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1078 return PartialSpec->PartialSpecialization; 1079 1080 return const_cast<ClassTemplateDecl*>( 1081 SpecializedTemplate.get<ClassTemplateDecl*>()); 1082 } 1083 1084 /// \brief Retrieve the set of template arguments that should be used 1085 /// to instantiate members of the class template or class template partial 1086 /// specialization from which this class template specialization was 1087 /// instantiated. 1088 /// 1089 /// \returns For a class template specialization instantiated from the primary 1090 /// template, this function will return the same template arguments as 1091 /// getTemplateArgs(). For a class template specialization instantiated from 1092 /// a class template partial specialization, this function will return the 1093 /// deduced template arguments for the class template partial specialization 1094 /// itself. 1095 const TemplateArgumentList &getTemplateInstantiationArgs() const { 1096 if (SpecializedPartialSpecialization *PartialSpec 1097 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1098 return *PartialSpec->TemplateArgs; 1099 1100 return getTemplateArgs(); 1101 } 1102 1103 /// \brief Note that this class template specialization is actually an 1104 /// instantiation of the given class template partial specialization whose 1105 /// template arguments have been deduced. 1106 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, 1107 TemplateArgumentList *TemplateArgs) { 1108 SpecializedPartialSpecialization *PS 1109 = new (getASTContext()) SpecializedPartialSpecialization(); 1110 PS->PartialSpecialization = PartialSpec; 1111 PS->TemplateArgs = TemplateArgs; 1112 SpecializedTemplate = PS; 1113 } 1114 1115 /// \brief Sets the type of this specialization as it was written by 1116 /// the user. This will be a class template specialization type. 1117 void setTypeAsWritten(QualType T) { 1118 TypeForDecl = T.getTypePtr(); 1119 } 1120 1121 void Profile(llvm::FoldingSetNodeID &ID) const { 1122 Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(), 1123 getASTContext()); 1124 } 1125 1126 static void 1127 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs, 1128 unsigned NumTemplateArgs, ASTContext &Context) { 1129 ID.AddInteger(NumTemplateArgs); 1130 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg) 1131 TemplateArgs[Arg].Profile(ID, Context); 1132 } 1133 1134 static bool classof(const Decl *D) { 1135 return D->getKind() == ClassTemplateSpecialization || 1136 D->getKind() == ClassTemplatePartialSpecialization; 1137 } 1138 1139 static bool classof(const ClassTemplateSpecializationDecl *) { 1140 return true; 1141 } 1142 1143 static bool classof(const ClassTemplatePartialSpecializationDecl *) { 1144 return true; 1145 } 1146}; 1147 1148class ClassTemplatePartialSpecializationDecl 1149 : public ClassTemplateSpecializationDecl { 1150 /// \brief The list of template parameters 1151 TemplateParameterList* TemplateParams; 1152 1153 ClassTemplatePartialSpecializationDecl(ASTContext &Context, 1154 DeclContext *DC, SourceLocation L, 1155 TemplateParameterList *Params, 1156 ClassTemplateDecl *SpecializedTemplate, 1157 TemplateArgumentListBuilder &Builder, 1158 ClassTemplatePartialSpecializationDecl *PrevDecl) 1159 : ClassTemplateSpecializationDecl(Context, 1160 ClassTemplatePartialSpecialization, 1161 DC, L, SpecializedTemplate, Builder, 1162 PrevDecl), 1163 TemplateParams(Params) { } 1164 1165public: 1166 static ClassTemplatePartialSpecializationDecl * 1167 Create(ASTContext &Context, DeclContext *DC, SourceLocation L, 1168 TemplateParameterList *Params, 1169 ClassTemplateDecl *SpecializedTemplate, 1170 TemplateArgumentListBuilder &Builder, 1171 ClassTemplatePartialSpecializationDecl *PrevDecl); 1172 1173 /// Get the list of template parameters 1174 TemplateParameterList *getTemplateParameters() const { 1175 return TemplateParams; 1176 } 1177 1178 // FIXME: Add Profile support! 1179 1180 static bool classof(const Decl *D) { 1181 return D->getKind() == ClassTemplatePartialSpecialization; 1182 } 1183 1184 static bool classof(const ClassTemplatePartialSpecializationDecl *) { 1185 return true; 1186 } 1187}; 1188 1189/// Declaration of a class template. 1190class ClassTemplateDecl : public TemplateDecl { 1191protected: 1192 /// \brief Data that is common to all of the declarations of a given 1193 /// class template. 1194 struct Common { 1195 Common() : InstantiatedFromMember(0, 0) {} 1196 1197 /// \brief The class template specializations for this class 1198 /// template, including explicit specializations and instantiations. 1199 llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations; 1200 1201 /// \brief The class template partial specializations for this class 1202 /// template. 1203 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> 1204 PartialSpecializations; 1205 1206 /// \brief The injected-class-name type for this class template. 1207 QualType InjectedClassNameType; 1208 1209 /// \brief The templated member class from which this was most 1210 /// directly instantiated (or null). 1211 /// 1212 /// The boolean value indicates whether this member class template 1213 /// was explicitly specialized. 1214 llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember; 1215 }; 1216 1217 // FIXME: Combine PreviousDeclaration with CommonPtr, as in 1218 // FunctionTemplateDecl. 1219 1220 /// \brief Previous declaration of this class template. 1221 ClassTemplateDecl *PreviousDeclaration; 1222 1223 /// \brief Pointer to the data that is common to all of the 1224 /// declarations of this class template. 1225 /// 1226 /// The first declaration of a class template (e.g., the declaration 1227 /// with no "previous declaration") owns this pointer. 1228 Common *CommonPtr; 1229 1230 ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, 1231 TemplateParameterList *Params, NamedDecl *Decl, 1232 ClassTemplateDecl *PrevDecl, Common *CommonPtr) 1233 : TemplateDecl(ClassTemplate, DC, L, Name, Params, Decl), 1234 PreviousDeclaration(PrevDecl), CommonPtr(CommonPtr) { } 1235 1236 ~ClassTemplateDecl(); 1237 1238public: 1239 /// Get the underlying class declarations of the template. 1240 CXXRecordDecl *getTemplatedDecl() const { 1241 return static_cast<CXXRecordDecl *>(TemplatedDecl); 1242 } 1243 1244 /// \brief Retrieve the previous declaration of this template. 1245 ClassTemplateDecl *getPreviousDeclaration() const { 1246 return PreviousDeclaration; 1247 } 1248 1249 virtual ClassTemplateDecl *getCanonicalDecl(); 1250 1251 /// Create a class template node. 1252 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC, 1253 SourceLocation L, 1254 DeclarationName Name, 1255 TemplateParameterList *Params, 1256 NamedDecl *Decl, 1257 ClassTemplateDecl *PrevDecl); 1258 1259 /// \brief Retrieve the set of specializations of this class template. 1260 llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() { 1261 return CommonPtr->Specializations; 1262 } 1263 1264 /// \brief Retrieve the set of partial specializations of this class 1265 /// template. 1266 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> & 1267 getPartialSpecializations() { 1268 return CommonPtr->PartialSpecializations; 1269 } 1270 1271 /// \brief Find a class template partial specialization with the given 1272 /// type T. 1273 /// 1274 /// \brief A dependent type that names a specialization of this class 1275 /// template. 1276 /// 1277 /// \returns the class template partial specialization that exactly matches 1278 /// the type \p T, or NULL if no such partial specialization exists. 1279 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T); 1280 1281 /// \brief Retrieve the type of the injected-class-name for this 1282 /// class template. 1283 /// 1284 /// The injected-class-name for a class template \c X is \c 1285 /// X<template-args>, where \c template-args is formed from the 1286 /// template arguments that correspond to the template parameters of 1287 /// \c X. For example: 1288 /// 1289 /// \code 1290 /// template<typename T, int N> 1291 /// struct array { 1292 /// typedef array this_type; // "array" is equivalent to "array<T, N>" 1293 /// }; 1294 /// \endcode 1295 QualType getInjectedClassNameType(ASTContext &Context); 1296 1297 /// \brief Retrieve the member class template that this class template was 1298 /// derived from. 1299 /// 1300 /// This routine will return non-NULL for templated member classes of 1301 /// class templates. For example, given: 1302 /// 1303 /// \code 1304 /// template <typename T> 1305 /// struct X { 1306 /// template <typename U> struct A {}; 1307 /// }; 1308 /// \endcode 1309 /// 1310 /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent 1311 /// is X<int>, also a CTSD) for which getSpecializedTemplate() will 1312 /// return X<int>::A<U>, a TemplateClassDecl (whose parent is again 1313 /// X<int>) for which getInstantiatedFromMemberTemplate() will return 1314 /// X<T>::A<U>, a TemplateClassDecl (whose parent is X<T>, also a TCD). 1315 /// 1316 /// \returns null if this is not an instantiation of a member class template. 1317 ClassTemplateDecl *getInstantiatedFromMemberTemplate() const { 1318 return CommonPtr->InstantiatedFromMember.getPointer(); 1319 } 1320 1321 void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) { 1322 assert(!CommonPtr->InstantiatedFromMember.getPointer()); 1323 CommonPtr->InstantiatedFromMember.setPointer(CTD); 1324 } 1325 1326 /// \brief Determines whether this template was a specialization of a 1327 /// member template. 1328 /// 1329 /// In the following example, the member template \c X<int>::Inner is a 1330 /// member specialization. 1331 /// 1332 /// \code 1333 /// template<typename T> 1334 /// struct X { 1335 /// template<typename U> struct Inner; 1336 /// }; 1337 /// 1338 /// template<> template<typename T> 1339 /// struct X<int>::Inner { /* ... */ }; 1340 /// \endcode 1341 bool isMemberSpecialization() { 1342 return CommonPtr->InstantiatedFromMember.getInt(); 1343 } 1344 1345 /// \brief Note that this member template is a specialization. 1346 void setMemberSpecialization() { 1347 assert(CommonPtr->InstantiatedFromMember.getPointer() && 1348 "Only member templates can be member template specializations"); 1349 CommonPtr->InstantiatedFromMember.setInt(true); 1350 } 1351 1352 // Implement isa/cast/dyncast support 1353 static bool classof(const Decl *D) 1354 { return D->getKind() == ClassTemplate; } 1355 static bool classof(const ClassTemplateDecl *D) 1356 { return true; } 1357 1358 virtual void Destroy(ASTContext& C); 1359}; 1360 1361/// Declaration of a friend template. For example: 1362/// 1363/// template <typename T> class A { 1364/// friend class MyVector<T>; // not a friend template 1365/// template <typename U> friend class B; // friend template 1366/// template <typename U> friend class Foo<T>::Nested; // friend template 1367class FriendTemplateDecl : public Decl { 1368public: 1369 typedef llvm::PointerUnion<NamedDecl*,Type*> FriendUnion; 1370 1371private: 1372 // The number of template parameters; always non-zero. 1373 unsigned NumParams; 1374 1375 // The parameter list. 1376 TemplateParameterList **Params; 1377 1378 // The declaration that's a friend of this class. 1379 FriendUnion Friend; 1380 1381 // Location of the 'friend' specifier. 1382 SourceLocation FriendLoc; 1383 1384 1385 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc, 1386 unsigned NParams, 1387 TemplateParameterList **Params, 1388 FriendUnion Friend, 1389 SourceLocation FriendLoc) 1390 : Decl(Decl::FriendTemplate, DC, Loc), 1391 NumParams(NParams), 1392 Params(Params), 1393 Friend(Friend), 1394 FriendLoc(FriendLoc) 1395 {} 1396 1397public: 1398 static FriendTemplateDecl *Create(ASTContext &Context, 1399 DeclContext *DC, SourceLocation Loc, 1400 unsigned NParams, 1401 TemplateParameterList **Params, 1402 FriendUnion Friend, 1403 SourceLocation FriendLoc); 1404 1405 /// If this friend declaration names a templated type (or 1406 /// a dependent member type of a templated type), return that 1407 /// type; otherwise return null. 1408 Type *getFriendType() const { 1409 return Friend.dyn_cast<Type*>(); 1410 } 1411 1412 /// If this friend declaration names a templated function (or 1413 /// a member function of a templated type), return that type; 1414 /// otherwise return null. 1415 NamedDecl *getFriendDecl() const { 1416 return Friend.dyn_cast<NamedDecl*>(); 1417 } 1418 1419 /// Retrieves the location of the 'friend' keyword. 1420 SourceLocation getFriendLoc() const { 1421 return FriendLoc; 1422 } 1423 1424 TemplateParameterList *getTemplateParameterList(unsigned i) const { 1425 assert(i <= NumParams); 1426 return Params[i]; 1427 } 1428 1429 unsigned getNumTemplateParameters() const { 1430 return NumParams; 1431 } 1432 1433 // Implement isa/cast/dyncast/etc. 1434 static bool classof(const Decl *D) { 1435 return D->getKind() == Decl::FriendTemplate; 1436 } 1437 static bool classof(const FriendTemplateDecl *D) { return true; } 1438}; 1439 1440/// Implementation of inline functions that require the template declarations 1441inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD) 1442 : Function(FTD) { } 1443 1444} /* end of namespace clang */ 1445 1446#endif 1447