DeclTemplate.h revision 32f2fb53d9d7c28c94d8569fd0fcf06cccee0c3d
1aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===//
2aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//
3aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//                     The LLVM Compiler Infrastructure
4aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//
5aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor// This file is distributed under the University of Illinois Open Source
6aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor// License. See LICENSE.TXT for details.
7aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//
8aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//===----------------------------------------------------------------------===//
9aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//
10aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//  This file defines the C++ template declaration subclasses.
11aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//
12aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor//===----------------------------------------------------------------------===//
13aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
14aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
15aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#define LLVM_CLANG_AST_DECLTEMPLATE_H
16aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
1755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor#include "clang/AST/DeclCXX.h"
18275c10a8a4a43219f67d8d2c912ec6294d9d9af2John McCall#include "clang/AST/TemplateBase.h"
19f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor#include "llvm/ADT/PointerUnion.h"
203f3ce82674b44a58a2af7d90feb55acd906dd762Anders Carlsson#include <limits>
2155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
22aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregornamespace clang {
23aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
24aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass TemplateParameterList;
25aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass TemplateDecl;
26aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass FunctionTemplateDecl;
27aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass ClassTemplateDecl;
28c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregorclass ClassTemplatePartialSpecializationDecl;
29aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass TemplateTypeParmDecl;
30aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass NonTypeTemplateParmDecl;
31aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass TemplateTemplateParmDecl;
32aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
33f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor/// \brief Stores a template parameter of any kind.
34f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregortypedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
35f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                            TemplateTemplateParmDecl*> TemplateParameter;
36f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
37aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor/// TemplateParameterList - Stores a list of template parameters for a
38aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor/// TemplateDecl and its derived classes.
39aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorclass TemplateParameterList {
40ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  /// The location of the 'template' keyword.
41ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  SourceLocation TemplateLoc;
42ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor
43ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  /// The locations of the '<' and '>' angle brackets.
44ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  SourceLocation LAngleLoc, RAngleLoc;
45ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor
46ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  /// The number of template parameters in this template
47aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  /// parameter list.
48aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  unsigned NumParams;
49aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
50ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
51bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                        NamedDecl **Params, unsigned NumParams,
52ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor                        SourceLocation RAngleLoc);
53aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
54aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorpublic:
551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static TemplateParameterList *Create(ASTContext &C,
56ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor                                       SourceLocation TemplateLoc,
57ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor                                       SourceLocation LAngleLoc,
58bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                       NamedDecl **Params,
59ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor                                       unsigned NumParams,
60ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor                                       SourceLocation RAngleLoc);
61aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
62aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  /// iterator - Iterates through the template parameters in this list.
63bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef NamedDecl** iterator;
64aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
65aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  /// const_iterator - Iterates through the template parameters in this list.
66bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef NamedDecl* const* const_iterator;
67aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
68bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
69aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  const_iterator begin() const {
70bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return reinterpret_cast<NamedDecl * const *>(this + 1);
71aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  }
72aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  iterator end() { return begin() + NumParams; }
73aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  const_iterator end() const { return begin() + NumParams; }
74aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
75aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  unsigned size() const { return NumParams; }
76ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor
77bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  NamedDecl* getParam(unsigned Idx) {
78f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    assert(Idx < size() && "Template parameter index out-of-range");
79f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return begin()[Idx];
80f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
81f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
82bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  const NamedDecl* getParam(unsigned Idx) const {
8340808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    assert(Idx < size() && "Template parameter index out-of-range");
8440808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    return begin()[Idx];
8540808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor  }
8640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
8762cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  /// \btief Returns the minimum number of arguments needed to form a
8862cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  /// template specialization. This may be fewer than the number of
8962cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  /// template parameters, if some of the parameters have default
900ceffb51b28b09db67404058c642dcb1f877f6e8Anders Carlsson  /// arguments or if there is a parameter pack.
9162cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  unsigned getMinRequiredArguments() const;
9262cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor
93ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Get the depth of this template parameter list in the set of
94ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template parameter lists.
95ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
96ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// The first template parameter list in a declaration will have depth 0,
97ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// the second template parameter list will have depth 1, etc.
98ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  unsigned getDepth() const;
99ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
100ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  SourceLocation getTemplateLoc() const { return TemplateLoc; }
101ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  SourceLocation getLAngleLoc() const { return LAngleLoc; }
102ddc29e116db3c3f4144355e67a0137b38b6bb6d1Douglas Gregor  SourceLocation getRAngleLoc() const { return RAngleLoc; }
10362cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor
10462cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  SourceRange getSourceRange() const {
10562cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor    return SourceRange(TemplateLoc, RAngleLoc);
10662cb18dd11472965e03374d40bc27d650bc331b6Douglas Gregor  }
107aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor};
108aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
109127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// \brief A helper class for making template argument lists.
110127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass TemplateArgumentListBuilder {
111127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateArgument *StructuredArgs;
112127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned MaxStructuredArgs;
113127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned NumStructuredArgs;
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateArgument *FlatArgs;
116127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned MaxFlatArgs;
117127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned NumFlatArgs;
1181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool AddingToPack;
120127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned PackBeginIndex;
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorpublic:
123127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateArgumentListBuilder(const TemplateParameterList *Parameters,
124127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                              unsigned NumTemplateArgs)
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : StructuredArgs(0), MaxStructuredArgs(Parameters->size()),
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NumStructuredArgs(0), FlatArgs(0),
127127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  MaxFlatArgs(std::max(MaxStructuredArgs, NumTemplateArgs)), NumFlatArgs(0),
128127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  AddingToPack(false), PackBeginIndex(0) { }
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void Append(const TemplateArgument& Arg);
131127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void BeginPack();
132127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void EndPack();
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void ReleaseArgs();
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned flatSize() const {
137127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return NumFlatArgs;
138127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
139127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  const TemplateArgument *getFlatArguments() const {
140127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return FlatArgs;
141127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned structuredSize() const {
144127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we don't have any structured args, just reuse the flat size.
145127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (!StructuredArgs)
146127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return flatSize();
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
148127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return NumStructuredArgs;
149d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor  }
150127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  const TemplateArgument *getStructuredArguments() const {
151127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we don't have any structured args, just reuse the flat args.
152127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (!StructuredArgs)
153127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return getFlatArguments();
1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return StructuredArgs;
156aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  }
157aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor};
158aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
159127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// \brief A template argument list.
160127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor///
161127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// FIXME: In the future, this class will be extended to support
162127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// variadic templates and member templates, which will make some of
163127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// the function names below make more sense.
164127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass TemplateArgumentList {
165127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The template argument list.
166127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  ///
167127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// The integer value will be non-zero to indicate that this
168127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// template argument list does not own the pointer.
169127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments;
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
171127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The number of template arguments in this template
172127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// argument list.
173127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned NumFlatArguments;
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments;
176127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned NumStructuredArguments;
1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
178aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregorpublic:
179127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateArgumentList(ASTContext &Context,
180127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                       TemplateArgumentListBuilder &Builder,
181127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                       bool TakeArgs);
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  /// \brief Produces a shallow copy of the given template argument list
184b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor  TemplateArgumentList(const TemplateArgumentList &Other);
185b9aa6b214c8fbc3e081dde575eef1f0913d48bdcDouglas Gregor
186127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  ~TemplateArgumentList();
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
188127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the template argument at a given index.
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const TemplateArgument &get(unsigned Idx) const {
190127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    assert(Idx < NumFlatArguments && "Invalid template argument index");
191127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return getFlatArgumentList()[Idx];
192127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
194127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the template argument at a given index.
195127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the number of template arguments in this
198127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// template argument list.
199127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned size() const { return NumFlatArguments; }
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the number of template arguments in the
202127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// flattened template argument list.
203127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned flat_size() const { return NumFlatArguments; }
2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
205127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the flattened template argument list.
2061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const TemplateArgument *getFlatArgumentList() const {
207127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return FlatArguments.getPointer();
208127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
209127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor};
2101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
211127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor//===----------------------------------------------------------------------===//
212127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor// Kinds of Templates
213127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor//===----------------------------------------------------------------------===//
214aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
215127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// TemplateDecl - The base class of all kinds of template declarations (e.g.,
216127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// class, function, etc.). The TemplateDecl class stores the list of template
217127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// parameters and a reference to the templated scoped declaration: the
218127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// underlying AST node.
219127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass TemplateDecl : public NamedDecl {
220127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorprotected:
221127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // This is probably never used.
222127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
223127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor               DeclarationName Name)
2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
225d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor
226127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Construct a template decl with the given name and parameters.
227127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Used when there is not templated element (tt-params, alias?).
228127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
229127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor               DeclarationName Name, TemplateParameterList *Params)
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
231d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor
232127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Construct a template decl with name, parameters, and templated element.
233127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
234127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor               DeclarationName Name, TemplateParameterList *Params,
235127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor               NamedDecl *Decl)
236127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
237127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      TemplateParams(Params) { }
238127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorpublic:
239127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  ~TemplateDecl();
240d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor
241127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the list of template parameters
242127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateParameterList *getTemplateParameters() const {
243127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return TemplateParams;
244d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor  }
245d684b0027e16163c4bdba3e2f8bfadda7d62a0d3Douglas Gregor
246127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the underlying, templated declaration.
247127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
248127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
249aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  // Implement isa/cast/dyncast/etc.
25080cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
251127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const TemplateDecl *D) { return true; }
252127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const FunctionTemplateDecl *D) { return true; }
253127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const ClassTemplateDecl *D) { return true; }
254aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor  static bool classof(const TemplateTemplateParmDecl *D) { return true; }
25580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) {
25680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return K >= TemplateFirst && K <= TemplateLast;
25780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  }
258aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
259127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorprotected:
260127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  NamedDecl *TemplatedDecl;
261127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateParameterList* TemplateParams;
262127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor};
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Provides information about a function template specialization,
265127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// which is a FunctionDecl that has been explicitly specialization or
266127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// instantiated from a function template.
267127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
268127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorpublic:
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief The function template specialization that this structure
270127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// describes.
271127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionDecl *Function;
2721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief The function template from which this function template
274127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// specialization was generated.
2751fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  ///
276d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  /// The two bits are contain the top 4 values of TemplateSpecializationKind.
277d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The template arguments used to produce the function template
280127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// specialization from the function template.
281127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  const TemplateArgumentList *TemplateArguments;
2821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
283b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// \brief The point at which this function template specialization was
284b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// first instantiated.
285b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  SourceLocation PointOfInstantiation;
286b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
2871fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  /// \brief Retrieve the template from which this function was specialized.
2881fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
289d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
290d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  /// \brief Determine what kind of template specialization this is.
291d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  TemplateSpecializationKind getTemplateSpecializationKind() const {
292d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return (TemplateSpecializationKind)(Template.getInt() + 1);
293d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  }
294d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
295d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  /// \brief Set the template specialization kind.
296d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(TSK != TSK_Undeclared &&
298d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor         "Cannot encode TSK_Undeclared for a function template specialization");
299d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    Template.setInt(TSK - 1);
3001fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor  }
3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
302b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// \brief Retrieve the first point of instantiation of this function
303b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// template specialization.
304b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  ///
305b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// The point of instantiation may be an invalid source location if this
306b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// function has yet to be instantiated.
307b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  SourceLocation getPointOfInstantiation() const {
308b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor    return PointOfInstantiation;
309b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  }
310b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
311b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// \brief Set the (first) point of instantiation of this function template
312b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// specialization.
313b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  void setPointOfInstantiation(SourceLocation POI) {
314b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor    PointOfInstantiation = POI;
315b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  }
316b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
317127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void Profile(llvm::FoldingSetNodeID &ID) {
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Profile(ID, TemplateArguments->getFlatArgumentList(),
319828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor            TemplateArguments->flat_size(),
3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            Function->getASTContext());
321127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static void
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
325828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor          unsigned NumTemplateArgs, ASTContext &Context) {
326127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    ID.AddInteger(NumTemplateArgs);
327127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
328828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor      TemplateArgs[Arg].Profile(ID, Context);
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
330127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor};
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3322db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor/// \brief Provides information a specialization of a member of a class
3332db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor/// template, which may be a member function, static data member, or
3342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor/// member class.
3352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorclass MemberSpecializationInfo {
33644e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor  // The member declaration from which this member was instantiated, and the
33744e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor  // manner in which the instantiation occurred (in the lower two bits).
33844e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor  llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
3392db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
340b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  // The point at which this member was first instantiated.
341b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  SourceLocation PointOfInstantiation;
342b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
3432db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregorpublic:
3442db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  explicit
3452db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK)
346b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor    : MemberAndTSK(IF, TSK - 1), PointOfInstantiation() {
34744e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor    assert(TSK != TSK_Undeclared &&
34844e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor           "Cannot encode undeclared template specializations for members");
34944e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor  }
3502db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
3512db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  /// \brief Retrieve the member declaration from which this member was
3522db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  /// instantiated.
35344e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor  NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
3542db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
3552db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  /// \brief Determine what kind of template specialization this is.
3562db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  TemplateSpecializationKind getTemplateSpecializationKind() const {
35744e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor    return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
3582db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  }
3592db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
3602db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  /// \brief Set the template specialization kind.
3612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
36244e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor    assert(TSK != TSK_Undeclared &&
36344e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor           "Cannot encode undeclared template specializations for members");
36444e368b6a85c42d681148ccd5e0052418ff9751eDouglas Gregor    MemberAndTSK.setInt(TSK - 1);
3652db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  }
366b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
367b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// \brief Retrieve the first point of instantiation of this member.
368b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// If the point of instantiation is an invalid location, then this member
369b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// has not yet been instantiated.
370b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  SourceLocation getPointOfInstantiation() const {
371b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor    return PointOfInstantiation;
372b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  }
373b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor
374b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  /// \brief Set the first point of instantiation.
375b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  void setPointOfInstantiation(SourceLocation POI) {
376b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor    PointOfInstantiation = POI;
377b3ae4fcd4314a9c1c46d41b200883599c32025b4Douglas Gregor  }
3782db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor};
3792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor
380127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// Declaration of a template function.
3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpclass FunctionTemplateDecl : public TemplateDecl {
382127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorprotected:
383127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Data that is common to all of the declarations of a given
384127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// function template.
385127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  struct Common {
386fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    Common() : InstantiatedFromMember(0, false) { }
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
388127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    /// \brief The function template specializations for this function
389127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    /// template, including explicit specializations and instantiations.
390127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations;
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
392d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    /// \brief The member function template from which this was most
393d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    /// directly instantiated (or null).
394fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    ///
395fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    /// The boolean value indicates whether this member function template
396fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    /// was explicitly specialized.
397fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember;
3983e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  };
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief A pointer to the previous declaration (if this is a redeclaration)
401127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// or to the data that is common to all declarations of this function
402127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// template.
403127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev;
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Retrieves the "common" pointer shared by all
406127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// (re-)declarations of the same function template. Calling this routine
407127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// may implicitly allocate memory for the common pointer.
408127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Common *getCommonPtr();
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
411127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                       TemplateParameterList *Params, NamedDecl *Decl)
412127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl),
413127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      CommonOrPrev((Common*)0) { }
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4153e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregorpublic:
416127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void Destroy(ASTContext &C);
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
418127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the underlying function declaration of the template.
419127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionDecl *getTemplatedDecl() const {
420127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return static_cast<FunctionDecl*>(TemplatedDecl);
4213e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
4223e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
423127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the set of function template specializations of this
424127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// function template.
425127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
426127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return getCommonPtr()->Specializations;
4273e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the previous declaration of this function template, or
430127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// NULL if no such declaration exists.
431127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  const FunctionTemplateDecl *getPreviousDeclaration() const {
432127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
4333e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
4343e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
435127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the previous declaration of this function template, or
436127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// NULL if no such declaration exists.
437127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *getPreviousDeclaration() {
438127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
4393e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
441127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Set the previous declaration of this function template.
442127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void setPreviousDeclaration(FunctionTemplateDecl *Prev) {
443127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Prev)
444127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      CommonOrPrev = Prev;
445127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  virtual FunctionTemplateDecl *getCanonicalDecl();
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Retrieve the member function template that this function template
450d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// was instantiated from.
451d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  ///
452d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// This routine will return non-NULL for member function templates of
453d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// class templates.  For example, given:
454d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  ///
455d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// \code
456d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// template <typename T>
457d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// struct X {
458d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  ///   template <typename U> void f();
459d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// };
460d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// \endcode
461d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  ///
462d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// X<int>::A<float> is a CXXMethodDecl (whose parent is X<int>, a
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will
464d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// return X<int>::f, a FunctionTemplateDecl (whose parent is again
465d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// X<int>) for which getInstantiatedFromMemberTemplate() will return
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a
467d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// ClassTemplateDecl).
468d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  ///
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \returns NULL if this is not an instantiation of a member function
470d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  /// template.
471d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
472fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    return getCommonPtr()->InstantiatedFromMember.getPointer();
473d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  }
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) {
476fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
477fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    getCommonPtr()->InstantiatedFromMember.setPointer(FTD);
478d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  }
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \brief Determines whether this template was a specialization of a
481fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// member template.
482fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
483fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// In the following example, the function template \c X<int>::f is a
484fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// member specialization.
485fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
486fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \code
487fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// template<typename T>
488fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// struct X {
489fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///   template<typename U> void f(T, U);
490fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// };
491fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
492fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// template<> template<typename T>
493fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// void X<int>::f(int, T);
494fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \endcode
495fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  bool isMemberSpecialization() {
496fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    return getCommonPtr()->InstantiatedFromMember.getInt();
497fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  }
498fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor
499fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \brief Note that this member template is a specialization.
500fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  void setMemberSpecialization() {
501fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
502fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor           "Only member templates can be member template specializations");
503fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    getCommonPtr()->InstantiatedFromMember.setInt(true);
504fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  }
505fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor
506127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Create a template function node.
507127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
508127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      SourceLocation L,
509127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      DeclarationName Name,
510127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      TemplateParameterList *Params,
511127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      NamedDecl *Decl);
5123e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
513127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Implement isa/cast/dyncast support
51480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
51580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const FunctionTemplateDecl *D) { return true; }
51680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == FunctionTemplate; }
517127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor};
518d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson
519127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor//===----------------------------------------------------------------------===//
520127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor// Kinds of Template Parameters
521127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor//===----------------------------------------------------------------------===//
52240808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
523127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// The TemplateParmPosition class defines the position of a template parameter
524127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// within a template parameter list. Because template parameter can be listed
525127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// sequentially for out-of-line template members, each template parameter is
526127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// given a Depth - the nesting of template parameter scopes - and a Position -
527127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// the occurrence within the parameter list.
528127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// This class is inheritedly privately by different kinds of template
529127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// parameters and is not part of the Decl hierarchy. Just a facility.
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpclass TemplateParmPosition {
531127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorprotected:
532127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // FIXME: This should probably never be called, but it's here as
533127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateParmPosition()
534127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : Depth(0), Position(0)
535127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  { /* assert(0 && "Cannot create positionless template parameter"); */ }
5363e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
537127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateParmPosition(unsigned D, unsigned P)
538127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : Depth(D), Position(P)
539127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  { }
5403e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
541127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
542127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // position? Maybe?
543127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned Depth;
544127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned Position;
5453e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
546127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorpublic:
547127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the nesting depth of the template parameter.
548127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned getDepth() const { return Depth; }
5493e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
550127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the position of the template parameter within its parameter list.
551127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned getPosition() const { return Position; }
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
553127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// Get the index of the template parameter within its parameter list.
554127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  unsigned getIndex() const { return Position; }
555127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor};
5563e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
557127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// TemplateTypeParmDecl - Declaration of a template type parameter,
558127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// e.g., "T" in
559127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @code
560127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// template<typename T> class vector;
561127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @endcode
562127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass TemplateTypeParmDecl : public TypeDecl {
563127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Whether this template type parameter was declaration with
564127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// the 'typename' keyword. If false, it was declared with the
565127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// 'class' keyword.
566127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool Typename : 1;
5673e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
568127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Whether this template type parameter inherited its
569127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// default argument.
570127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool InheritedDefault : 1;
5713e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
572127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Whether this is a parameter pack.
573127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool ParameterPack : 1;
574127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
575127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The default template argument, if any.
576a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DefaultArgument;
577127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
579127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                       bool Typename, QualType Type, bool ParameterPack)
580127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : TypeDecl(TemplateTypeParm, DC, L, Id), Typename(Typename),
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      InheritedDefault(false), ParameterPack(ParameterPack), DefaultArgument() {
582127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    TypeForDecl = Type.getTypePtr();
5833e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
5843e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
585127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorpublic:
586127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
587127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      SourceLocation L, unsigned D, unsigned P,
588127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      IdentifierInfo *Id, bool Typename,
589127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                      bool ParameterPack);
590c971f8694661776ecdec2ccc33fbe0333eeaf191Douglas Gregor
591127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Whether this template type parameter was declared with
592127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// the 'typename' keyword. If not, it was declared with the 'class'
593127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// keyword.
594127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool wasDeclaredWithTypename() const { return Typename; }
595c971f8694661776ecdec2ccc33fbe0333eeaf191Douglas Gregor
596127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Determine whether this template parameter has a default
597127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// argument.
598833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  bool hasDefaultArgument() const { return DefaultArgument != 0; }
599199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
600127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the default argument, if any.
601833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  QualType getDefaultArgument() const { return DefaultArgument->getType(); }
60240808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
603833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// \brief Retrieves the default argument's source information, if any.
604a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
605833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
606833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// \brief Retrieves the location of the default argument declaration.
607833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  SourceLocation getDefaultArgumentLoc() const;
60840808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
609127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Determines whether the default argument was inherited
610127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// from a previous declaration of this template.
611127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool defaultArgumentWasInherited() const { return InheritedDefault; }
612f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
613127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Set the default argument for this template parameter, and
614127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// whether that default argument was inherited from another
615127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// declaration.
616a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
617127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    DefaultArgument = DefArg;
618127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    InheritedDefault = Inherited;
619f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
62040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
621833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// \brief Removes the default argument of this template parameter.
622833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  void removeDefaultArgument() {
623833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    DefaultArgument = 0;
624833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    InheritedDefault = false;
625833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
626833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
627ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Retrieve the depth of the template parameter.
628ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  unsigned getDepth() const;
629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Retrieve the index of the template parameter.
631ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  unsigned getIndex() const;
632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
633127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Returns whether this is a parameter pack.
634127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool isParameterPack() const { return ParameterPack; }
635fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson
636127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Implement isa/cast/dyncast/etc.
63780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
638127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const TemplateTypeParmDecl *D) { return true; }
63980cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == TemplateTypeParm; }
6403e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor};
6413e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
642127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// NonTypeTemplateParmDecl - Declares a non-type template parameter,
643127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// e.g., "Size" in
644127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @code
645127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// template<int Size> class array { };
646127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @endcode
647127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass NonTypeTemplateParmDecl
648127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  : public VarDecl, protected TemplateParmPosition {
649127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The default template argument, if any.
650127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Expr *DefaultArgument;
651127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
652127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D,
653127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                          unsigned P, IdentifierInfo *Id, QualType T,
654a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                          TypeSourceInfo *TInfo)
655a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    : VarDecl(NonTypeTemplateParm, DC, L, Id, T, TInfo, VarDecl::None),
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateParmPosition(D, P), DefaultArgument(0)
657127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  { }
658127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
6591c5976e5e933c0d74f7e04e8f907a93f5edbfec1Anders Carlssonpublic:
660127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static NonTypeTemplateParmDecl *
661127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D,
662a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall         unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo);
6639ba41645892da0000fe8a7832b80208f44dafedaAnders Carlsson
664127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getDepth;
665127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getPosition;
666127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getIndex;
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
668127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Determine whether this template parameter has a default
669127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// argument.
670127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  bool hasDefaultArgument() const { return DefaultArgument; }
671fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson
672127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the default argument, if any.
673127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  Expr *getDefaultArgument() const { return DefaultArgument; }
674127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
675127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the location of the default argument, if any.
676127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  SourceLocation getDefaultArgumentLoc() const;
677127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
678127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Set the default argument for this template parameter.
679127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void setDefaultArgument(Expr *DefArg) {
680127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    DefaultArgument = DefArg;
6813b36b66a00b7d5bab71b486a54694f0ae397bddbAnders Carlsson  }
682127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
683127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Implement isa/cast/dyncast/etc.
68480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
685127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
68680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
6871c5976e5e933c0d74f7e04e8f907a93f5edbfec1Anders Carlsson};
6881c5976e5e933c0d74f7e04e8f907a93f5edbfec1Anders Carlsson
689127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// TemplateTemplateParmDecl - Declares a template template parameter,
690127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// e.g., "T" in
691127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @code
692127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// template <template <typename> class T> class container { };
693127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// @endcode
694127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// A template template parameter is a TemplateDecl because it defines the
695127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor/// name of a template and the template parameters allowable for substitution.
696127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorclass TemplateTemplateParmDecl
697127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  : public TemplateDecl, protected TemplateParmPosition {
6987e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
699127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief The default template argument, if any.
700788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  TemplateArgumentLoc DefaultArgument;
7017e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
702127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
703127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                           unsigned D, unsigned P,
704127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                           IdentifierInfo *Id, TemplateParameterList *Params)
705127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
706788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      TemplateParmPosition(D, P), DefaultArgument()
707127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    { }
7087e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
709127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregorpublic:
710127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC,
711127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                          SourceLocation L, unsigned D,
712127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                          unsigned P, IdentifierInfo *Id,
713127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                          TemplateParameterList *Params);
7147e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
715127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getDepth;
716127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getPosition;
717127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  using TemplateParmPosition::getIndex;
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
719127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Determine whether this template parameter has a default
720127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// argument.
721788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  bool hasDefaultArgument() const {
722788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return !DefaultArgument.getArgument().isNull();
723788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  }
7247e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
725127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Retrieve the default argument, if any.
726788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  const TemplateArgumentLoc &getDefaultArgument() const {
727788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return DefaultArgument;
728788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  }
7297e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
730127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  /// \brief Set the default argument for this template parameter.
731788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  void setDefaultArgument(const TemplateArgumentLoc &DefArg) {
732127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    DefaultArgument = DefArg;
733127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
7347e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
735127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Implement isa/cast/dyncast/etc.
73680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
737127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  static bool classof(const TemplateTemplateParmDecl *D) { return true; }
73880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
7397e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor};
7407e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor
7413e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// \brief Represents a class template specialization, which refers to
7423e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// a class template with a given set of template arguments.
7433e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor///
7443e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// Class template specializations represent both explicit
7453e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// specialization of class templates, as in the example below, and
7463e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// implicit instantiations of class templates.
7473e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor///
7483e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// \code
7493e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// template<typename T> class array;
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// template<>
7523e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// class array<bool> { }; // class template specialization array<bool>
7533e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// \endcode
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpclass ClassTemplateSpecializationDecl
7553e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  : public CXXRecordDecl, public llvm::FoldingSetNode {
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Structure that stores information about a class template
75837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// specialization that was instantiated from a class template partial
75937d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// specialization.
76037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  struct SpecializedPartialSpecialization {
76137d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    /// \brief The class template partial specialization from which this
76237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    /// class template specialization was instantiated.
76337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    ClassTemplatePartialSpecializationDecl *PartialSpecialization;
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76537d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    /// \brief The template argument list deduced for the class template
76637d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    /// partial specialization itself.
76737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    TemplateArgumentList *TemplateArgs;
76837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  };
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7703e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  /// \brief The template that this specialization specializes
77137d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
77237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    SpecializedTemplate;
7733e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
7743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// \brief The type-as-written of an explicit template specialization.
7753cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// Does not apply to implicit specializations.
7763cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *TypeAsWritten;
7773cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
7787e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  /// \brief The template arguments used to describe this specialization.
7797e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateArgumentList TemplateArgs;
780cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
7819cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  /// \brief The point where this template was instantiated (if any)
7829cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  SourceLocation PointOfInstantiation;
7839cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall
784cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  /// \brief The kind of specialization this declaration refers to.
785cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  /// Really a value of type TemplateSpecializationKind.
786d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  unsigned SpecializationKind : 3;
787cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
788c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregorprotected:
789c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
7907e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                                  DeclContext *DC, SourceLocation L,
7913e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor                                  ClassTemplateDecl *SpecializedTemplate,
7928e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                  TemplateArgumentListBuilder &Builder,
7938e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                  ClassTemplateSpecializationDecl *PrevDecl);
7941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7953e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregorpublic:
7963e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  static ClassTemplateSpecializationDecl *
7973e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
7983e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor         ClassTemplateDecl *SpecializedTemplate,
79991fdf6f576b91f023c3bebb0d3786aab555cb3c5Anders Carlsson         TemplateArgumentListBuilder &Builder,
800cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor         ClassTemplateSpecializationDecl *PrevDecl);
8013e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
80237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  virtual void Destroy(ASTContext& C);
80337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor
804136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall  virtual void getNameForDiagnostic(std::string &S,
805136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                    const PrintingPolicy &Policy,
806136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall                                    bool Qualified) const;
807136a6988960ac3aeb96f298da7a1a182db7217cdJohn McCall
8083e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  /// \brief Retrieve the template that this specialization specializes.
80937d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  ClassTemplateDecl *getSpecializedTemplate() const;
8103e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Retrieve the template arguments of the class template
81237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// specialization.
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const TemplateArgumentList &getTemplateArgs() const {
8147e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor    return TemplateArgs;
8153e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
8163e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
817cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  /// \brief Determine the kind of specialization that this
818cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  /// declaration represents.
819cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  TemplateSpecializationKind getSpecializationKind() const {
820cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    return static_cast<TemplateSpecializationKind>(SpecializationKind);
821cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  }
822cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
823cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  void setSpecializationKind(TemplateSpecializationKind TSK) {
824cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor    SpecializationKind = TSK;
825cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor  }
826cc636688c4fd10b1732ce3e33b2b106024d545caDouglas Gregor
8279cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  /// \brief Get the point of instantiation (if any), or null if none.
8289cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  SourceLocation getPointOfInstantiation() const {
8299cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall    return PointOfInstantiation;
8309cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  }
8319cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall
8329cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  void setPointOfInstantiation(SourceLocation Loc) {
8339cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall    assert(Loc.isValid() && "point of instantiation must be valid!");
8349cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall    PointOfInstantiation = Loc;
8359cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall  }
8369cc7807e1622c2f945b607bdd39dd283df5e7bb5John McCall
83737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// \brief If this class template specialization is an instantiation of
83837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// a template (rather than an explicit specialization), return the
83937d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// class template or class template partial specialization from which it
84037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// was instantiated.
8411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::PointerUnion<ClassTemplateDecl *,
84237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor                     ClassTemplatePartialSpecializationDecl *>
84337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  getInstantiatedFrom() const {
84437d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    if (getSpecializationKind() != TSK_ImplicitInstantiation &&
845d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
846d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
84737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor      return (ClassTemplateDecl*)0;
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (SpecializedPartialSpecialization *PartialSpec
85037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor          = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
85137d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor      return PartialSpec->PartialSpecialization;
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    return const_cast<ClassTemplateDecl*>(
85437d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor                             SpecializedTemplate.get<ClassTemplateDecl*>());
85537d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  }
8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// \brief Retrieve the set of template arguments that should be used
85837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// to instantiate members of the class template or class template partial
85937d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// specialization from which this class template specialization was
86037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// instantiated.
86137d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  ///
86237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// \returns For a class template specialization instantiated from the primary
86337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// template, this function will return the same template arguments as
86437d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// getTemplateArgs(). For a class template specialization instantiated from
86537d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// a class template partial specialization, this function will return the
86637d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// deduced template arguments for the class template partial specialization
86737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// itself.
86837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  const TemplateArgumentList &getTemplateInstantiationArgs() const {
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (SpecializedPartialSpecialization *PartialSpec
87037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor        = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
87137d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor      return *PartialSpec->TemplateArgs;
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    return getTemplateArgs();
87437d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  }
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87637d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// \brief Note that this class template specialization is actually an
87737d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// instantiation of the given class template partial specialization whose
87837d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  /// template arguments have been deduced.
87937d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
88037d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor                          TemplateArgumentList *TemplateArgs) {
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SpecializedPartialSpecialization *PS
88237d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor      = new (getASTContext()) SpecializedPartialSpecialization();
88337d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    PS->PartialSpecialization = PartialSpec;
88437d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    PS->TemplateArgs = TemplateArgs;
88537d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor    SpecializedTemplate = PS;
88637d93e9252026d4fb836d9c05d0122a2d46e56beDouglas Gregor  }
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
888fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor  /// \brief Sets the type of this specialization as it was written by
889fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor  /// the user. This will be a class template specialization type.
8903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  void setTypeAsWritten(TypeSourceInfo *T) {
8913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TypeAsWritten = T;
8923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
8933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
8943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// \brief Gets the type of this specialization as it was written by
8953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// the user, if it was so written.
8963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *getTypeAsWritten() const {
8973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return TypeAsWritten;
898fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor  }
899fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
900c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  void Profile(llvm::FoldingSetNodeID &ID) const {
901828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor    Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(),
902828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor            getASTContext());
903c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
904c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
9051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static void
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
907828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor          unsigned NumTemplateArgs, ASTContext &Context) {
9080ceffb51b28b09db67404058c642dcb1f877f6e8Anders Carlsson    ID.AddInteger(NumTemplateArgs);
9093e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor    for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
910828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor      TemplateArgs[Arg].Profile(ID, Context);
9113e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
9123e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
91380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
91480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) {
91580cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return K == ClassTemplateSpecialization ||
91680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall           K == ClassTemplatePartialSpecialization;
9173e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
9183e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
9193e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  static bool classof(const ClassTemplateSpecializationDecl *) {
9203e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor    return true;
9213e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
922c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
923c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  static bool classof(const ClassTemplatePartialSpecializationDecl *) {
924c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
925c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
926c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor};
927c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpclass ClassTemplatePartialSpecializationDecl
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  : public ClassTemplateSpecializationDecl {
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief The list of template parameters
931c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  TemplateParameterList* TemplateParams;
932c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
933833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// \brief The source info for the template arguments as written.
9343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// FIXME: redundant with TypeAsWritten?
935833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentLoc *ArgsAsWritten;
936833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned NumArgsAsWritten;
937833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
938ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief The class template partial specialization from which this
939ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// class template partial specialization was instantiated.
940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
941ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// The boolean value will be true to indicate that this class template
942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// partial specialization was specialized at this level.
943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
944ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      InstantiatedFromMember;
945ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
946c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  ClassTemplatePartialSpecializationDecl(ASTContext &Context,
947c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor                                         DeclContext *DC, SourceLocation L,
948c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor                                         TemplateParameterList *Params,
949c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor                                         ClassTemplateDecl *SpecializedTemplate,
9508e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                         TemplateArgumentListBuilder &Builder,
951833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                         TemplateArgumentLoc *ArgInfos,
952833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                         unsigned NumArgInfos,
9538e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                               ClassTemplatePartialSpecializationDecl *PrevDecl)
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    : ClassTemplateSpecializationDecl(Context,
9558e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                      ClassTemplatePartialSpecialization,
9568e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                      DC, L, SpecializedTemplate, Builder,
9578e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor                                      PrevDecl),
958833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      TemplateParams(Params), ArgsAsWritten(ArgInfos),
959833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      NumArgsAsWritten(NumArgInfos), InstantiatedFromMember(0, false) { }
960c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
961c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregorpublic:
962c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  static ClassTemplatePartialSpecializationDecl *
963c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
964c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor         TemplateParameterList *Params,
965c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor         ClassTemplateDecl *SpecializedTemplate,
96691fdf6f576b91f023c3bebb0d3786aab555cb3c5Anders Carlsson         TemplateArgumentListBuilder &Builder,
967d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall         const TemplateArgumentListInfo &ArgInfos,
9683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall         QualType CanonInjectedType,
969c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor         ClassTemplatePartialSpecializationDecl *PrevDecl);
970c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
971c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  /// Get the list of template parameters
972c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  TemplateParameterList *getTemplateParameters() const {
973c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return TemplateParams;
974c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
975c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
976833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// Get the template arguments as written.
977833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentLoc *getTemplateArgsAsWritten() const {
978833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    return ArgsAsWritten;
979833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
980833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
981833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  /// Get the number of template arguments as written.
982833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned getNumTemplateArgsAsWritten() const {
983833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    return NumArgsAsWritten;
984833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
985833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
986ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Retrieve the member class template partial specialization from
987ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// which this particular class template partial specialization was
988ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// instantiated.
989ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
990ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \code
991ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template<typename T>
992ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// struct Outer {
993ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///   template<typename U> struct Inner;
994ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///   template<typename U> struct Inner<U*> { }; // #1
995ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// };
996ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
997ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// Outer<float>::Inner<int*> ii;
998ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \endcode
999ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
1000ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1001ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// end up instantiating the partial specialization
1002ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1003ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template partial specialization \c Outer<T>::Inner<U*>. Given
1004ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \c Outer<float>::Inner<U*>, this function would return
1005ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \c Outer<T>::Inner<U*>.
1006ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
1007ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ClassTemplatePartialSpecializationDecl *First
1008ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1009ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return First->InstantiatedFromMember.getPointer();
1010ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1011ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1012ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void setInstantiatedFromMember(
1013ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1014ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ClassTemplatePartialSpecializationDecl *First
1015ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1016ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    First->InstantiatedFromMember.setPointer(PartialSpec);
1017ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1018ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1019ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Determines whether this class template partial specialization
1020ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template was a specialization of a member partial specialization.
1021ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
1022ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// In the following example, the member template partial specialization
1023ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \c X<int>::Inner<T*> is a member specialization.
1024ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
1025ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \code
1026ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template<typename T>
1027ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// struct X {
1028ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///   template<typename U> struct Inner;
1029ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///   template<typename U> struct Inner<U*>;
1030ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// };
1031ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ///
1032ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// template<> template<typename T>
1033ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// struct X<int>::Inner<T*> { /* ... */ };
1034ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \endcode
1035ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  bool isMemberSpecialization() {
1036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ClassTemplatePartialSpecializationDecl *First
1037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1038ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return First->InstantiatedFromMember.getInt();
1039ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1040ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1041ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  /// \brief Note that this member template is a specialization.
1042ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void setMemberSpecialization() {
1043ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ClassTemplatePartialSpecializationDecl *First
1044ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1045ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    assert(First->InstantiatedFromMember.getPointer() &&
1046ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor           "Only member templates can be member template specializations");
1047ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return First->InstantiatedFromMember.setInt(true);
1048ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1049ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1050c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  // FIXME: Add Profile support!
1051c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
105280cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
105380cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) {
105480cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall    return K == ClassTemplatePartialSpecialization;
1055c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
1056c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
1057c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  static bool classof(const ClassTemplatePartialSpecializationDecl *) {
1058c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return true;
1059c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
10603e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor};
10613e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
10623e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor/// Declaration of a class template.
10633e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregorclass ClassTemplateDecl : public TemplateDecl {
10643e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregorprotected:
10655953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// \brief Data that is common to all of the declarations of a given
10665953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// class template.
10675953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  struct Common {
1068fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    Common() : InstantiatedFromMember(0, 0) {}
1069e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
10705953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    /// \brief The class template specializations for this class
10715953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    /// template, including explicit specializations and instantiations.
10725953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations;
10737da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
1074c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    /// \brief The class template partial specializations for this class
1075c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    /// template.
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>
1077c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor      PartialSpecializations;
1078c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
10797da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    /// \brief The injected-class-name type for this class template.
10807da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    QualType InjectedClassNameType;
1081e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1082e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    /// \brief The templated member class from which this was most
1083e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    /// directly instantiated (or null).
1084fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    ///
1085fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    /// The boolean value indicates whether this member class template
1086fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    /// was explicitly specialized.
1087fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember;
10885953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  };
10895953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
1090fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  // FIXME: Combine PreviousDeclaration with CommonPtr, as in
1091fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  // FunctionTemplateDecl.
1092fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor
10937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  /// \brief Previous declaration of this class template.
10945953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  ClassTemplateDecl *PreviousDeclaration;
10955953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
10965953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// \brief Pointer to the data that is common to all of the
10975953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// declarations of this class template.
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
10995953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// The first declaration of a class template (e.g., the declaration
11005953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// with no "previous declaration") owns this pointer.
11015953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  Common *CommonPtr;
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11033e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
11045953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor                    TemplateParameterList *Params, NamedDecl *Decl,
11055953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor                    ClassTemplateDecl *PrevDecl, Common *CommonPtr)
11065953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    : TemplateDecl(ClassTemplate, DC, L, Name, Params, Decl),
11075953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor      PreviousDeclaration(PrevDecl), CommonPtr(CommonPtr) { }
11083e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
11095953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  ~ClassTemplateDecl();
11103e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
11113e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregorpublic:
11123e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  /// Get the underlying class declarations of the template.
11133e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  CXXRecordDecl *getTemplatedDecl() const {
11143e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor    return static_cast<CXXRecordDecl *>(TemplatedDecl);
11153e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
11163e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
11177da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// \brief Retrieve the previous declaration of this template.
11187da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  ClassTemplateDecl *getPreviousDeclaration() const {
11197da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return PreviousDeclaration;
11207da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  }
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1122b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis  virtual ClassTemplateDecl *getCanonicalDecl();
11237da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
11245953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  /// Create a class template node.
11253e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
11263e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor                                   SourceLocation L,
11273e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor                                   DeclarationName Name,
11283e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor                                   TemplateParameterList *Params,
11295953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor                                   NamedDecl *Decl,
11305953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor                                   ClassTemplateDecl *PrevDecl);
11313e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
11323e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  /// \brief Retrieve the set of specializations of this class template.
11333e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() {
11345953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor    return CommonPtr->Specializations;
11353e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  }
11363e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
1137c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  /// \brief Retrieve the set of partial specializations of this class
1138c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  /// template.
1139c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
1140c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  getPartialSpecializations() {
1141c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor    return CommonPtr->PartialSpecializations;
1142c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor  }
1143c8ab2563ac8f7dcc4fdc518b5cc7015ecbb2f003Douglas Gregor
1144b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// \brief Find a class template partial specialization with the given
1145b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// type T.
1146b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  ///
1147b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// \brief A dependent type that names a specialization of this class
1148b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// template.
1149b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  ///
1150b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// \returns the class template partial specialization that exactly matches
1151b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  /// the type \p T, or NULL if no such partial specialization exists.
1152b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// \brief Retrieve the template specialization type of the
11553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  /// injected-class-name for this class template.
11567da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  ///
11577da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// The injected-class-name for a class template \c X is \c
11587da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// X<template-args>, where \c template-args is formed from the
11597da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// template arguments that correspond to the template parameters of
11607da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// \c X. For example:
11617da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  ///
11627da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// \code
11637da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// template<typename T, int N>
11647da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// struct array {
11657da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  ///   typedef array this_type; // "array" is equivalent to "array<T, N>"
11667da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// };
11677da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  /// \endcode
11683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  QualType getInjectedClassNameSpecialization(ASTContext &Context);
11697da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
1170e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// \brief Retrieve the member class template that this class template was
1171e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// derived from.
1172e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ///
1173e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// This routine will return non-NULL for templated member classes of
1174e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// class templates.  For example, given:
1175e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ///
1176e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// \code
1177e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// template <typename T>
1178e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// struct X {
1179e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ///   template <typename U> struct A {};
1180e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// };
1181e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// \endcode
1182e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ///
1183e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent
1184e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// is X<int>, also a CTSD) for which getSpecializedTemplate() will
1185e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// return X<int>::A<U>, a TemplateClassDecl (whose parent is again
1186e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// X<int>) for which getInstantiatedFromMemberTemplate() will return
1187e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// X<T>::A<U>, a TemplateClassDecl (whose parent is X<T>, also a TCD).
1188e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ///
1189e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  /// \returns null if this is not an instantiation of a member class template.
1190e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
1191fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    return CommonPtr->InstantiatedFromMember.getPointer();
1192e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1193e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1194e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) {
1195fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    assert(!CommonPtr->InstantiatedFromMember.getPointer());
1196fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    CommonPtr->InstantiatedFromMember.setPointer(CTD);
1197e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1198e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1199fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \brief Determines whether this template was a specialization of a
1200fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// member template.
1201fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
1202fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// In the following example, the member template \c X<int>::Inner is a
1203fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// member specialization.
1204fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
1205fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \code
1206fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// template<typename T>
1207fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// struct X {
1208fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///   template<typename U> struct Inner;
1209fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// };
1210fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  ///
1211fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// template<> template<typename T>
1212fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// struct X<int>::Inner { /* ... */ };
1213fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \endcode
1214fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  bool isMemberSpecialization() {
1215fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    return CommonPtr->InstantiatedFromMember.getInt();
1216fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  }
1217fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor
1218fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  /// \brief Note that this member template is a specialization.
1219fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  void setMemberSpecialization() {
1220fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    assert(CommonPtr->InstantiatedFromMember.getPointer() &&
1221fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor           "Only member templates can be member template specializations");
1222fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor    CommonPtr->InstantiatedFromMember.setInt(true);
1223fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  }
1224fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor
12253e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor  // Implement isa/cast/dyncast support
122680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
122780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const ClassTemplateDecl *D) { return true; }
122880cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == ClassTemplate; }
12295953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor
12305953d8b37f92f0cf548941f617c9b0a7703df33bDouglas Gregor  virtual void Destroy(ASTContext& C);
12313e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor};
12323e00bad490f1bae8a2c60f934e7eb5dbb9752c5dDouglas Gregor
1233dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall/// Declaration of a friend template.  For example:
1234dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall///
1235dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall/// template <typename T> class A {
1236dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall///   friend class MyVector<T>; // not a friend template
1237dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall///   template <typename U> friend class B; // friend template
1238dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall///   template <typename U> friend class Foo<T>::Nested; // friend template
1239dd4a3b0065b9a7e7b00073df415a798886c090f3John McCallclass FriendTemplateDecl : public Decl {
1240dd4a3b0065b9a7e7b00073df415a798886c090f3John McCallpublic:
124132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
1242dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1243dd4a3b0065b9a7e7b00073df415a798886c090f3John McCallprivate:
1244dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  // The number of template parameters;  always non-zero.
1245dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  unsigned NumParams;
1246dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1247dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  // The parameter list.
1248dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  TemplateParameterList **Params;
1249dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1250dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  // The declaration that's a friend of this class.
1251dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  FriendUnion Friend;
1252dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1253dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  // Location of the 'friend' specifier.
1254dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  SourceLocation FriendLoc;
1255dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1256dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1257dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
1258dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                     unsigned NParams,
1259dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                     TemplateParameterList **Params,
1260dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                     FriendUnion Friend,
1261dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                     SourceLocation FriendLoc)
1262dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    : Decl(Decl::FriendTemplate, DC, Loc),
1263dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall      NumParams(NParams),
1264dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall      Params(Params),
1265dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall      Friend(Friend),
1266dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall      FriendLoc(FriendLoc)
1267dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  {}
1268dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1269dd4a3b0065b9a7e7b00073df415a798886c090f3John McCallpublic:
1270dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  static FriendTemplateDecl *Create(ASTContext &Context,
1271dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                                    DeclContext *DC, SourceLocation Loc,
1272dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                                    unsigned NParams,
1273dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                                    TemplateParameterList **Params,
1274dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                                    FriendUnion Friend,
1275dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall                                    SourceLocation FriendLoc);
1276dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1277dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// If this friend declaration names a templated type (or
1278dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// a dependent member type of a templated type), return that
1279dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// type;  otherwise return null.
128032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  TypeSourceInfo *getFriendType() const {
128132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    return Friend.dyn_cast<TypeSourceInfo*>();
1282dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  }
1283dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1284dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// If this friend declaration names a templated function (or
1285dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// a member function of a templated type), return that type;
1286dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// otherwise return null.
1287dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  NamedDecl *getFriendDecl() const {
1288dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    return Friend.dyn_cast<NamedDecl*>();
1289dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  }
1290dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1291dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  /// Retrieves the location of the 'friend' keyword.
1292dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  SourceLocation getFriendLoc() const {
1293dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    return FriendLoc;
1294dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  }
1295dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1296dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  TemplateParameterList *getTemplateParameterList(unsigned i) const {
1297dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    assert(i <= NumParams);
1298dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    return Params[i];
1299dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  }
1300dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1301dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  unsigned getNumTemplateParameters() const {
1302dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall    return NumParams;
1303dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  }
1304dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1305dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  // Implement isa/cast/dyncast/etc.
130680cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
130780cd64a8450d8e2c079dc134d9711cd45ba89d63John McCall  static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
1308dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall  static bool classof(const FriendTemplateDecl *D) { return true; }
1309dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall};
1310dd4a3b0065b9a7e7b00073df415a798886c090f3John McCall
1311e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// Implementation of inline functions that require the template declarations
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpinline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
1313e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  : Function(FTD) { }
1314e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1315aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor} /* end of namespace clang */
1316aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor
1317aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#endif
1318