SemaTemplateInstantiateDecl.cpp revision 3b85ecf2049c8670eba30d0c06f28f64168af9b8
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
137d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall#include "Lookup.h"
14aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
19a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
20c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
2183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
2685b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramer  class TemplateDeclInstantiator
27b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
30d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    void InstantiateAttrs(Decl *Tmpl, Decl *New);
33d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
38d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
397e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
42390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
43390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
444f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
454f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
516477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
53a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
54a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
55d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
56d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
57d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
58615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
60bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
616477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
62e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
637974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
65d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
66e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
689106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
6948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
70ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
71ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
7748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
7848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
86fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
87fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
88fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
89fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
91ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
93e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
95e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
96e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
97ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
98ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
99ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
105d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
106d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlssonvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
107d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
108d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
109d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
110d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
111d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
112d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
113d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
114d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
115d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
116d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1174f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1184f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1194f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1204f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1214f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1224f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1234f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1244f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1254f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1264f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1274f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1284f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
131a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
132ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
133ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
134ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
135ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
137a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
144ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1485126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1495126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs);
1505126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1515126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1525126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
15346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
15417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
1606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
1616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
1626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
1636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
1646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
1656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
1666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
1676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
1696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
1706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
1716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
1726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
1746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
1756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
1766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
1786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
1796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
1816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
1826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
1836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
1846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
1866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
1876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1883d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
189ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
190a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
1910a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
1920a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
1930a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
1940a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
1953d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1963d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
197b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1983d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1993d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2000a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
201a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
2023d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2033d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2043d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
21346460a68f6508775e98c19b4bb8454bb471aac24John McCall
214390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
215390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
2163d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
2176826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
2186826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
2196826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName);
2206826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
2237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
2247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
2257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
2277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
229251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
230251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
231251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
232251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
233cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
234251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
2361f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
2371f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
2381f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
2391f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
2401f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Extract the initializer, skipping through any temporary-binding
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // expressions and look at the subexpression as it was written.
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *DInit = D->getInit();
2446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
2456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = Binder->getSubExpr();
2466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
2476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = ICE->getSubExprAsWritten();
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer is a parenthesized list of expressions that is
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // type-dependent. Instantiate each of the expressions; we'll be
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // performing direct initialization with them.
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getExprs(),
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getNumExprs(),
2586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
2616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getLParenLoc(),
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
2646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
2656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getRParenLoc());
26683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer resolved to a constructor. Instantiate the constructor
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // arguments.
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getArgs(),
2756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getNumArgs(),
2766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        if (D->hasCXXDirectInitializer()) {
2796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeLParenLoc =
2806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor            SemaRef.PP.getLocForEndOfToken(D->getLocation());
2816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
2826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                          : CommaLocs.back();
2836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeLParenLoc,
2856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                move_arg(InitArgs),
2866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                CommaLocs.data(),
2876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeRParenLoc);
2886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else if (InitArgs.size() == 1) {
2896eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          Expr *Init = (Expr*)(InitArgs.take()[0]);
2906eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
2916eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       SemaRef.Owned(Init),
2926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       false);
2936eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else {
2946eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          assert(InitArgs.size() == 0);
2956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
296a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor        }
2976eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      }
2986eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
2996eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      OwningExprResult Init
3006eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
301a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor
3026eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // FIXME: Not happy about invalidating decls just because of a bad
3036eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // initializer, unless it affects the type.
3046eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (Init.isInvalid())
3056eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        Var->setInvalidDecl();
3066eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      else
3076eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
3086eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                     D->hasCXXDirectInitializer());
3096eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3106eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3111f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
31265b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
31365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
3143d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3153d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3163d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3173d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
320a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
32107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
32207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
32307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
32407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
325a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
32607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
32707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
33507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
344ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
345ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
348ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
353e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
35607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
35707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
362ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
3638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
3648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
365663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
366663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
367f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
368663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
370d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
371d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
372f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
373f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
375f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
376f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
377f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
3788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
380f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
38146460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
382f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
3858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
38702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
38802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
38902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
39002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
39102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
39202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
39302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
39402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
39502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
396fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
39702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
39802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
399fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
40002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
40102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
40202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
40302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
40402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
405a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
406a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
407a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
408e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    Decl *NewND;
409e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall
410e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    // Hack to make this work almost well pending a rewrite.
411e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    if (ND->getDeclContext()->isRecord())
412e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
4137557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor    else if (D->wasSpecialization()) {
4147557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      // Totally egregious hack to work around PR5866
4157557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      return 0;
4167557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor    } else
417e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = Visit(ND);
41802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
419c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
42002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
42102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
42402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
42502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
4265fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
42702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
42802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
429fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
430fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
434ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
435ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
438ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
44243d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
44343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
4441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
446b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
447b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
4488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
4498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
454741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
4568dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
45706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
45817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
4608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4610ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
46417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
46517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
469ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
470ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
472ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
474ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
475ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
4928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
4963b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
49717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
498b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
504fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
505c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
506c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
507fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
508fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
5098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
5118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5136477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
5146477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
5156477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
5166477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
5176477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
518ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
519ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
520ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
521ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
524ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
525ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
526ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
527ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
528ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
529ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
530ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
533e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
534550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
535550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
536550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
537e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
538ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
540d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
541e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
542e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
543e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
544e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
545e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
546f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
547f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
548e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
549e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
550e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
551e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
552e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
553e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  if (D->getFriendObjectKind())
554e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setObjectOfFriendDecl(true);
555e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  else
556e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
557e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
558f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
559f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
560f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
561f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
562259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
563259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  if (Inst->getFriendObjectKind()) {
564e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
565259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
566e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
56746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Inst->setAccess(D->getAccess());
568e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
569ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
570ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
571ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
572ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
573ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
574ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
575ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
576ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
577ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
578ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
579ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
580ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
581ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
582ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
583ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
584ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
585ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
586e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
587e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
588e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
589d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
5907974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
5917974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
592ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
593ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
594ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
595ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
596ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
597ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
598ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
599ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
600ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
601ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
602ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
603ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
604ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
605ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
606ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
607ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
609ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
610ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
611ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
612ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
613ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
614ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
6157974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
6167974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
6177974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
6187974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
619d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
620550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
621550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
622550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
623550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
624550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
625550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
626d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
627d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
629d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
631a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
632a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
633a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
634a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
635a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
636a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
637a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
638a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
639a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
640a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
641d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
642d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
64346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
64446460a68f6508775e98c19b4bb8454bb471aac24John McCall
6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
646d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
64737d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
648a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
64937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
650a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
651a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
652e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
653e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
654e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
655e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
656e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall      !(InstTemplate->getFriendObjectKind() &&
657e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall        !D->getTemplatedDecl()->isThisDeclarationADefinition()))
658a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
659a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
660a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
661a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
662a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
663d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
664d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
665d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
666d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
667d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
668d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
669d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
6706c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
6716c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
6726c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
6736c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
6746c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
6756c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
676d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
677d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
679741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
680741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
681d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
682eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
683eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
684eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
685eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
686eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
687d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
688f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
689d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
69002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
69102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
69202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
69302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
69402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
695d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
696d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
69717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
698d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
699d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
700d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
70102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
70202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
70302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
70402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
70502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
7067557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
707a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
708127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
709127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
710127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
711127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
712a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
713127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
715d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
716d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
717828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
721127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
724127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
725127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
726127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
72979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
73079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
73179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
733e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
734ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
735e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
7362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
737fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
738e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
739e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
740e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                    TemplateArgs);
74102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
743a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                           D->getDeclName(), T, D->getTypeSourceInfo(),
744a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
7450130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
74602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
748e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
749e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
750e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
751e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
75202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
753a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
754a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
755a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
756a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
757a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
758a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
759a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
760a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
761a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
762a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
763a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
764a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
765a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
766a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
767a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
768a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
769a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
770a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
771a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
772a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
773a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
77466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
77566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
77666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
77766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                FunctionTemplate,
77866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
77966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
78002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
781a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
782e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
783e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
7841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
785e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
786e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
787a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
7886826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
7896826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
7906826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
791a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
792a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
793a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
794a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
7956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
796a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
797a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
798a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
799a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
800a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
8016826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
8026826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
803a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
804a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
8059f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
8069f54ad4381370c6b771424b53d219e661d6d6706John McCall                                   false, Redeclaration,
807e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
808e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
809a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
810a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
811a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  NamedDecl *FromFriendD
812a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
813a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FromFriendD->getFriendObjectKind()) {
814a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
8156826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
816a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
817a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
818a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
819a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
820a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
821a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
822a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
823a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
824a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!Owner->isDependentContext() && !PrevDecl)
825a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
826a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
827a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!TemplateParams)
828a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
829a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
830a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
831e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
832e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
8332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
834d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
835d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
836d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
8376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
8386b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
839d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
8401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
8411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
842d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
8436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
845d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
846d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
8476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
8516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
8546b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
8556b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
8566b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
8576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
85879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
85979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
86079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
86179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
86248dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
8630ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
864ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
8652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
8662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
8672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
8692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
870dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
872dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
87317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
874dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
875dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
876dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
880a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                        Constructor->getTypeSourceInfo(),
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
8820130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
88317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
88417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
88517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
88617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
88717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
88817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
8890130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
89065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
89265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
893183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
89465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
89565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
89665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
89765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
898a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                       T, Conversion->getTypeSourceInfo(),
8990130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
90065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
901dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
903a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                   D->getDeclName(), T, D->getTypeSourceInfo(),
9040130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
905dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
9066b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
907d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
908d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
909d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
911d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
912d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
913d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
914d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
915d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
916d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
917d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
918d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
919d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
920d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
921d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
922d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
924d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
925d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
9261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
927d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
92866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
92966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
93066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
93166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              FunctionTemplate,
93266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
93366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
93466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else {
93566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
9362db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
93766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
93866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
9407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
9417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
9427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
9437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
9465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
9475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
948beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
9495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
9505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
9515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
9522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9536826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
9546826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
956d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
9576826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, Owner);
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
959dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
960dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
961dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
962dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
9636826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
9646826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
965dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
9662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
96765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
96865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
9699f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
97065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
97165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
9724ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
9734ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
9744ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
97546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
97646460a68f6508775e98c19b4bb8454bb471aac24John McCall
9776826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
978a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
979dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
9822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
9832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
984615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
985dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
986615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
987615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
98803b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
98917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
99003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
99103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
992bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
99365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
994bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
995bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
9966477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
99758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
998a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
99958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
100058e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
100158e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
100258e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
100358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
100458e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
100558e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
100658e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
100758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
100858e4677a948e80c92deeebbcd3bdd9266adda798John McCall
100958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
10102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
10112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
101258e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
10132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
101558e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
10167a9813ced7455b8a33a807489ca77a4f809c8a73John McCall    = ParmVarDecl::Create(SemaRef.Context,
10177a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          SemaRef.Context.getTranslationUnitDecl(),
10187a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          D->getLocation(),
101958e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
10202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10219351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
1022f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
1023f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
10240ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
10250ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
10260ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
10272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
10282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
10292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
103048dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
10312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
10322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
10332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1034e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1035e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1036e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1037e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1038e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1039e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1041e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1042e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1043550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1044e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1045e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1046e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1047e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
10480f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
10490f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1050e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1051550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1052550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1053550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1054550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1055e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1056e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1057e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
105833642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
105933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
106033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
106133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1062a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
106333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
106433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
106533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
106633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
106733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
106833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
106933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
107033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
107133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
107233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
107333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
107433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
107533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
107633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
107733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
107833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
107933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
108033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
108133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
108233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
108333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
108433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
108533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
108633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
108733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
108833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
108933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
109033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1091550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1092550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1093550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1094550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
109533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
109633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
109733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
10980dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
10999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
11009106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
11019106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
11029106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
11039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
11049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
11059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
11069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
11079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
11089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
11099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
11109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
11119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
11129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
11149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
11159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
11169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
11179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
11189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
11199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
11219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
11229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
11239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
11259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
11269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
112748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
112848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
112948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
113048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
113148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
113248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
113348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
113448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
113548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
113648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
113748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
113848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
113948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
114048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1141ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1142ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1143ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1144ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11459f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
11469f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
11479f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
11489f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
11499f54ad4381370c6b771424b53d219e661d6d6706John McCall
11509f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
11519f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
11529f54ad4381370c6b771424b53d219e661d6d6706John McCall
1153ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1154ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1155ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1156ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1157ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1158ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1159ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1160ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1161ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1162ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1163ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
11649f54ad4381370c6b771424b53d219e661d6d6706John McCall
11659f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
11669f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
11679f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
11689f54ad4381370c6b771424b53d219e661d6d6706John McCall
11699f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
11709f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
11719f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
11729f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
11739f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
11749f54ad4381370c6b771424b53d219e661d6d6706John McCall
11759f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
11769f54ad4381370c6b771424b53d219e661d6d6706John McCall
11779f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
11789f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1179ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1180ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
11819f54ad4381370c6b771424b53d219e661d6d6706John McCall
1182ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1183ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1184ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1185ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11869f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
11879f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
11889f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
11899f54ad4381370c6b771424b53d219e661d6d6706John McCall
1190323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1191323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
11929f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
11939f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
11949f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
11959f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
11969f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
11979f54ad4381370c6b771424b53d219e661d6d6706John McCall      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
11989f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
11999f54ad4381370c6b771424b53d219e661d6d6706John McCall
12009f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
12019f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
12029f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
12039f54ad4381370c6b771424b53d219e661d6d6706John McCall
12049f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
12059f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
12069f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1207323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1208323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1209323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
12109f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1211ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1212ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1213ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1214ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1215ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
12169f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
12179f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1218ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1219ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
12217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
12227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
12237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
12257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
12267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
12277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
12287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
12307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
12317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
12327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
12347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
12397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1240ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1241ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
12437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
12447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
12467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
12500dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
12510dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
12520dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12540dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
12550dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
12560dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
12599488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
12640d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1265ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1266ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
12680dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
12690dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1270ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1271d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
12727e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
12738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
12748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
12758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1276e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1277e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1278e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1279e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1280e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1281e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1282e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1283ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1284e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1285e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1286e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1287e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1288bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1289e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1290e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1291e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1292e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1293bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1294e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
12959148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1296e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1297e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1298e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1299e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1300e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1301e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1302e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1303e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1304e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1305e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1306e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1307e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1308e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1309e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1310e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1311e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1313e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1315ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1316ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1317ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1318ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1319ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1320ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1321ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1322ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1323ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1324ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1325ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1326ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1327ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1328550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1329550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1330550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1331550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1332550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1333ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1334ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1335ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1336ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1337ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1338ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1339ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1340ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1341ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1342833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1343833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1344833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1345833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1346d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1347833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1348d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1349d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1350ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1351d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1357ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1358ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1360ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1361d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1363ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1364ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1365ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1366ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1370ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1371ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1372ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1373ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1374ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1376ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1377ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1378ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1379ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1380ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1381ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1382ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1383ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1384ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1386ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1387ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1391ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
1392ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType WrittenTy
1393ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1394d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1395ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1396ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1397ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1398ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1399ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1401ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1405ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1406ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1407ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1408ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1409ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1412ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1414ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1415ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1416ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1417ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1418ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1419ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1420ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1422ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1423ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1424ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1425ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1426ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1427ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1428d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
1429ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1430ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1431ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1432ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1433ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1437ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1438ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1439ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1440ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
1441ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
14425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
1443ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
14445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
14455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
14465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1447ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
14485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1450ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
14515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
14525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
14535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1454ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
14557e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
14560ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
14585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
14595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
14606477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
14615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
14625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
14635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
1465ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
1466ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
1467ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
14685545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
14695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
14715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
14725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
14745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
14765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
14775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
14785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
14805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
14815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
14825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1483183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
14845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
1486ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1487ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
14885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
14895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
14905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1491beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
14925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
14935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
14945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
14955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1497e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1499e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1502e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1503e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1504e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
15051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1506cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1507cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1508cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1511cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1512cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1513cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1514cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1515cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1516cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1517cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1519cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1521cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1522bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1523cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1524cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1525f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1526cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1527cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15290ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
15300ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
15310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15320ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
15330ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
15340ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
15350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
15360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
15370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
15380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
15390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
15400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
15410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
15420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
15430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
15440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
15450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
15470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
15480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
15500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
15520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
15530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
15540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
15550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
15560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
15570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
15580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
15590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
15600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
15610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
15620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1563ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getNoReturnAttr(),
1564ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getCallConv()));
15650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
15660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1567e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1568e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1569e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
15705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
15715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
15725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
15735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
15745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
15775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1578e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1579e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
15825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1583e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1584e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
15855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
15875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
15885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
15895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1590a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1591a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1592a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1593a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1594b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1595b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1596b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1597b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1598a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1599b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1600b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1601b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1602b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1603b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1604e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1605e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1606e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1607e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1608f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1609b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1610e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1611e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
161254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
161354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
161454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
16156fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1617251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1618251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1619251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1620251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
16211eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
16223b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
16231eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
16241eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
16256fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
16261eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1627e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1628e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1629e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1630e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1631e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1632e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1633e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1634e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1635e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1636e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1637e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1638e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1639e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1640e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1641e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1642e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
16431eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1644e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
16451eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1646d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1647d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1649d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1651d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
16527ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1653d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1655f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1656f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1657f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1658b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1659b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1660b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1661b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1662b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1663b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1664b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1666e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1667e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
166854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
166960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
167060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
167160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
167260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
167360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
167460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
167560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
167660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
167854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
167954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
168054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
168154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
168254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
168354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1684b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1685b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1686b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1687b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1688b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
16891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1690090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1691090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1692090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
16931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1694090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1695090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1696090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
16971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1700090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1701e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
170252604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
170352604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
170452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1706e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1707b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1708b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1709aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1710aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1711aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
171360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
171460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
171560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
171660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
171760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
1718b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1719b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
17201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1721b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1723b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1724b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1725b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1726a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1727a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1728a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1729a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1730a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
17317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
17327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
17337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
17347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
17367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
17377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
17397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1740e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1741e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1742e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1743e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
17447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
17457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
17467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1747e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1748e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
17497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
17507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
17537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
17547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
17550d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
17560d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17580d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
17597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
17607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
17611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
1763e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
17640d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
1765e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
1766e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
1767e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
1768e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1769e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1770e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
17717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
17737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1774251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
17751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1776251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1777251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1778251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
1779251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1780251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1781251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
17821028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
1783251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1784251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
17851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
17877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
17887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
17917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
17927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
17937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
17947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
17957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
17987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
17997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
18007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
18011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18021028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
1803ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
18047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
18057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
18067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
18077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
18081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    Var->setPreviousDeclaration(OldVar);
1809583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1810583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
1811583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1812583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
18137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
18147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
18157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
18161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
18187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
18207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
18211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
18237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
18241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1825a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1826815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1827090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1828090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1829090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1830090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1832090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1833090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1834090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1835090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
183672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
183772f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1838090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1839090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1840090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
18411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1842090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1843090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1844090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1845090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1846090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1847090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1848090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1849090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1850090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1851090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1852090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1853090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1854090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1855090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1856a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
1857802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
1858802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
1859802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
1860a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
1861802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
1862802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
1863802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
1864802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
1865a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
18661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1867090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1868802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
1869090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1870090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1871090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
18729988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
18731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18749988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
18759988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1876e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
18779988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
1878e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1879e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
18801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1882090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1883090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1884802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
1885090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1886090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1887090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1888090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1889090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1890090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1891090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1892090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
18931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1894090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1895090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1896090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
18971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1898090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1900090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1901090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       NewInits.data(), NewInits.size());
1903090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1904090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
190552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
190652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
190752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
190852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
190952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
191052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
191152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
191252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
191352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
191452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
191552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
191652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
191752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
191852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
191952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
19200d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
19210d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
19220d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
19230d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
19240d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
19250d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
19260d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
19270d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
19280d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
19290d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
19300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
19310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
19320d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1933ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
1934ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1935ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
1936ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
1937ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1938ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
1939ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
1941ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
1942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
1944ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
1945ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1946ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1947ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1948ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
194952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
195052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
195152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
195252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
195352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
195452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
195552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
195652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
195752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
195852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
195952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
196052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
196152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
196252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
196352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
196452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
196552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
196652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
196752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
196852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
196952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
197052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
197152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
197252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
197352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
197452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
197552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
197652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
197752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
197852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
197952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
198052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
198152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
198252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
198352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
198452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
198552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
198652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
198752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1988ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
1989ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
1990ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
1991ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
1992ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1993ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1994ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
1995ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
1996ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
1997ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
1998ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1999ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
20017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
20027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2003ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
20047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
20057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
20067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
20070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
20080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2009ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
20100d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
20110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
201252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
201352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
201452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
201552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
201652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
201752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
201852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
201952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
202052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
202152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
202252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
202352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
202452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
202552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
202652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2027ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2028ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2029815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
20300d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
20317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
20327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
20337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
20357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
20367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
20377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
20387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
20397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
20400d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20410d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
20420d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
20430d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2044815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
20450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
20460d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
20471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
204852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
204952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
20501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
205152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
205252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2053815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
205452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
205552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2056815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
20577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
205852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
205952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
206052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
206152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
206252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2063a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
20640d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
20650d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
20660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2067ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2068ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2069ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2070ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2071ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2072d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2073d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2074d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
20751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2076d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2077d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2078d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2080ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2081ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2082ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2083ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2084ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2085ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2087815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2088815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2089815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2090815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
20911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2092815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2093815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2094815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2095815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2096815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2097815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2098815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2099815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2100815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2101815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
210202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
210302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
210402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
210502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
2106e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2107e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
210802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
2109e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
211002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
211102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
211202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
211302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2114ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2115ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2116815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2117815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2118815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2119815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2120815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2121815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2122815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2123815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2124815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2125815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2126815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2127815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2128815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2129815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2130815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2131815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2132815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2133815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2134815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2135815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2136815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2137815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2138ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2139ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
2140e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2141e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2143550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
2144550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2145550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
21462bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
21472bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
21482bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
21492bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2150815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2151e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2152e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2153e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2154e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2155e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // If the RecordDecl is actually the injected-class-name or a "templated"
2156e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // declaration for a class template or class template partial
2157e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // specialization, substitute into the injected-class-name of the
2158e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // class template or partial specialization to find the new DeclContext.
2159e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2160e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2161e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2162e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
2163e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
2164e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2165e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2166e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = Context.getTypeDeclType(Record);
2167e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
2168e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    }
2169e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2170e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
2171e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // Substitute into the injected-class-name to get the type corresponding
2172e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // to the instantiation we want. This substitution should never fail,
2173e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // since we know we can instantiate the injected-class-name or we wouldn't
2174e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // have gotten to the injected-class-name!
2175e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
2176e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // instantiation in the common case?
2177e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2178e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2179e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2180e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2181e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2182e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2183e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2184e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2185e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // We are performing "partial" template instantiation to create the
2186e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // member declarations for the members of a class template
2187e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // specialization. Therefore, D is actually referring to something in
2188e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the current instantiation. Look through the current context,
2189e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // which contains actual instantiations, to find the instantiation of
2190e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the "current instantiation" that D refers to.
21911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
219252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
21931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
219452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2195e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2196e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
219752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
219852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
219952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
22001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(false &&
220152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
2202e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return Record;
220352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
2204e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2205e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2206e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2207e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
220852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2209e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2210e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2211e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2212e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
221444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2216815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2217815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2218815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2219815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
2220815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2221815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
222217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2223815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2224815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2225815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2226815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2227815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2228815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2229815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2230815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2231815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2232815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
22331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
223417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
223517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2236815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22389f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
22399f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert((Result || isa<UsingShadowDecl>(D))
22409f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
22419f54ad4381370c6b771424b53d219e661d6d6706John McCall
2242815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2243815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2244815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2245815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2246815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2247d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2249d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
225060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
225160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
225260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
225360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
225460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
225560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
225660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
225760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
225860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
225960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
226060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
226160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
22621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
22647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
22651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2266c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2267c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2268c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
22691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22706fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2271b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
22727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
22737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
22741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
22767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
22777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2278c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
22791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2280c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2281c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2282c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2283c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2286d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2287d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
2288