SemaTemplateInstantiateDecl.cpp revision 3dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512
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);
463dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
483d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
526477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
54a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
55a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
56d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
57d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
58d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
59615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
61bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
626477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
63e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
657974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
66d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
67e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
699106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
71ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
72ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
7848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
87fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
88fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
89fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
90fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
92ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
94e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
96e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
97e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
98ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
99ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
106d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
107d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlssonvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
108d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
109d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
110d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
111d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
112d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
113d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
114d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
115d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
116d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
117d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1184f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1194f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1204f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1214f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1224f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1234f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1244f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1254f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1264f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1274f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1284f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1294f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1303dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1313dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1323dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1333dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1343dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1353dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1363dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1373dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1383dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1393dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1403dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1413dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1423dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1433dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1443dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
147a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
148ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
149ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
150ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
151ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
153a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
160ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1645126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1655126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs);
1665126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1675126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1685126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
16946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
17017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
1766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
1776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
1786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
1796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
1806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
1816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
1826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
1836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
1856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
1866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
1876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
1886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1896eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
1906eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
1916eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
1926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1936eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
1946eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
1956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
1976eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
1986eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
1996eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2006eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2016eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2026eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2036eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2043d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
205ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
206a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2070a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2080a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2090a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2100a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2113d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2123d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
213b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2143d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
2153d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2160a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
217a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
2183d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
22946460a68f6508775e98c19b4bb8454bb471aac24John McCall
230390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
231390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
2323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
2336826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
2346826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
2356826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName);
23660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
23760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
2386826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
2427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
2437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
2447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
2457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
2461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
247251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
248251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
249251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
250251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
251cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
252251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
25360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
25460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
25560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
2561f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
2571f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
2581f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
2591f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
2601f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
2616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // Extract the initializer, skipping through any temporary-binding
2626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // expressions and look at the subexpression as it was written.
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *DInit = D->getInit();
264a89ebea6bd68d8333f2d020106a3640831aa6507Douglas Gregor    if (CXXExprWithTemporaries *ExprTemp
265a89ebea6bd68d8333f2d020106a3640831aa6507Douglas Gregor          = dyn_cast<CXXExprWithTemporaries>(DInit))
266a89ebea6bd68d8333f2d020106a3640831aa6507Douglas Gregor      DInit = ExprTemp->getSubExpr();
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = Binder->getSubExpr();
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      DInit = ICE->getSubExprAsWritten();
2716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
2736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer is a parenthesized list of expressions that is
2746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // type-dependent. Instantiate each of the expressions; we'll be
2756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // performing direct initialization with them.
2766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getExprs(),
2806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getNumExprs(),
2816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
2846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getLParenLoc(),
2866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
2876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
2886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getRParenLoc());
28983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
2906eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
2916eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer resolved to a constructor. Instantiate the constructor
2926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // arguments.
2936eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2946eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2976eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getArgs(),
2986eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getNumArgs(),
2996eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
3006eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
3016eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        if (D->hasCXXDirectInitializer()) {
3026eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeLParenLoc =
3036eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor            SemaRef.PP.getLocForEndOfToken(D->getLocation());
3046eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
3056eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                          : CommaLocs.back();
3066eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
3076eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeLParenLoc,
3086eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                move_arg(InitArgs),
3096eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                CommaLocs.data(),
3106eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                                FakeRParenLoc);
3116eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else if (InitArgs.size() == 1) {
3126eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          Expr *Init = (Expr*)(InitArgs.take()[0]);
3136eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
3146eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       SemaRef.Owned(Init),
3156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                       false);
3166eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        } else {
3176eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          assert(InitArgs.size() == 0);
3186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
319a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor        }
3206eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      }
3216eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      OwningExprResult Init
3236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
324a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor
3256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // FIXME: Not happy about invalidating decls just because of a bad
3266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // initializer, unless it affects the type.
3276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (Init.isInvalid())
3286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        Var->setInvalidDecl();
3296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      else
3306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
3316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                     D->hasCXXDirectInitializer());
3326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3341f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
33565b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
33665b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
3373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3393d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3403d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
343a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
34407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
34507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
34607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
34707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
348a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
34907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
35007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
35807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
3648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
3658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
3668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
367ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
368ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
371ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
3758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
376e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
37907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
38007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
3811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
3848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
385ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
3868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
3878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
388663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
389663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
390f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
391663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
393d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
394d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
395f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
396f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
398f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
399f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
400f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
403f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
40446460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
405f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
41002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
41102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
41202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
41302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
41402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
41502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
41602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
41702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
41802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
419fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
42002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
42102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
422fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
42302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
42402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
42502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
42602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
42702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
428a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
429a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
430a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
431e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    Decl *NewND;
432e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall
433e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    // Hack to make this work almost well pending a rewrite.
43463644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor    if (ND->getDeclContext()->isRecord()) {
43563644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor      if (!ND->getDeclContext()->isDependentContext()) {
43663644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
43763644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor      } else {
43863644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
43963644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        // templated friend declarations. This doesn't produce a correct AST;
44063644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        // however this is sufficient for some AST analysis. The real solution
44163644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        // must be put in place during the pending rewrite. See PR5848.
44263644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor        return 0;
44363644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor      }
44463644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor    } else if (D->wasSpecialization()) {
4457557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      // Totally egregious hack to work around PR5866
4467557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      return 0;
4477557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor    } else
448e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = Visit(ND);
44902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
450c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
45102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
45202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
45502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
45602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
4575fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
45802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
45902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
460fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
461fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
466ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
469ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
47343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
47443d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
477b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
478b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
485741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
4878dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
48806c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
48917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4920ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
49517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
49617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
4988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
500ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
501ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
503ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
505ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
506ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
5198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
5273b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
52817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
529b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
5308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
5318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
535fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
536c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
537c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
538fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
539fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
5408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
5428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5446477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
5456477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
5466477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
5476477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
5486477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
550ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
551ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
552ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
553ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
554ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
555ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
557ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
558ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
559ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
560ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
561ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
562ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
563ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
564e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
565550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
566550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
567550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
568e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
569ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
571d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
572e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
573e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
574e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
575e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
576e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
577f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
578f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
579e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
580e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
581e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
582e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
583e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
584e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  if (D->getFriendObjectKind())
585e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setObjectOfFriendDecl(true);
586e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  else
587e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
588e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
589f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
590f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
591f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
592f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
593259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
594259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  if (Inst->getFriendObjectKind()) {
595e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
596259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
597e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
59846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Inst->setAccess(D->getAccess());
599e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
600ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
601ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
602ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
603ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
604ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
605ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
606ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
607ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
609ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
610ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
611ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
612ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
613ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
614ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
615ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
616ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
618e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
619e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
620d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
6217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
6227974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
623ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
624ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
625ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
626ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
627ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
628ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
631ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
636ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
637ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
638ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
643ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
644ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
645ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
6467974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
6477974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
6487974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
6497974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
650d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
651550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
652550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
653550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
654550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
655550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
656550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
657d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
658d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
660d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
661ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
662a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
663a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
664a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
665a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
666a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
667a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
668a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
669a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
670a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
671a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
672d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
673d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
67446460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
67546460a68f6508775e98c19b4bb8454bb471aac24John McCall
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
677d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
67837d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
679a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
68037d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
681a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
682a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
683e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
684e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
685e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
686e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
687e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall      !(InstTemplate->getFriendObjectKind() &&
688e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall        !D->getTemplatedDecl()->isThisDeclarationADefinition()))
689a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
690a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
691a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
692a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
693a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
694d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
695d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
696d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
697d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
698d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
699d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
700d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
7016c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
7026c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
7036c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
7046c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
7056c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
7066c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
707d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
708d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
710741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
711741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
712d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
713eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
714eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
715eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
716eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
717eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
718d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
719f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
720d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
72102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
72202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
72302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
72402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
72502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
726d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
727d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
72817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
729d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
730d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
731d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
73202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
73302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
73402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
73502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
73602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
7377557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
738a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
739127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
740127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
741127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
742127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
743a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
744127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
746d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
747d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
748828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
752127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
7531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
755127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
756127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
757127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
76079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
76179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
76279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
764e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
765ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
766e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
7672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
768fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
76968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
77068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
77168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
77268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
77368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
77468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  else
77568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), TemplateArgs);
77668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
77702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
7781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
779a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                           D->getDeclName(), T, D->getTypeSourceInfo(),
780a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
7810130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
78202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
784e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
785e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
786e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
787838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
78802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
789a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
790a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
791a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
792a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
793a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
794a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
795a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
796a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
797a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
798a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
799a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
800a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
801a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
802a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
803a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
804a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
805a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
806a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
807a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
808a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
809a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
81066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
81166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
812838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
81366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
81466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
81502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
816a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
817e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
818e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
820e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
821e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
822a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
8236826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
8246826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
8256826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
826a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
827a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
828a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
829a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
8306826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
831a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
832a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
833a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
834a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
835a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
8366826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
8376826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
839a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
8409f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
8419f54ad4381370c6b771424b53d219e661d6d6706John McCall                                   false, Redeclaration,
842e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
843e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
844a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
845a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
846a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  NamedDecl *FromFriendD
847a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
848a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FromFriendD->getFriendObjectKind()) {
849a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
8506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
851a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
852a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
853a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
854a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
855a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
856a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
857a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
858a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
859a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!Owner->isDependentContext() && !PrevDecl)
860a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
861a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
862a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!TemplateParams)
863a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
864a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
865a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
866e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
867e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
8682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
869d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
871d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
8726b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
8736b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
874d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
877d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
8786b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
880d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
881d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
8826b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
8866b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8886b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
8896b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
8906b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
8916b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
8926b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
89379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
89479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
89579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
89679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
89748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
8980ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
899ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
9002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
9012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
9022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
9042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
905dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
907dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
90817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
909dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
910dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
911dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
915a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                        Constructor->getTypeSourceInfo(),
9161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
9170130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
91817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
91917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
92017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
92117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
92217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
92317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
9240130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
92565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
9261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
92765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
928183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
92965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
93065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
93165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
93265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
933a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                       T, Conversion->getTypeSourceInfo(),
9340130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
93565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
936dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
938a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                   D->getDeclName(), T, D->getTypeSourceInfo(),
9390130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
940dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
9416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
942d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
943d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
944d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
946d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
947d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
948d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
949d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
950d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
951d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
952d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
953d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
954d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
955d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
956d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
957d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
959d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
960d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
962d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
96366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
96466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
965838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
96666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
96766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
96866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else {
96966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
9702db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
97166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
97266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
9747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
9757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
9767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
9777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
9805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
9815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
982838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
9835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
9845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
9855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
9862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9876826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
9886826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
990d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
9916826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, Owner);
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
993dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
994dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
995dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
996dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
9976826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
9986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
999dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
10002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
100165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
100265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
10039f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
100465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
100565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
10064ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
10074ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
10084ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
100946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
101046460a68f6508775e98c19b4bb8454bb471aac24John McCall
10116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
1012a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
1013dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
10162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
10172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1018615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1019dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1020615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1021615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
102203b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
102317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
102403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
102503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1026bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
102765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1028bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1029bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
10306477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
103158e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
1032a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
103358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
103458e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
103558e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
103658e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
103758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
103858e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
103958e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
104058e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
104158e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
104258e4677a948e80c92deeebbcd3bdd9266adda798John McCall
104358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
10442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
10452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
104658e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
10472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
104958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
10507a9813ced7455b8a33a807489ca77a4f809c8a73John McCall    = ParmVarDecl::Create(SemaRef.Context,
10517a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          SemaRef.Context.getTranslationUnitDecl(),
10527a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          D->getLocation(),
105358e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
10542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
10559351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
1056f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
1057f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
10580ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
10590ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
10600ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
10612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
10622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
10632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
106448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
10652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
10662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
10672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1068e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1069e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1070e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1071e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1072e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1073e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1076e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1077550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1078e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1079e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1080e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1081e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
10820f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
10830f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1084e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1085550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1086550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1087550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1088550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1089e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1090e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1091e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
109233642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
109333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
109433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
109533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1096a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
109733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
109833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
109933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
110033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
110133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
110233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
110333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
110433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
110533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
110633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
110733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
110833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
110933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
111033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
111133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
111233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
111333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
111433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
111533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
111633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
111733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
111833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
111933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
112033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
112133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
112233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
112333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
112433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1125550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1126550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1127550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1128550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
112933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
113033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
113133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
11320dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
11339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
11349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
11359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
11369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
11379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
11389106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
11399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
11409106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
11419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
11429106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
11439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
11449106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
11459106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
11469106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11479106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
11489106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
11499106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
11509106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
11519106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
11529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
11539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
11559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
11569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
11579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
11589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
11599106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
11609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
116148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
116248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
116348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
116448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
116548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
116648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
116748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
116848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
116948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
117048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
117148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
117248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
117348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
117448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1175ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1176ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1177ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1178ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
11799f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
11809f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
11819f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
11829f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
11839f54ad4381370c6b771424b53d219e661d6d6706John McCall
11849f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
11859f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
11869f54ad4381370c6b771424b53d219e661d6d6706John McCall
1187ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1188ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1189ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1190ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1191ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1192ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1193ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1194ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1195ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1196ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1197ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
11989f54ad4381370c6b771424b53d219e661d6d6706John McCall
11999f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
12009f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
12019f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
12029f54ad4381370c6b771424b53d219e661d6d6706John McCall
12039f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
12049f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
12059f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
12069f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
12079f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
12089f54ad4381370c6b771424b53d219e661d6d6706John McCall
12099f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
12109f54ad4381370c6b771424b53d219e661d6d6706John McCall
12119f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
12129f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1213ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1214ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
12159f54ad4381370c6b771424b53d219e661d6d6706John McCall
1216ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1217ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1218ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1219ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12209f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
12219f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
12229f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
12239f54ad4381370c6b771424b53d219e661d6d6706John McCall
1224323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1225323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
12269f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
12279f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
12289f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
12299f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
12309f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
12319f54ad4381370c6b771424b53d219e661d6d6706John McCall      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
12329f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
12339f54ad4381370c6b771424b53d219e661d6d6706John McCall
12349f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
12359f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
12369f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
12379f54ad4381370c6b771424b53d219e661d6d6706John McCall
12389f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
12399f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
12409f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1241323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1242323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1243323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
12449f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1245ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1246ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1247ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1248ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1249ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
12509f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
12519f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1252ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1253ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
12557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
12567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
12577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
12597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
12607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
12617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
12627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
12647ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
12657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
12667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
12687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
12737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1274ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1275ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
12767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
12777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
12787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
12797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
12807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
12811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
12821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
12840dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
12850dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
12860dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12880dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
12890dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
12900dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
12939488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12947ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
12957ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
12967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
12977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
12980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1299ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1300ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
13010d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
13020dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
13030dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1304ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1305d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
13067e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
13078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
13088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
13098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1310e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1311e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1312e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1313e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1314e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1315e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1316e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1317ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1318e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1319e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1320e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1321e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1322bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1323e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1324e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1325e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1326e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1327bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1328e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
13299148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1330e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1331e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1332e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1333e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1334e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1335e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1336e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1337e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1338e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1339e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1340e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1341e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1342e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1343e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1344e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1345e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1347e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1348ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1349ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1350ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1351ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1357ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1358ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1360ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1361ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1362550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1363550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1364550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1365550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1366550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1370ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1371ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1372ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1373ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1374ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1376833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1377833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1378833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1379833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1380d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1381833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1382d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1383d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1384ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1385d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1386ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1387ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1391ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1392ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1393ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1394ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1395d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1396ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1397ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1398ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1399ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1401ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1405ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1406ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1407ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1408ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1409ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1412ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1414ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1415ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1416ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1417ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1418ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1419ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1420ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1422ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1423ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1424ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1425ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
1426ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType WrittenTy
1427ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1428d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1429ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1430ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1431ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1432ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1433ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1437ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1438ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1439ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1440ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1441ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1442ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1443ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1444ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1445ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1446ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1447ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1448ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1449ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1450ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1451ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1452ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1453ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1454ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1455ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1456ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1457ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1458ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1459ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1460ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1461ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1462d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
1463ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1464ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1465ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1466ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1467ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1468ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1474ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
1475ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
14765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
1477ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
14785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
14795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
14805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1481ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
14825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
14831eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1484ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
14855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
14865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
14875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1488ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
14897e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
14900ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
14925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
14935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
14946477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
14955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
14965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
14975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
1499ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
1500ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
1501ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
15025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
15035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
15055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
15065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
15085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
15105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
15115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
15125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
15145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
15155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
15165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1517183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
15185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
1520ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1521ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
15225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
15235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
15245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1525beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
15265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
15275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
15285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
15295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1531e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1532e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1533e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1536e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1537e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1538e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1540cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1541cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1542cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1545cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1546cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1547cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1548cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1549cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1550cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1551cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1553cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1555cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1556bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1557cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1558cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1559f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1560cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1561cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
15640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
15650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
15670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
15680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
15690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
15700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
15710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
15720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
15730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
15740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
15750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
15760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
15770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
15780ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
15790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
15810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
15820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
15840ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
15850ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
15860ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
15870ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
15880ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
15890ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
15900ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
15910ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
15920ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
15930ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
15940ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
15950ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
15960ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1597ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getNoReturnAttr(),
1598ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getCallConv()));
15990ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
16000ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1601e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1602e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1603e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
16045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
16055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
16065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
16075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
16085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
16101eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
16115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1612e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1613e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
16141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
16165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1617e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1618e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
16195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
16215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
16225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
16235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1624a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1625a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1626a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1627a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1628b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1629b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1630b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1631b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1632a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1633b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1634b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1635b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1636b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1637b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1638e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1639e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1640e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1641e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1642f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1643b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1644e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1645e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
164654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
164754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
164854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
16496fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1651251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1652251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1653251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1654251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
16551eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
16563b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
16571eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
16581eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
16596fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
16601eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1661e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1662e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1663e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1664e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1665e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1666e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1667e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1668e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1669e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1670e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1671e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1672e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1673e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1674e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1675e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1676e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
16771eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1678e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
16791eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1680d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1681d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
16821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1683d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1685d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
16867ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1687d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
16881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1689f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1690f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1691f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1692b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1693b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1694b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1695b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1696b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1697b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1698b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
16991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1700e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1701e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
170254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
170360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
170460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
170560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
170660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
170760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
170860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
170960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
171060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
171254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
171354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
171454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
171554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
171654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
171754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1718b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1719b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1720b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1721b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1722b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1724090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1725090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1726090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
17271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1728090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1729090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1730090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
17311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
17321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1734090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1735e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
173652604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
173752604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
173852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
17391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1740e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1741b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1742b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1743aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1744aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1745aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
17461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
174760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
174860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
174960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
175060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
175160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
1752b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1753b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1755b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1757b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1758b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1759b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1760a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1761a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1762a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1763a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1764a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
17657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
17667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
17677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
17687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
17707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
17717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
17727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
17737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1774e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1775e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1776e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1777e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
17787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
17797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
17807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1781e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1782e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
17837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
17847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
17851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
17877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
17887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
17890d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
17900d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
17911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17920d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
17937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
17947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
17951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
1797e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
17980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
1799e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
1800e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
1801e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
1802e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1803e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1804e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
18057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
18067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
18077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1808251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
18091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1810251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1811251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1812251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
1813251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1814251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1815251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
18161028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
1817251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1818251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
18217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
18227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
18257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
18267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
18277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
18287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
18297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
18301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
18327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
18337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
18347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18361028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
1837ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
18387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
18397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
18407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
18417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
1842583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1843583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
1844583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1845583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
18467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
18477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
18487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
18491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
18517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
18521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
18537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
18567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
18571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1858a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1859815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1860090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1861090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1862090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1863090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
18641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1865090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
18669db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
18679db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
1868090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1869090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
187072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
187172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1872090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1873090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1874090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
18759db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
18761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1877090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
18789db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    Expr *InitE = Init->getInit();
18799db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (!InitE) {
18809db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      // Nothing to instantiate;
18819db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(InitE)) {
18829db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      if (InstantiateInitializationArguments(*this, ParenList->getExprs(),
18839db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                             ParenList->getNumExprs(),
18849db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                             TemplateArgs, CommaLocs,
18859db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                             NewArgs)) {
18869db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
18879db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        continue;
18889db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      }
18899db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
18909db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      OwningExprResult InitArg = SubstExpr(InitE, TemplateArgs);
18919db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      if (InitArg.isInvalid()) {
18929db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
18939db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        continue;
18949db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      }
18959db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
18969db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      NewArgs.push_back(InitArg.release());
1897090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
18989db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
1899090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1900090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1901a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
1902802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
1903802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
1904802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
1905a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
19069db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
1907802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
1908802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
1909802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
1910802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
1911a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1913090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1914802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
1915090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1916090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1917090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
19189988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
19191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19209988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
19219988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1922e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
19239988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
1924e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1925e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
19261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1928090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1929090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1930802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
1931090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1932090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1933090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
19349db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
19359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
1936090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
19379db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
1938090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1939090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
19401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1941090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1942090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1943090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
19441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1945090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1947090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1948090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
19499db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
19509db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
1951090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1952090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
195352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
195452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
195552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
195652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
195752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
195852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
195952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
196052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
196152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
196252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
196352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
196452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
196552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
196652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
196752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
19680d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
19690d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
19700d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
19710d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
19720d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
19730d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
19740d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
19750d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
19760d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
19770d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
19780d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
19790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
19800d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1981ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
1982ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1983ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
1984ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
1985ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1986ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
1987ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1988ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
1989ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
1990ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1991ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
1992ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
1993ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1994ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1995ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1996ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
199752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
199852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
199952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
200052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
200152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
200252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
200352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
200452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
200552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
200652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
200752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
200852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
200952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
201052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
201152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
201252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
201352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
201452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
201552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
201652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
201752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
201852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
201952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
202052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
202152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
202252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
202352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
202452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
202552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
202652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
202752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
202852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
202952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
203052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
203152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
203252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
203352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
203452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
203552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2036ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2037ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2038ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2039ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2040ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2041ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2042ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2043ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2044ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2045ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2046ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2047ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
20497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
20507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2051ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
20527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
20537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
20547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
20550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
20560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2057ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
20580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
20590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
206052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
206152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
206252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
206352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
206452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
206552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
206652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
206752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
206852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
206952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
207052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
207152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
207252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
207352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
207452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2075ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2076ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2077815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
20780d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
20797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
20807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
20817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
20837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
20847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
20857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
20867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
20877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
20880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
20900d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
20910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2092815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
20930d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
20940d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
20951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
209752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
20981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
210052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2101815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
210252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
210352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2104815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
21057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
210652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
210752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
210852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
210952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
211052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2111a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
21120d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
21130d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
21140d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2115ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2117ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2119ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2120d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2121d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2122d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
21231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2124d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2125d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2126d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
21271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2128ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2129ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2130ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2131ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2132ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2133ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2134815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2135815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2136815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2137815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2138815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
21391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2140815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2141815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2143815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2144815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2145815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2146815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2147815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2148815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2149815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
215002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
215102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
215202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
215302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
2154e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2155e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
215602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
2157e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
215802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
215902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
216002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
216102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2162ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2163ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2164815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2165815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2166815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2167815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2168815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2169815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2170815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2171815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2172815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2173815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2174815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2175815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2176815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2177815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2178815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2179815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2180815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2181815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2182815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2183815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2184815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2185815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2186ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2187ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
2188e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2189e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2190815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2191550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
21926d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2193550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
21942bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
21952bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
21962bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
21972bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2198815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2199e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2200e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2201e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2202e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
22038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
22048b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
22058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
22068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
22078b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2208e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2209e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2210e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2211e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
2212e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
2213e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2214e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2215e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = Context.getTypeDeclType(Record);
2216e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
22178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    }
2218e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2219e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
22208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
22218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
22228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
22238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
22248b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
22258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
22268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
22278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
22288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2229e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2230e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2231e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2232e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2233e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2234e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2235e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2236e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
22378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
22388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
22398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
22408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
22418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
22428b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
22438b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
22448b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
22451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
224652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
22488b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2249e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2250e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
225152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
22528b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
22538b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
22548b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
225552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
225652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
22578b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
22588b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
22598b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
22608b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
22618b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
22628b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
22638b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
226452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
22658b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
226652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
22678b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2268e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2269e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2270e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
227152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2272e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2273e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2274e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2275e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
227744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
22781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2279815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2280815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2281815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2282815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
2283815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2284815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
228517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2286815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2287815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2288815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2289815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2290815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2291815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2292815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2293815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2294815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2295815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
22961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
229717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
229817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2299815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
23001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23019f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
23029f54ad4381370c6b771424b53d219e661d6d6706John McCall    assert((Result || isa<UsingShadowDecl>(D))
23039f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
23049f54ad4381370c6b771424b53d219e661d6d6706John McCall
2305815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2306815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2307815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2308815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2309815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2310d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2312d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
231360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
231460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
231560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
231660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
231760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
231860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
231960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
232060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
232160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
232260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
232360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
232460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
23251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
23277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
23281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2329c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2330c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2331c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
23321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23336fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2334b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
23357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
23367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
23371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
23397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
23407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2341c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2342291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2343291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2344291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2345291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2346291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2347291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2348291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2349291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2350291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2351291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2352291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2353291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2354291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2355291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2356291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2357291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2358291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2359291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
23601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2361c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2362c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2363c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2364c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
23651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2367d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2368d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
2369