Template.h revision 23323e0253716ff03c95a00fb6903019daafe3aa
1d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//===------- SemaTemplate.h - C++ Templates ---------------------*- C++ -*-===/
2d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//
3d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//                     The LLVM Compiler Infrastructure
4d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//
5d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor// This file is distributed under the University of Illinois Open Source
6d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor// License. See LICENSE.TXT for details.
7d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//===----------------------------------------------------------------------===/
8d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//
9d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//  This file provides types used in the semantic analysis of C++ templates.
10d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//
11d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor//===----------------------------------------------------------------------===/
12d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#ifndef LLVM_CLANG_SEMA_TEMPLATE_H
13d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#define LLVM_CLANG_SEMA_TEMPLATE_H
14d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
15d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#include "clang/AST/DeclTemplate.h"
16d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor#include "clang/AST/DeclVisitor.h"
17d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#include "llvm/ADT/SmallVector.h"
18d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#include <cassert>
19d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor#include <utility>
20d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
21d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregornamespace clang {
22d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// \brief Data structure that captures multiple levels of template argument
23d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// lists for use in template instantiation.
24d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///
25d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// Multiple levels of template arguments occur when instantiating the
26d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// definitions of member templates. For example:
27d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///
28d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// \code
29d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// template<typename T>
30d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// struct X {
31d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///   template<T Value>
32d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///   struct Y {
33d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///     void f();
34d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///   };
35d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// };
36d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// \endcode
37d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  ///
38d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// When instantiating X<int>::Y<17>::f, the multi-level template argument
39d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// list will contain a template argument list (int) at depth 0 and a
40d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  /// template argument list (17) at depth 1.
4124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  class MultiLevelTemplateArgumentList {
4224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  public:
4324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    typedef std::pair<const TemplateArgument *, unsigned> ArgList;
4424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
4524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  private:
46d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief The template argument lists, stored from the innermost template
47357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor    /// argument list (first) to the outermost template argument list (last).
48686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    SmallVector<ArgList, 4> TemplateArgumentLists;
49d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
50d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  public:
51d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief Construct an empty set of template argument lists.
52d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    MultiLevelTemplateArgumentList() { }
53d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
54d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief Construct a single-level template argument list.
55357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor    explicit
56d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    MultiLevelTemplateArgumentList(const TemplateArgumentList &TemplateArgs) {
5724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      addOuterTemplateArguments(&TemplateArgs);
58d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    }
59d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
60d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief Determine the number of levels in this template argument
61d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// list.
62d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    unsigned getNumLevels() const { return TemplateArgumentLists.size(); }
63d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
64d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief Retrieve the template argument at a given depth and index.
65d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    const TemplateArgument &operator()(unsigned Depth, unsigned Index) const {
66d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor      assert(Depth < TemplateArgumentLists.size());
6724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      assert(Index < TemplateArgumentLists[getNumLevels() - Depth - 1].second);
6824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      return TemplateArgumentLists[getNumLevels() - Depth - 1].first[Index];
69d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    }
70d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor
71d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    /// \brief Determine whether there is a non-NULL template argument at the
72d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    /// given depth and index.
73d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    ///
74d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    /// There must exist a template argument list at the given depth.
75d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    bool hasTemplateArgument(unsigned Depth, unsigned Index) const {
76d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor      assert(Depth < TemplateArgumentLists.size());
77d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor
7824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      if (Index >= TemplateArgumentLists[getNumLevels() - Depth - 1].second)
79d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor        return false;
80d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor
81d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor      return !(*this)(Depth, Index).isNull();
82d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    }
83d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
84d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief Clear out a specific template argument.
85d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    void setArgument(unsigned Depth, unsigned Index,
86d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                     TemplateArgument Arg) {
87d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      assert(Depth < TemplateArgumentLists.size());
88d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      assert(Index < TemplateArgumentLists[getNumLevels() - Depth - 1].second);
89d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      const_cast<TemplateArgument&>(
90d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                TemplateArgumentLists[getNumLevels() - Depth - 1].first[Index])
91d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        = Arg;
92d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    }
93d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
94d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// \brief Add a new outermost level to the multi-level template argument
95d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    /// list.
96d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    void addOuterTemplateArguments(const TemplateArgumentList *TemplateArgs) {
97910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgumentLists.push_back(ArgList(TemplateArgs->data(),
98910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                              TemplateArgs->size()));
9924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    }
10024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
10124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    /// \brief Add a new outmost level to the multi-level template argument
10224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    /// list.
10324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    void addOuterTemplateArguments(const TemplateArgument *Args,
10424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                   unsigned NumArgs) {
10524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      TemplateArgumentLists.push_back(ArgList(Args, NumArgs));
106d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor    }
107d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
108d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    /// \brief Retrieve the innermost template argument list.
10924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    const ArgList &getInnermost() const {
11024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      return TemplateArgumentLists.front();
111d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    }
112d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor  };
1138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  /// \brief The context in which partial ordering of function templates occurs.
1157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  enum TPOC {
1168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// \brief Partial ordering of function templates for a function call.
1178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    TPOC_Call,
1188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// \brief Partial ordering of function templates for a call to a
1198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// conversion function.
1208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    TPOC_Conversion,
1218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// \brief Partial ordering of function templates in other contexts, e.g.,
1228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// taking the address of a function template or matching a function
1238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    /// template specialization to a function template.
1248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    TPOC_Other
1258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  };
12602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
1277cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  // This is lame but unavoidable in a world without forward
1287cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  // declarations of enums.  The alternatives are to either pollute
1297cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  // Sema.h (by including this file) or sacrifice type safety (by
1307cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  // making Sema.h declare things as enums).
1317cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  class TemplatePartialOrderingContext {
1327cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall    TPOC Value;
1337cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  public:
1347cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall    TemplatePartialOrderingContext(TPOC Value) : Value(Value) {}
1357cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall    operator TPOC() const { return Value; }
1367cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall  };
1377cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall
13802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  /// \brief Captures a template argument whose value has been deduced
13902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  /// via c++ template argument deduction.
14002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  class DeducedTemplateArgument : public TemplateArgument {
14102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// \brief For a non-type template argument, whether the value was
14202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// deduced from an array bound.
14302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    bool DeducedFromArrayBound;
14402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
14502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  public:
14602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    DeducedTemplateArgument()
14702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      : TemplateArgument(), DeducedFromArrayBound(false) { }
14802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
14902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    DeducedTemplateArgument(const TemplateArgument &Arg,
15002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            bool DeducedFromArrayBound = false)
15102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      : TemplateArgument(Arg), DeducedFromArrayBound(DeducedFromArrayBound) { }
15202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
15302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// \brief Construct an integral non-type template argument that
154d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// has been deduced, possibly from an array bound.
15502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    DeducedTemplateArgument(const llvm::APSInt &Value,
15602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            QualType ValueType,
15702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                            bool DeducedFromArrayBound)
15802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      : TemplateArgument(Value, ValueType),
15902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        DeducedFromArrayBound(DeducedFromArrayBound) { }
16002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
16102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// \brief For a non-type template argument, determine whether the
16202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// template argument was deduced from an array bound.
16302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    bool wasDeducedFromArrayBound() const { return DeducedFromArrayBound; }
16402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
16502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// \brief Specify whether the given non-type template argument
16602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    /// was deduced from an array bound.
16702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    void setDeducedFromArrayBound(bool Deduced) {
16802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      DeducedFromArrayBound = Deduced;
16902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    }
17002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  };
1712a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
1722a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  /// \brief A stack-allocated class that identifies which local
1732a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  /// variable declaration instantiations are present in this scope.
1742a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  ///
1752a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  /// A new instance of this class type will be created whenever we
1762a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  /// instantiate a new function declaration, which will have its own
1772a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  /// set of parameter declarations.
1782a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  class LocalInstantiationScope {
17912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  public:
18012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// \brief A set of declarations.
181686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    typedef SmallVector<Decl *, 4> DeclArgumentPack;
18212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
18312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  private:
1842a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief Reference to the semantic analysis that is performing
1852a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// this template instantiation.
1862a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    Sema &SemaRef;
1872a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
18812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    typedef llvm::DenseMap<const Decl *,
18912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                           llvm::PointerUnion<Decl *, DeclArgumentPack *> >
19012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      LocalDeclsMap;
19112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
1922a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief A mapping from local declarations that occur
1932a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// within a template to their instantiations.
1942a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    ///
1952a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// This mapping is used during instantiation to keep track of,
1962a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// e.g., function parameter and variable declarations. For example,
1972a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// given:
1982a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    ///
1992a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \code
2002a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    ///   template<typename T> T add(T x, T y) { return x + y; }
2012a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \endcode
2022a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    ///
2032a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// when we instantiate add<int>, we will introduce a mapping from
2042a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// the ParmVarDecl for 'x' that occurs in the template to the
2052a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// instantiated ParmVarDecl for 'x'.
20612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    ///
20712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// For a parameter pack, the local instantiation scope may contain a
20812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// set of instantiated parameters. This is stored as a DeclArgumentPack
20912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// pointer.
21012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    LocalDeclsMap LocalDecls;
2112a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
21212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// \brief The set of argument packs we've allocated.
213686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    SmallVector<DeclArgumentPack *, 1> ArgumentPacks;
21412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
2152a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief The outer scope, which contains local variable
2162a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// definitions from some other instantiation (that may not be
2172a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// relevant to this particular scope).
2182a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope *Outer;
2192a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
2202a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief Whether we have already exited this scope.
2212a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    bool Exited;
2222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
2232a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief Whether to combine this scope with the outer scope, such that
2242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// lookup will search our outer scope.
2252a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    bool CombineWithOuterScope;
2262a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
227d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief If non-NULL, the template parameter pack that has been
228d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// partially substituted per C++0x [temp.arg.explicit]p9.
229d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    NamedDecl *PartiallySubstitutedPack;
230d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
231d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief If \c PartiallySubstitutedPack is non-null, the set of
232d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// explicitly-specified template arguments in that pack.
233d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument *ArgsInPartiallySubstitutedPack;
234d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
235d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief If \c PartiallySubstitutedPack, the number of
236d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// explicitly-specified template arguments in
237d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// ArgsInPartiallySubstitutedPack.
238d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    unsigned NumArgsInPartiallySubstitutedPack;
239d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
2402a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    // This class is non-copyable
2412a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope(const LocalInstantiationScope &);
2422a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope &operator=(const LocalInstantiationScope &);
2432a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
2442a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  public:
2452a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false)
2462a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope),
247d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Exited(false), CombineWithOuterScope(CombineWithOuterScope),
248d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        PartiallySubstitutedPack(0)
2492a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    {
2502a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      SemaRef.CurrentInstantiationScope = this;
2512a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    }
2522a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
2532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    ~LocalInstantiationScope() {
2542a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      Exit();
2552a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    }
256d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner
257d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    const Sema &getSema() const { return SemaRef; }
2582a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
2592a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    /// \brief Exit this local instantiation scope early.
2602a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    void Exit() {
2612a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      if (Exited)
2622a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall        return;
2632a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
26412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned I = 0, N = ArgumentPacks.size(); I != N; ++I)
26512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        delete ArgumentPacks[I];
26612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
2672a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      SemaRef.CurrentInstantiationScope = Outer;
2682a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      Exited = true;
2692a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    }
2702a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
27123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    /// \brief Clone this scope, and all outer scopes, down to the given
27223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    /// outermost scope.
27323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    LocalInstantiationScope *cloneScopes(LocalInstantiationScope *Outermost) {
27423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (this == Outermost) return this;
27523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *newScope =
27623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        new LocalInstantiationScope(SemaRef, CombineWithOuterScope);
27723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
27823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      newScope->Outer = 0;
27923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (Outer)
28023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        newScope->Outer = Outer->cloneScopes(Outermost);
28123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
28223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      newScope->PartiallySubstitutedPack = PartiallySubstitutedPack;
28323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      newScope->ArgsInPartiallySubstitutedPack = ArgsInPartiallySubstitutedPack;
28423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      newScope->NumArgsInPartiallySubstitutedPack =
28523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        NumArgsInPartiallySubstitutedPack;
28623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
28723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      for (LocalDeclsMap::iterator I = LocalDecls.begin(), E = LocalDecls.end();
28823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins           I != E; ++I) {
28923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        const Decl *D = I->first;
29023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored =
29123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          newScope->LocalDecls[D];
29223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        if (I->second.is<Decl *>()) {
29323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          Stored = I->second.get<Decl *>();
29423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        } else {
29523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          DeclArgumentPack *OldPack = I->second.get<DeclArgumentPack *>();
29623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          DeclArgumentPack *NewPack = new DeclArgumentPack(*OldPack);
29723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          Stored = NewPack;
29823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins          newScope->ArgumentPacks.push_back(NewPack);
29923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        }
30023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      }
30123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      return newScope;
30223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
30323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
30423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    /// \brief deletes the given scope, and all otuer scopes, down to the
30523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    /// given outermost scope.
30623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    static void deleteScopes(LocalInstantiationScope *Scope,
30723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                             LocalInstantiationScope *Outermost) {
30823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      while (Scope && Scope != Outermost) {
30923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        LocalInstantiationScope *Out = Scope->Outer;
31023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        delete Scope;
31123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Scope = Out;
31223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      }
31323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
31423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
31512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// \brief Find the instantiation of the declaration D within the current
31612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// instantiation scope.
31712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    ///
31812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// \param D The declaration whose instantiation we are searching for.
31912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    ///
32012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// \returns A pointer to the declaration or argument pack of declarations
32112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// to which the declaration \c D is instantiataed, if found. Otherwise,
32212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    /// returns NULL.
32312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    llvm::PointerUnion<Decl *, DeclArgumentPack *> *
32412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    findInstantiationOf(const Decl *D);
3252a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
3262a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    void InstantiatedLocal(const Decl *D, Decl *Inst);
32712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    void InstantiatedLocalPackArg(const Decl *D, Decl *Inst);
32812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    void MakeInstantiatedLocalArgPack(const Decl *D);
329d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
330d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief Note that the given parameter pack has been partially substituted
331d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// via explicit specification of template arguments
332d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// (C++0x [temp.arg.explicit]p9).
333d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    ///
334d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \param Pack The parameter pack, which will always be a template
335d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// parameter pack.
336d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    ///
337d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \param ExplicitArgs The explicitly-specified template arguments provided
338d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// for this parameter pack.
339d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    ///
340d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \param NumExplicitArgs The number of explicitly-specified template
341d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// arguments provided for this parameter pack.
342d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    void SetPartiallySubstitutedPack(NamedDecl *Pack,
343d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                     const TemplateArgument *ExplicitArgs,
344d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                     unsigned NumExplicitArgs);
345d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
346d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// \brief Retrieve the partially-substitued template parameter pack.
347d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    ///
348d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    /// If there is no partially-substituted parameter pack, returns NULL.
349d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    NamedDecl *getPartiallySubstitutedPack(
350d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                      const TemplateArgument **ExplicitArgs = 0,
351d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                           unsigned *NumExplicitArgs = 0) const;
3522a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  };
353d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
354d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  class TemplateDeclInstantiator
3558491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    : public DeclVisitor<TemplateDeclInstantiator, Decl *>
3568491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor  {
357d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Sema &SemaRef;
3588491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor    Sema::ArgumentPackSubstitutionIndexRAII SubstIndex;
359d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    DeclContext *Owner;
360d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
36123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    Sema::LateInstantiatedAttrVec* LateAttrs;
36223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    LocalInstantiationScope *StartingScope;
363d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
364d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// \brief A list of out-of-line class template partial
365d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// specializations that will need to be instantiated after the
366d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// enclosing class's instantiation is complete.
367686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    SmallVector<std::pair<ClassTemplateDecl *,
368d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                                ClassTemplatePartialSpecializationDecl *>, 4>
369d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      OutOfLinePartialSpecs;
370d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
371d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  public:
372d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
373d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
3748491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor      : SemaRef(SemaRef), SubstIndex(SemaRef, -1), Owner(Owner),
37523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        TemplateArgs(TemplateArgs), LateAttrs(0), StartingScope(0) { }
376d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
377d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // FIXME: Once we get closer to completion, replace these manually-written
378d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // declarations with automatically-generated ones from
379d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // clang/AST/DeclNodes.inc.
380d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
38157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *VisitLabelDecl(LabelDecl *D);
382d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
383d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
384d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
385162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
3863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
387d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
388d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
389d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
39087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
391d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
392d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
393d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
394d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitFriendDecl(FriendDecl *D);
395d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
396d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                            TemplateParameterList *TemplateParams = 0);
397d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
398d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
399af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                             TemplateParameterList *TemplateParams = 0,
400af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                             bool IsClassScopeSpecialization = false);
401d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
402d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
403d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
404d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
405d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
406d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
407d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
408d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
409d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
410d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
411d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
412d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
413d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitUsingDecl(UsingDecl *D);
414d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
415d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
416d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
417af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    Decl *VisitClassScopeFunctionSpecializationDecl(
418af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      ClassScopeFunctionSpecializationDecl *D);
419d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
420d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
421d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    Decl *VisitDecl(Decl *D) {
422d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
423d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                                                   DiagnosticsEngine::Error,
424d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                                                   "cannot instantiate %0 yet");
425d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
426d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        << D->getDeclKindName();
427d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
428d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      return 0;
429d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    }
430d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
43123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    // Enable late instantiation of attributes.  Late instantiated attributes
43223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    // will be stored in LA.
43323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    void enableLateAttributeInstantiation(Sema::LateInstantiatedAttrVec *LA) {
43423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs = LA;
43523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      StartingScope = SemaRef.CurrentInstantiationScope;
43623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
43723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
43823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    // Disable late instantiation of attributes.
43923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    void disableLateAttributeInstantiation() {
44023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs = 0;
44123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      StartingScope = 0;
44223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
44323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
44423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    LocalInstantiationScope *getStartingScope() const { return StartingScope; }
44523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
446d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    typedef
447686775deca8b8685eb90801495880e3abdd844c2Chris Lattner      SmallVectorImpl<std::pair<ClassTemplateDecl *,
448d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                                     ClassTemplatePartialSpecializationDecl *> >
449d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        ::iterator
450d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      delayed_partial_spec_iterator;
451d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
452d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// \brief Return an iterator to the beginning of the set of
453d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// "delayed" partial specializations, which must be passed to
454d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// InstantiateClassTemplatePartialSpecialization once the class
455d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// definition has been completed.
456d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    delayed_partial_spec_iterator delayed_partial_spec_begin() {
457d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      return OutOfLinePartialSpecs.begin();
458d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    }
459d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
460d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// \brief Return an iterator to the end of the set of
461d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// "delayed" partial specializations, which must be passed to
462d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// InstantiateClassTemplatePartialSpecialization once the class
463d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    /// definition has been completed.
464d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    delayed_partial_spec_iterator delayed_partial_spec_end() {
465d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      return OutOfLinePartialSpecs.end();
466d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    }
467d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
468d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Helper functions for instantiating methods.
469d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
470686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                             SmallVectorImpl<ParmVarDecl *> &Params);
471d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
472d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
473d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
474d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    TemplateParameterList *
475d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      SubstTemplateParams(TemplateParameterList *List);
476d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
477d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    bool SubstQualifier(const DeclaratorDecl *OldDecl,
478d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                        DeclaratorDecl *NewDecl);
479d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    bool SubstQualifier(const TagDecl *OldDecl,
480d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                        TagDecl *NewDecl);
481d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
4823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Decl *InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias);
483d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    ClassTemplatePartialSpecializationDecl *
484d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    InstantiateClassTemplatePartialSpecialization(
485d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
486d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
4878491ffe86c50241b47c6d7ef8cd9ee00f5e675daDouglas Gregor  };
488d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor}
489d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor
490d1102433214bd33b5bef5b493944292a1e82c2fbDouglas Gregor#endif // LLVM_CLANG_SEMA_TEMPLATE_H
491