SemaTemplateInstantiateDecl.cpp revision 9148c3f5829f4d031249faeb1043e7be914539e8
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"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
18c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
1983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class VISIBILITY_HIDDEN TemplateDeclInstantiator
26b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
29d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    void InstantiateAttrs(Decl *Tmpl, Decl *New);
32d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
37d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
387e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
41390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
42390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
434f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
444f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
506477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
52a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
53a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
54d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
55d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
56d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
57615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
59bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
606477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
61e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
627974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
637974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
64d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
65e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
679106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
680dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Decl *VisitDecl(Decl *) {
723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      assert(false && "Template instantiation of unknown declaration kind!");
738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
76fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
77fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
78fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
79fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
81ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
83e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
85e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
86e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
87ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
88ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
89ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
90ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
91ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
95d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
96d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlssonvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
97d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
98d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
99d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
100d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
101d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
102d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
103d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
104d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
105d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
106d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1074f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1084f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1094f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1104f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1114f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1124f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1134f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1144f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1154f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1164f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1174f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1184f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
121ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
122ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
123ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
124ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
125ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
127ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
1288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
134ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
13817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
144ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
1450a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
1460a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
1470a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
1480a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
1490a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
1503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
152b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1533d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1543d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
1550a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
156a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
1573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
1627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
1637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
1657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
167390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
168390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
1693d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
170eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
1737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
1747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
1757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
1767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
1777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
179251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
180251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
181251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
182251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
183cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
184251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1853d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    OwningExprResult Init
187ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
1883d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    if (Init.isInvalid())
1893d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      Var->setInvalidDecl();
19042dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl    else if (!D->getType()->isDependentType() &&
19142dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl             !D->getInit()->isTypeDependent() &&
192e48319a8a901bc915d48d02b99c62e5f2589dbd9Douglas Gregor             !D->getInit()->isValueDependent()) {
19342dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl      // If neither the declaration's type nor its initializer are dependent,
19442dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl      // we don't want to redo all the checking, especially since the
19542dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl      // initializer might have been wrapped by a CXXConstructExpr since we did
19642dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl      // it the first time.
19742dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl      Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
19842dddbeadb82a918d83c14bdcce47ba8c0ed6fbaSebastian Redl    }
19983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // FIXME: We're faking all of the comma locations, which is suboptimal.
20183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Do we even need these comma locations?
20283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
20383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      if (PLE->getNumExprs() > 0) {
20483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
20583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
20683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          Expr *E = PLE->getExpr(I)->Retain();
20783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          FakeCommaLocs.push_back(
20883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
20983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        }
210e9f8eb64ed2d41c23db4a9774768613d4e07a865Douglas Gregor        PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
21183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Add the direct initializer to the declaration.
21483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            PLE->getLParenLoc(),
21683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            Sema::MultiExprArg(SemaRef,
21783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                       (void**)PLE->getExprs(),
21883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                           PLE->getNumExprs()),
21983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            FakeCommaLocs.data(),
22083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getRParenLoc());
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // When Init is destroyed, it will destroy the instantiated ParenListExpr;
22383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // we've explicitly retained all of its subexpressions already.
22483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    } else
225b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
2263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                   D->hasCXXDirectInitializer());
22765b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
22865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
2293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
2303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
2313d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
2323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
23507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  DeclaratorInfo *DI = D->getDeclaratorInfo();
23607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
23707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
23807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
23907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
24007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      DI = D->getDeclaratorInfo();
24107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
24207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
2488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
2498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
25007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
2518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
2568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
259ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
260ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
263ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
2648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
2658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
2678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
268e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
2698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
27107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
27207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
2748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
2758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
2768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
277ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
2788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
2798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
280663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
281663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
282f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
283663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
286d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
287f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
288f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
2891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
290f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
291f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
292f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
2938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
295f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
296f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
2978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
30102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
30202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
30302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
30402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
30502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
30602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
30702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
30802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
30902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
310fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
31102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
31202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
313fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
31402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
31502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
31602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
31702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
31802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
319a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
320a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
321a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
32202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *NewND = Visit(ND);
32302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
324c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
32502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
32602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
32902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
33002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
3315fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
33202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
33302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
334fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
335fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
3368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
339ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
340ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
343ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
34743d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
34843d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
351b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
352b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
3588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
359741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
3618dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
36206c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
36317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
3648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
3658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3660ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
3678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
36917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
37017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
374ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
375ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
377ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
380ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
3818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
3848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
3858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
3868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
3878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
3908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
3918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
3968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
3978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
3988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
40117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
402b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
408fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
409c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
410c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
411fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
412fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
4138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
4158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4176477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
4186477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
4196477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
4206477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
4216477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
422ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
423ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
424ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
425ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
426ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
427ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
428ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
429ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
430ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
431ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
432ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
433ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
437e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
438550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
439550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
440550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
441e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
442ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
444d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
445e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
446e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
447e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
448e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
449e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
450f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
451f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
453e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
455e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
456e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
457e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  if (D->getFriendObjectKind())
458e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setObjectOfFriendDecl(true);
459e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor  else
460e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
461e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
462f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
463f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
464f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
465f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
466259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
467259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  if (Inst->getFriendObjectKind()) {
468e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
469259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
470e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
471e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
474ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
476ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
477ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
478ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
479ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
480ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
482ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
483ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
485ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
487ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
489e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
490e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
491e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
492d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
4937974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
4947974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
495ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
496ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
497ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
498ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
499ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
500ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
501ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
502ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
503ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
504ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
505ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
506ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
507ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
510ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
511ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
512ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
513ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
514ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
515ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
517ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
5187974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
5197974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
5207974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
5217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
522d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
523550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
524550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
525550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
526550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
527550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
528550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
529d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
530d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
532d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
533ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
534a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
535a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
536a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
537a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
538a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
539a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
540a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
541a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
542a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
543a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
544d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
545d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
547d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
54837d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
549a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
55037d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
551a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
552a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
553a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getInstantiatedFromMemberTemplate())
554a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
555a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
556a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
557a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
558a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
559d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
560d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
561d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
562d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
563d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
564d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
565d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
566d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
567d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
569741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
570741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
571d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
572eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
573eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
574eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
575eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
576eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
577d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
578f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
579d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
58002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
58102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
58202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
58302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
58402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
585d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
586d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
58717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
588d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
589d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
590d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
59102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
59202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
59302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
59402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
59502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
596a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
597a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
598127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
599127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
600127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
601127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
602a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
603127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
605d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
606d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
607828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
611127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
614127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
615127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
616127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
618550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
621ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
622e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
6232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
624fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
625e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
626e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
627e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                    TemplateArgs);
62802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
630a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getDeclName(), T, D->getDeclaratorInfo(),
631a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
6320130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
63302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
636e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
637e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
638e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
63902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
640a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
641a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
642a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
643a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
644a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
645a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
646a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
647a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
648a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
649a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
650a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
651a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
652a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
653a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
654a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
655a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
656a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
657a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
658a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
659a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
660a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
66102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
662a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
663e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
664e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
6651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
666e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
667e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
668a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
669e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
670a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
671a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
672a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
673a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
674a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Sema::LookupResult R;
675a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
676a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                              Sema::LookupOrdinaryName, true);
677a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
678a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    PrevDecl = R.getAsSingleDecl(SemaRef.Context);
679a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
680a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
681a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
682a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
683a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
684a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
685a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = 0;
686a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
687a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
688fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
689e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
690e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
691a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
692a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
693a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  NamedDecl *FromFriendD
694a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
695a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FromFriendD->getFriendObjectKind()) {
696a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
697a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
698a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
699a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
700a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
701a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
702a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
703a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
704a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
705a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!Owner->isDependentContext() && !PrevDecl)
706a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
707a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
708a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!TemplateParams)
709a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
710a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
711a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
712a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
713127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
714127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
715127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
716d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                                &TemplateArgs.getInnermost(),
717127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
718fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
719fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
720e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
721e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
7222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
723d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
724d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
725d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
7266b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
7276b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
728d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
7291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
731d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
7326b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
7331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
734d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
735d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
7366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
7371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
7406b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7426b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
7436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
7446b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
7456b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
7466b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
747550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
74848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
7490ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
750ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
7512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
7522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
7532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
7552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
756dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
758dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
75917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
760dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
761dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
762dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
76617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getDeclaratorInfo(),
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
7680130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
76917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
77017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
77117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
77217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
77317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
77417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
7750130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
77665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
77865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
779183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
78065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
78165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
78265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
78365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
78465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       T, Conversion->getDeclaratorInfo(),
7850130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
78665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
787dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
789dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->getDeclName(), T, D->getDeclaratorInfo(),
7900130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
791dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
7926b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
793d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
794d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
795d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
797d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
798d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
799d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
800d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
801d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
802d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
803d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
804d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
805d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
806d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
807d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
808d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
810d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
811d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
813d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
814d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  } else if (!FunctionTemplate)
8152db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
8162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
8187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
8197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
8207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
8217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
8245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
8255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
826beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
8275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
8295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
8302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
831dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  NamedDecl *PrevDecl = 0;
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
833d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
834f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    Sema::LookupResult R;
835f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
836f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    PrevDecl = R.getAsSingleDecl(SemaRef.Context);
8371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
838dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
839dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
840dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
841dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
842dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
843dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor      PrevDecl = 0;
844dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
8452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
846d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams)
8476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // Record this function template specialization.
8486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
8496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              FunctionTemplate,
850d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                              &TemplateArgs.getInnermost(),
8516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              InsertPos);
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
85465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
855fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
85665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
85765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
858a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
859a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
860dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
8632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
8642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
865615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
866dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
867615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
868615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
86903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
87017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
87103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
87203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
873bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
87465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
875bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
876bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
8776477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
87858e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
87958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  DeclaratorInfo *DI = D->getDeclaratorInfo();
88058e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
88158e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
88258e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
88358e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
88458e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
88558e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
88658e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
88758e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
88858e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
88958e4677a948e80c92deeebbcd3bdd9266adda798John McCall
89058e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
8912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
8922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
89358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
8942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
89658e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
89758e4677a948e80c92deeebbcd3bdd9266adda798John McCall    = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
89858e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
8992dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
9009351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
901f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
902f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
9030ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
9040ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
9050ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
9062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
9072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
9082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
90948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
9102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
9112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
9122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
913e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
914e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
915e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
916e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
917e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
918e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
921e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
922550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
923e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
924e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
925e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
926e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
9270f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
9280f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
929e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
930550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
931550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
932550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
933550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
934e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
935e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
936e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
93733642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
93833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
93933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
94033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
94133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  DeclaratorInfo *DI = D->getDeclaratorInfo();
94233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
94333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
94433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
94533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
94633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
94733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
94833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
94933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
95033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
95133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
95233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
95333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
95433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
95533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
95633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
95733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
95833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
95933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
96033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
96133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
96233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
96333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
96433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
96533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
96633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
96733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
96833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
96933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
970550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
971550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
972550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
973550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
97433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
97533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
97633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
9770dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
9789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
9799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
9809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
9819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
9829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
9839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
9849106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
9859106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
9869106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
9879106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
9889106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
9899106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
9909106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
9919106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
9929106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
9939106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
9949106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
9959106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
9969106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
9979106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
9989106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
9999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
10009106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
10019106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
10029106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
10039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
10049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
10059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
10069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorDecl *
10070dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
10110dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
10120dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
10130dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10150dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
10160dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
10170dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
10211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  D->getTargetNameLocation(),
10220d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                  D->getTargetName(), 0, D->isTypeName());
10230d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
10250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                           D);
10260d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
10270dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
10280dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1029ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1030d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
10317e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
10328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
10338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
10348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1035e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1036e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1037e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1038e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1039e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1040e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1041e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1042ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1043e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1044e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1045e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1046e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1047bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1048e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1049e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1050e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1051e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1052bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1053e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
10549148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1055e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1056e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1057e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1058e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1059e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1060e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1061e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1062e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1063e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1064e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1065e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1066e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1067e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1068e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1069e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1070e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1072e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1073ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1074ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1075ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1076ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1077ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1078ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1079ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1080ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1081ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1082ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1083ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1084ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1085ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1086ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1087550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1088550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1089550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1090550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1091550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1092ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1093ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1094ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1095ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1096ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1097ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1099ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1101833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1102833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1103833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1104833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1105833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1106833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1107833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1108833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                      TemplateArgs))
1109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1111ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1112ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1113ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1114ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1115ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1117ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1119ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        /*FIXME:*/PartialSpec->getLocation(),
1120ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.data(),
1121ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size(),
1122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        /*FIXME:*/PartialSpec->getLocation(),
1123ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1124ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1125ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1126ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1127ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1128ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1129ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1130ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1131ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1132ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1133ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1134ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1135ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1136ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1137ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1138ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1139ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1140ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1141ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1142ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1143ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1144ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1145ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1146ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1147ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1148ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1149ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1150ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1151ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1152ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
1153ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType WrittenTy
1154ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1155ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    InstTemplateArgs.data(),
1156ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    InstTemplateArgs.size(),
1157ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1158ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1159ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1160ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1161ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1162ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1163ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1164ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1165ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1166ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1167ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1168ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1169ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1170ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1171ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1172ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1173ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1174ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1175ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1176ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1177ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1178ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1179ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1180ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1181ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1182ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1183ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1184ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1185ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1186ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1187ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1188ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1189ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1190833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                                     InstTemplateArgs.data(),
1191833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                                     InstTemplateArgs.size(),
1192ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1193ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1194ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1195ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1196ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1197ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1198ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1199ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1200ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1201ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1202ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1203ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
1204ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
12055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
1206ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
12075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
12085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
12095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1210ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
12115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1213ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
12145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
12155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
12165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1217ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
12187e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
12190ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
12215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
12225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
12236477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
12245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
12255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
12265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
1228ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
1229ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
1230ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
12315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
12325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
12345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
12355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
12375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
12395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
12405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
12415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
12435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
12445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
12455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1246183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
12475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
1249ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1250ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
12515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
12525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
12535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1254beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
12555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
12565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
12575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
12585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1260e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1261e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1262e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
12631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1265e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1266e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1267e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1270cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1271cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
12731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1274cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1275cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1276cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1277cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1278cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1279cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1280cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
12811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1282cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1284cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1285bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1286cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1287cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1288cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1289cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1291e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1292e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1293e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
12945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
12955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
12965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
12975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
12985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
13001eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
13015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1302e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1303e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
13065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
130777b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
130877b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
13095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
13105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
13111d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman    Record->setEmpty(false);
13125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
13135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
13145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
13155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
13165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
13175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
13185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
13195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
13205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
13215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
13225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1323a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1324a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1325a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1326a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1327b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1328b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1329b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1330b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1331a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1332b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1333b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1334b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1335b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1336b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1337e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1338e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1339e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1340e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1341f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1342b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1343e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1344e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
134554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
134654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
134754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
13486fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1350251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1351251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1352251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1353251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
13541eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
13553b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
13561eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
13571eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
13586fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
13591eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1360e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1361e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1362e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1363e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1364e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1365e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1366e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1367e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1368e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1369e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1370e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1371e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1372e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1373e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1374e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1375e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
13761eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1377e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
13781eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1379d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1380d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1382d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1384d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
13857ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1386d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1388f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1389f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1390f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1391b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1392b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1393b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1394b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1395b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1396b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1397b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1399e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1400e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
140154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
140254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
140354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
140554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
140654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
140754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
140854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
140954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
141054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1411b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1412b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1413b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1414b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1415b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1417090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1418090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1419090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1421090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1422090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1423090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
14251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1427090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1428e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
142952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
143052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
143152604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1433e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1434b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1435b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1436aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1437aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1438aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1440b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1441b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1443b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1445b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1446b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1447b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1448a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1449a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1450a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1451a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1452a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
14537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
14547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
14557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
14567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
14577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
14587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
14597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
14607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
14617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1462e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1463e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1464e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1465e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
14667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
14677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
14687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1469e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1470e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
14717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
14727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
14757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
14767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
14770d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
14780d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14800d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
14817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
14827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
1485e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
14860d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
1487e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
1488e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
1489e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
1490e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1491e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1492e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
14937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
14947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
14957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1496251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
14971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1498251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1499251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1500251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
1501251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1502251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1503251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
15041028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
1505251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1506251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
15097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
15107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
15137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
15147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
15157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
15167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
15177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
15207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
15217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
15227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15241028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
1525ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
15267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
15277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
15287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
15297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
15301028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    Var->setPreviousDeclaration(OldVar);
1531583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1532583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
1533583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1534583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
15357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
15367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
15377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
15407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
15427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
15457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
15461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1547a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1548815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1549090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1550090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1551090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1552090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1554090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1555090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1556090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1557090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
155872f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
155972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1560090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1561090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1562090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1564090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1565090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1566090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1567090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1568090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1569090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1570090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1571090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1572090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1573090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1574090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1575090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1576090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1577090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1578c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      QualType BaseType(Init->getBaseClass(), 0);
1579c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1580c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman                           New->getDeclName());
1581090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1582090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInit = BuildBaseInitializer(BaseType,
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1584090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1585090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getSourceLocation(),
1586090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1587090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1588090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
15899988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15919988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
15929988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1593e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
15949988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
1595e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1596e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1599090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1600090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1601090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1602090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1603090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1604090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1605090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1606090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1607090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1608090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1610090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1611090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1612090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1614090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1616090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1617090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       NewInits.data(), NewInits.size());
1619090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1620090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
162152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
162252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
162352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
162452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
162552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
162652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
162752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
162852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
162952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
163052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
163152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
163252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
163352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
163452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
163552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
16360d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
16370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
16380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
16390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
16400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
16410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
16420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
16430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
16440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
16450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
16460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
16470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
16480d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1649ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
1650ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
1652ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
1653ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1654ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
1655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
1657ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
1658ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
1660ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
1661ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1662ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1663ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1664ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
166552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
166652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
166752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
166852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
166952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
167052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
167152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
167252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
167352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
167452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
167552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
167652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
167752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
167852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
167952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
168052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
168152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
168252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
168352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
168452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
168552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
168652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
168752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
168852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
168952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
169052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
169152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
169252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
169352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
169452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
169552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
169652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
169752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
169852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
169952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
170052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
170152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
170252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
170352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
17040d8df780aef1acda5962347a32591efc629b6748Anders Carlssonstatic bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
17050d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
17060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
17070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
17080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
17090d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
171052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
171152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
171252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
171352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
171452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
171552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
171652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
171752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
171852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
171952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
172052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
172152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
172252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
172352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
172452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1725815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
17260d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
17270d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
17280d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
17290d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
17300d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
17310d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
1732815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
17330d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
17340d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
17351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
173752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
174052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
1741815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
174252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
174352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
1744815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
17457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
174652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
174752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
174852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
174952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
175052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
1751a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
17520d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
17530d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
17540d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1755ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
1756ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1757ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1758ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
1759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1760d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1761d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
1762d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
1764d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
1765d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
1766d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
17671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1768815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
1769815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1770815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1771815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1772815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
17731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
1774815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
1775815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
1776815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
1777815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
1778815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
1779815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
1780815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1781815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
1782815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1783815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
178402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
178502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
178602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
178702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
1788e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1789e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
179002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
1791e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
179202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
179302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
179402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
179502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1796ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
1797ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
1798815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1799815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
1800815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
1801815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
1802815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
1803815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1804815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
1805815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
1806815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
1807815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
1808815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
1809815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
1810815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1811815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
1812815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
1813815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1814815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
1815815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
1816815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1817815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
1818815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
1819815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
1820ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1821ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
1822e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1823e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
182444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
182544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    // Transform all of the elements of the overloaded function set.
18261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    OverloadedFunctionDecl *Result
182744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
18281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
183044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                                                FEnd = Ovl->function_end();
183144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor         F != FEnd; ++F) {
183244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      Result->addOverload(
1833e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1834e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                               TemplateArgs)));
183544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    }
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return Result;
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
18391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1840815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
1841550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1842550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1843550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
18442bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
18452bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
18462bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
18472bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
1848815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1849e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1850e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
1851e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
1852e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1853e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // If the RecordDecl is actually the injected-class-name or a "templated"
1854e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // declaration for a class template or class template partial
1855e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // specialization, substitute into the injected-class-name of the
1856e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // class template or partial specialization to find the new DeclContext.
1857e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
1858e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1859e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1860e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
1861e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
1862e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1863e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1864e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = Context.getTypeDeclType(Record);
1865e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
1866e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    }
1867e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1868e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
1869e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // Substitute into the injected-class-name to get the type corresponding
1870e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // to the instantiation we want. This substitution should never fail,
1871e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // since we know we can instantiate the injected-class-name or we wouldn't
1872e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // have gotten to the injected-class-name!
1873e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1874e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // instantiation in the common case?
1875e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1876e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1877e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1878e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
1879e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
1880e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
1881e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
1882e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1883e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // We are performing "partial" template instantiation to create the
1884e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // member declarations for the members of a class template
1885e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // specialization. Therefore, D is actually referring to something in
1886e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the current instantiation. Look through the current context,
1887e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // which contains actual instantiations, to find the instantiation of
1888e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the "current instantiation" that D refers to.
18891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
189052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
18911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
189252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
1893e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
1894e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
189552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
189652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
189752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
18981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(false &&
189952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
1900e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return Record;
190152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
1902e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1903e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
1904e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
1905e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
190652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1907e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
1908e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
1909e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1910e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
19111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
191244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
19131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1914815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
1915815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
1916815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
1917815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
1918815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
1919815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
192017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
1921815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
1922815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
1923815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
1924815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
1925815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
1926815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1927815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
1928815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
1929815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1930815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
193217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
193317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
1934815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1936815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1937815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1938815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1939815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1940815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1941815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1942d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
19431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
1944d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1945d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1946d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1947d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1948b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
19491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
19517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
19521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
1953c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
1954c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
1955c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
19561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19576fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1958b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
19597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
19607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
19611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
19637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
19647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
1965c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
1967c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
1968c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
1969c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
1970c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
19711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1973d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1974d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1975