SemaTemplateInstantiateDecl.cpp revision 7557a1348d2821dce126a778aa7acd7a00b814fd
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
14817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
1546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
1556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
1566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
1576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
1586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
1596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
1606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
1616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
1636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
1646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
1656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
1666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
1686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
1696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
1706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
1726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
1736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
1756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
1766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
1776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
1786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
1806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
1816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1823d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
183ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
184a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
1850a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
1860a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
1870a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
1880a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
1893d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1903d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
191b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1923d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1933d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
1940a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
195a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
1963d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1983d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
207390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
2083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
2096826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
2106826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
2116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName);
2126826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
2157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
2167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
2177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
2187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
2197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
222251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
223251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
224251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
225cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
226251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
2281f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
2291f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
2301f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
2311f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
2321f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
2336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Extract the initializer, skipping through any temporary-binding
2346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // expressions and look at the subexpression as it was written.
2356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *DInit = D->getInit();
2366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
2376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = Binder->getSubExpr();
2386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
2396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = ICE->getSubExprAsWritten();
2406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer is a parenthesized list of expressions that is
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // type-dependent. Instantiate each of the expressions; we'll be
2446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // performing direct initialization with them.
2456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getExprs(),
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getNumExprs(),
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getLParenLoc(),
2556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getRParenLoc());
25883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
2596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
2606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer resolved to a constructor. Instantiate the constructor
2616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // arguments.
2626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getArgs(),
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getNumArgs(),
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        if (D->hasCXXDirectInitializer()) {
2716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeLParenLoc =
2726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor            SemaRef.PP.getLocForEndOfToken(D->getLocation());
2736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
2746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                          : CommaLocs.back();
2756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeLParenLoc,
2776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                move_arg(InitArgs),
2786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                CommaLocs.data(),
2796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeRParenLoc);
2806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else if (InitArgs.size() == 1) {
2816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          Expr *Init = (Expr*)(InitArgs.take()[0]);
2826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
2836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       SemaRef.Owned(Init),
2846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       false);
2856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else {
2866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          assert(InitArgs.size() == 0);
2876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
288a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor        }
2896eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      }
2906eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
2916eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      OwningExprResult Init
2926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
293a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor
2946eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // FIXME: Not happy about invalidating decls just because of a bad
2956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // initializer, unless it affects the type.
2966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (Init.isInvalid())
2976eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        Var->setInvalidDecl();
2986eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      else
2996eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
3006eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                     D->hasCXXDirectInitializer());
3016eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3026eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3031f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
30465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
30565b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
3063d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3073d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3093d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
312a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
31307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
31407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
31507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
31607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
317a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
31807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
31907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
32707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
3338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
3348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
3358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
336ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
337ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
340ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
345e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
34807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
34907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
354ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
357663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
358663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
359f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
360663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
362d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
363d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
364f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
365f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
368f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
369f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
373f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
3748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
3768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
37802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
37902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
38002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
38102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
38202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
38302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
38402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
38502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
38602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
387fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
38802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
38902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
390fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
39102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
39202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
39302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
39402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
39502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
396a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
397a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
398a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
399e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    Decl *NewND;
400e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall
401e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    // Hack to make this work almost well pending a rewrite.
402e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    if (ND->getDeclContext()->isRecord())
403e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
4047557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor    else if (D->wasSpecialization()) {
4057557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      // Totally egregious hack to work around PR5866
4067557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      return 0;
4077557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor    } else
408e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = Visit(ND);
40902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
410c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
41102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
41202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
41502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
41602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
4175fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
41802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
41902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
420fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
421fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
425ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
426ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
429ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
43343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
43443d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
437b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
438b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
4448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
445741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
4468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
4478dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
44806c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
44917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4520ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
45517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
45617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
460ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
461ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
463ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
466ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
48717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
488b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
493c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
494fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
495c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
496c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
497fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
498fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
5018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5036477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
5046477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
5056477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
5066477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
5076477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
510ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
511ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
512ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
513ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
514ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
515ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
517ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
518ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
519ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
520ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
521ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
523e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
524550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
525550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
526550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
527e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
528ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
530d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
531e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
532e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
533e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
534e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
535e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
536f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
537f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
538e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
539e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
540e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
541e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
542e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
543e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  if (D->getFriendObjectKind())
544e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setObjectOfFriendDecl(true);
545e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  else
546e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
547e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
548f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
549f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
550f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
551f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
552259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
553259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  if (Inst->getFriendObjectKind()) {
554e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
555259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
556e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
557e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
558ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
559ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
560ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
561ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
562ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
563ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
564ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
565ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
566ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
567ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
568ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
569ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
570ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
571ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
572ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
573ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
574ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
575e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
576e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
577e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
578d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
5797974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
5807974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
581ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
582ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
583ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
584ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
585ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
586ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
587ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
588ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
589ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
590ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
591ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
592ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
593ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
594ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
595ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
596ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
597ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
598ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
599ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
600ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
601ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
602ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
603ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
6047974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
6057974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
6067974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
6077974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
609550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
610550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
611550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
612550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
613550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
614550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
618d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
619ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
620a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
621a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
622a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
623a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
624a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
625a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
626a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
627a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
628a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
629a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
630d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
631d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
633d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
63437d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
635a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
63637d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
637a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
638a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
639e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
640e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
641e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
642e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
643e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall      !(InstTemplate->getFriendObjectKind() &&
644e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall        !D->getTemplatedDecl()->isThisDeclarationADefinition()))
645a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
646a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
647a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
648a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
649a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
650d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
651d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
652d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
653d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
654d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
655d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
656d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
6576c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
6586c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
6596c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
6606c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
6616c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
6626c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
663d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
664d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
666741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
667741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
668d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
669eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
670eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
671eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
672eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
673eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
674d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
675f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
676d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
67702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
67802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
67902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
68002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
68102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
682d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
683d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
68417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
685d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
686d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
687d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
68802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
68902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
69002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
69102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
69202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
6937557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
694a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
695127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
696127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
697127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
698127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
699a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
700127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
702d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
703d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
704828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
708127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
710127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
711127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
712127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
713127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
715550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
717e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
718ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
719e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
7202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
721fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
722e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
723e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
724e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                    TemplateArgs);
72502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
727a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                           D->getDeclName(), T, D->getTypeSourceInfo(),
728a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
7290130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
73002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
7311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
732e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
733e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
734e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
735e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
73602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
737a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
738a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
739a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
740a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
741a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
742a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
743a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
744a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
745a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
746a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
747a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
748a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
749a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
750a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
751a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
752a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
753a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
754a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
755a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
756a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
757a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
75866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
75966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
76066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
76166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                FunctionTemplate,
76266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
76366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
76402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
765a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
766e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
767e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
769e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
770e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
771a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
7726826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
7736826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
7746826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
775a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
776a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
777a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
778a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
7796826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
780a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
781a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
782a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
783a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
784a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
7856826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
7866826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
787a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
788a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
7899f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
7909f54ad4381370c6b771424b53d219e661d6d6706John McCall                                   false, Redeclaration,
791e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
792e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
793a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
794a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
795a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  NamedDecl *FromFriendD
796a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
797a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FromFriendD->getFriendObjectKind()) {
798a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
7996826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
800a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
801a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
802a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
803a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
804a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
805a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
806a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
807a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
808a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!Owner->isDependentContext() && !PrevDecl)
809a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
810a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
811a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!TemplateParams)
812a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
813a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
814a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
815e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
816e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
8172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
818d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
819d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
820d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
8216b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
8226b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
823d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
8251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
826d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
8276b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
829d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
830d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
8316b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
8356b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
8386b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
8396b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
8406b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
8416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
842550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
84348dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
8440ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
845ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
8462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
8472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
8482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
8502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
851dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
853dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
85417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
855dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
856dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
857dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
8601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
861a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                        Constructor->getTypeSourceInfo(),
8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
8630130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
86417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
86517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
86617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
86717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
86817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
86917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
8700130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
87165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
87365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
874183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
87565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
87665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
87765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
87865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
879a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                       T, Conversion->getTypeSourceInfo(),
8800130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
88165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
882dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
884a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                   D->getDeclName(), T, D->getTypeSourceInfo(),
8850130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
886dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
8876b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
888d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
889d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
890d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
892d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
893d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
894d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
895d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
896d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
897d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
898d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
899d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
900d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
901d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
902d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
903d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
905d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
906d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
908d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
90966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
91066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
91166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
91266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              FunctionTemplate,
91366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
91466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
91566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else {
91666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
9172db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
91866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
91966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
9217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
9227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
9237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
9247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
9275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
9285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
929beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
9305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
9315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
9325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
9332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9346826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
9356826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
9361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
937d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
9386826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, Owner);
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
940dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
941dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
942dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
943dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
9446826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
9456826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
946dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
9472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
94865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
94965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
9509f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
95165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
95265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
9534ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
9544ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
9554ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
9566826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
957a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
958dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9602dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
9612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
9622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
963615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
964dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
965615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
966615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
96703b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
96817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
96903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
97003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
971bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
97265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
973bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
974bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
9756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
97658e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
977a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
97858e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
97958e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
98058e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
98158e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
98258e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
98358e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
98458e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
98558e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
98658e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
98758e4677a948e80c92deeebbcd3bdd9266adda798John McCall
98858e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
9892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
9902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
99158e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
9922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
99458e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
99558e4677a948e80c92deeebbcd3bdd9266adda798John McCall    = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
99658e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
9972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9989351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
999f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
1000f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
10010ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
10020ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
10030ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
10042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
10052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
10062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
100748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
10082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
10092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
10102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1011e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1012e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1013e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1014e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1015e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1016e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
10171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1018e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1019e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1020550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1021e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1022e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1023e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1024e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
10250f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
10260f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1027e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1028550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1029550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1030550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1031550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1032e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1033e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1034e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
103533642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
103633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
103733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
103833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1039a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
104033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
104133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
104233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
104333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
104433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
104533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
104633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
104733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
104833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
104933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
105033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
105133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
105233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
105333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
105433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
105533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
105633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
105733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
105833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
105933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
106033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
106133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
106233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
106333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
106433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
106533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
106633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
106733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1068550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1069550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1070550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1071550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
107233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
107333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
107433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
10750dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
10769106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
10779106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
10789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
10799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
10809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
10819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
10829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
10839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
10849106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
10859106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
10869106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
10879106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
10889106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
10899106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
10909106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
10919106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
10929106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
10939106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
10949106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
10959106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
10969106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
10979106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
10989106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
10999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
11009106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11019106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
11029106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
11039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
110448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
110548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
110648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
110748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
110848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
110948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
111048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
111148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
111248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
111348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
111448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
111548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
111648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
111748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1118ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1119ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1120ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1121ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11229f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
11239f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
11249f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
11259f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
11269f54ad4381370c6b771424b53d219e661d6d6706John McCall
11279f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
11289f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
11299f54ad4381370c6b771424b53d219e661d6d6706John McCall
1130ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1131ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1132ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1133ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1134ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1135ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1136ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1137ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1138ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1139ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1140ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
11419f54ad4381370c6b771424b53d219e661d6d6706John McCall
11429f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
11439f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
11449f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
11459f54ad4381370c6b771424b53d219e661d6d6706John McCall
11469f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
11479f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
11489f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
11499f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
11509f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
11519f54ad4381370c6b771424b53d219e661d6d6706John McCall
11529f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
11539f54ad4381370c6b771424b53d219e661d6d6706John McCall
11549f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
11559f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1156ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1157ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
11589f54ad4381370c6b771424b53d219e661d6d6706John McCall
1159ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1160ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1161ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1162ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11639f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
11649f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
11659f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
11669f54ad4381370c6b771424b53d219e661d6d6706John McCall
1167323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1168323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
11699f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
11709f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
11719f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
11729f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
11739f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
11749f54ad4381370c6b771424b53d219e661d6d6706John McCall      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
11759f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
11769f54ad4381370c6b771424b53d219e661d6d6706John McCall
11779f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
11789f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
11799f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
11809f54ad4381370c6b771424b53d219e661d6d6706John McCall
11819f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
11829f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
11839f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1184323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1185323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1186323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
11879f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1188ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1189ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1190ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1191ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1192ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
11939f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
11949f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1195ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1196ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
11987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
11997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
12007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
12027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
12037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
12047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
12057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
12077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
12087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
12097ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12107ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
12117ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12127ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
12167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1217ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1218ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
12207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
12217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
12237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
12270dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
12280dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
12290dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12310dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
12320dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
12330dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
12369488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
12410d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1242ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1243ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12440d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
12450dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
12460dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1247ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1248d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
12497e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
12508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
12518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
12528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1253e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1254e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1255e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1256e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1257e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1258e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1259e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1260ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1261e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1262e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1263e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1264e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1265bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1266e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1267e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1268e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1269e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1270bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1271e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
12729148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1273e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1274e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1275e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1276e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1277e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1278e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1279e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1280e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1281e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1282e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1283e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1284e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1285e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1286e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1287e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1288e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1290e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1291ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1292ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1293ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1294ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1295ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1296ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1297ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1298ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1299ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1300ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1301ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1302ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1303ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1304ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1305550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1306550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1307550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1308550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1309550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1310ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1311ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1312ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1315ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1316ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1317ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1318ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1319833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1320833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1321833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1322833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1323d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1324833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1325d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1326d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1327ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1328d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1329ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1330ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1331ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1332ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1333ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1334ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1335ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1336ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1337ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1338d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1339ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1340ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1341ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1342ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1343ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1344ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1345ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1346ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1347ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1348ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1349ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1350ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1351ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1357ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1358ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1360ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1361ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1363ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1364ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1365ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1366ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
1369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType WrittenTy
1370ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1371d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1372ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1373ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1374ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1376ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1377ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1378ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1379ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1380ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1381ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1382ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1383ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1384ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1386ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1387ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1391ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1392ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1393ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1394ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1395ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1396ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1397ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1398ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1399ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1401ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1405d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
1406ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1407ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1408ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1409ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1412ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1414ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1415ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1416ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1417ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
1418ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
14195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
1420ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
14215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
14225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
14235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1424ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
14255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1427ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
14285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
14295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
14305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1431ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
14327e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
14330ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
14355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
14365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
14376477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
14385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
14395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
14405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
1442ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
1443ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
1444ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
14455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
14465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
14485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
14495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
14515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
14535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
14545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
14555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
14575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
14585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
14595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1460183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
14615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
1463ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1464ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
14655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
14665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
14675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1468beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
14695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
14705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
14715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
14725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1474e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1475e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1476e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1479e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1480e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1481e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1483cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1484cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1485cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1488cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1489cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1490cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1491cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1492cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1493cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1494cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1496cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1498cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1499bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1500cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1501cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1502f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1503cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1504cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
15051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15060ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
15070ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
15080ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15090ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
15100ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
15110ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
15120ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
15130ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
15140ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
15150ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
15160ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
15170ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
15180ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
15190ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
15200ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
15210ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
15220ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15230ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
15240ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
15250ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15260ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
15270ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15280ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
15290ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
15300ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
15310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
15320ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
15330ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
15340ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
15350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
15360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
15370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
15380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
15390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
15400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->getNoReturnAttr()));
15410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
15420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1543e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1544e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1545e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
15465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
15475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
15485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
15495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
15505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
15535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1554e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1555e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
15585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1559e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1560e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
15615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
15635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
15645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
15655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1566a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1567a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1568a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1569a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1570b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1571b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1572b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1573b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1574a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1575b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1576b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1577b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1578b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1579b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1580e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1581e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1582e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1583e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1584f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1585b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1586e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1587e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
158854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
158954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
159054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
15916fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
15921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1593251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1594251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1595251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1596251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
15971eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
15983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
15991eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
16001eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
16016fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
16021eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1603e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1604e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1605e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1606e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1607e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1608e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1609e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1610e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1611e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1612e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1613e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1614e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1615e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1616e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1617e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1618e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
16191eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1620e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
16211eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1622d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1623d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1625d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1627d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
16287ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1629d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1631f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1632f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1633f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1634b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1635b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1636b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1637b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1638b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1639b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1640b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1642e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1643e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
164454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
164554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
164654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
164954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
165054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
165154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
165254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
165354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1654b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1655b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1656b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1657b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1658b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1660090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1661090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1662090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1664090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1665090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1666090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1670090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1671e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
167252604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
167352604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
167452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
16751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1676e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1677b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1678b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1679aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1680aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1681aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
16821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1683b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1684b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1686b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
16871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1688b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1689b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1690b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1691a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1692a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1693a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1694a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1695a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
16967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
16977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
16987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
16997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
17017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
17027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
17047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1705e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1706e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1707e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1708e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
17097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
17107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
17117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1712e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1713e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
17147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
17157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
17187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
17197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
17200d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
17210d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
17247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
17257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
17261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
17271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
1728e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
17290d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
1730e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
1731e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
1732e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
1733e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1734e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1735e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
17367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
17387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1739251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
17401028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1741251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1742251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1743251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
1744251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1745251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1746251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
17471028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
1748251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1749251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
17501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
17527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
17537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
17567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
17577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
17587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
17597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
17607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
17611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
17637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
17647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
17657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17671028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
1768ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
17697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
17707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
17717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
17727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
17731028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    Var->setPreviousDeclaration(OldVar);
1774583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1775583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
1776583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1777583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
17787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
17797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
17807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
17811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
17837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
17857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
17861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
17887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
17891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1790a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1791815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1792090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1793090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1794090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1795090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1797090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1798090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1799090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1800090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
180172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
180272f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1803090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1804090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1805090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
18061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1807090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1808090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1809090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1810090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1811090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1812090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1813090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1814090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1815090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1816090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1817090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1818090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1819090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1820090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1821a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
1822802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
1823802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
1824802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
1825a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
1826802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
1827802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
1828802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
1829802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
1830a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1832090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1833802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
1834090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1835090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1836090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
18379988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18399988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
18409988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1841e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
18429988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
1843e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1844e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
18451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1847090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1848090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1849802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
1850090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1851090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1852090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1853090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1854090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1855090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1856090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1857090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
18581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1859090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1860090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1861090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
18621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1863090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
18641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1865090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1866090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
18671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       NewInits.data(), NewInits.size());
1868090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1869090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
187052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
187152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
187252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
187352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
187452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
187552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
187652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
187752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
187852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
187952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
188052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
188152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
188252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
188352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
188452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
18850d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
18860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
18870d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
18880d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
18890d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
18900d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
18910d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
18920d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
18930d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
18940d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
18950d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
18960d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
18970d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1898ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
1899ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1900ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
1901ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
1902ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
1904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1905ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
1906ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
1907ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1908ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
1909ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
1910ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1911ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1912ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1913ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
191452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
191552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
191652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
191752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
191852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
191952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
192052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
192152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
192252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
192352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
192452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
192552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
192652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
192752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
192852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
192952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
193052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
193152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
193252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
193352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
193452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
193552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
193652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
193752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
193852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
193952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
194052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
194152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
194252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
194352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
194452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
194552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
194652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
194752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
194852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
194952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
195052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
195152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
195252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1953ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
1954ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
1955ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
1956ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
1957ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1958ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1959ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
1960ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
1961ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
1962ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
1963ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1964ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
19667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
19677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
1968ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
19697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
19707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
19717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
19720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
19730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
1974ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
19750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
19760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
197752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
197852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
197952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
198052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
198152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
198252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
198352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
198452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
198552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
198652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
198752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
198852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
198952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
199052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
199152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1992ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
1993ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
1994815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
19950d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
19967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
19977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
19987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
19997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
20007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
20017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
20027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
20037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
20047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
20050d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
20070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
20080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2009815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
20100d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
20110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
20121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
201452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
20151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
201752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2018815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
201952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
202052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2021815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
20227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
202352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
202452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
202552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
202652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
202752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2028a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
20290d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
20300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
20310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2032ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2033ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2034ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2035ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2037d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2038d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2039d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
20401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2041d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2042d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2043d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
20441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2045ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2046ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2047ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2048ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2049ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2050ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2051815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2052815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2053815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2054815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2055815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2057815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2058815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2059815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2060815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2061815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2062815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2063815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2064815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2065815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2066815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
206702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
206802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
206902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
207002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
2071e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2072e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
207302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
2074e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
207502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
207602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
207702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
207802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2079ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2080ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2081815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2082815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2083815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2084815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2085815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2087815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2088815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2089815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2090815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2091815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2092815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2093815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2094815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2095815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2096815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2097815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2098815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2099815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2100815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2101815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2102815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2103ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2104ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
2105e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2106e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2107815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2108550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
2109550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2110550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
21112bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
21122bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
21132bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
21142bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2115815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2116e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2117e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2118e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2119e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2120e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // If the RecordDecl is actually the injected-class-name or a "templated"
2121e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // declaration for a class template or class template partial
2122e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // specialization, substitute into the injected-class-name of the
2123e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // class template or partial specialization to find the new DeclContext.
2124e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2125e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2126e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2127e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
2128e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
2129e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2130e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2131e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = Context.getTypeDeclType(Record);
2132e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
2133e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    }
2134e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2135e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
2136e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // Substitute into the injected-class-name to get the type corresponding
2137e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // to the instantiation we want. This substitution should never fail,
2138e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // since we know we can instantiate the injected-class-name or we wouldn't
2139e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // have gotten to the injected-class-name!
2140e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
2141e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // instantiation in the common case?
2142e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2143e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2144e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2145e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2146e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2147e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2148e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2149e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2150e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // We are performing "partial" template instantiation to create the
2151e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // member declarations for the members of a class template
2152e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // specialization. Therefore, D is actually referring to something in
2153e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the current instantiation. Look through the current context,
2154e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // which contains actual instantiations, to find the instantiation of
2155e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the "current instantiation" that D refers to.
21561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
215752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
215952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2160e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2161e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
216252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
216352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
216452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(false &&
216652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
2167e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return Record;
216852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
2169e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2170e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2171e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2172e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
217352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2174e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2175e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2176e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2177e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
21781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
217944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
21801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2181815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2182815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2183815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2184815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
2185815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2186815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
218717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2188815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2189815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2190815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2191815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2192815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2193815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2194815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2195815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2196815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2197815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
219917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
220017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2201815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
22021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22039f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
22049f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert((Result || isa<UsingShadowDecl>(D))
22059f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
22069f54ad4381370c6b771424b53d219e661d6d6706John McCall
2207815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2208815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2209815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2210815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2211815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2212d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2214d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
2215d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
2216d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
2217d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
2218b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
22217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2223c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2224c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2225c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22276fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2228b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
22297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
22307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
22311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
22337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
22347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2235c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
22361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2237c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2238c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2239c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2240c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2243d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2244d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
2245