SemaTemplateInstantiateDecl.cpp revision ed9c0f90b7e0811c209b95e39fe07c211c531285
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
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
35d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
367e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
39390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
414f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
424f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
486477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
4902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
50a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
51a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
52d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
53d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
54d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
55615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
57bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
586477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
59e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
607974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
617974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
62d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
63e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Decl *VisitDecl(Decl *) {
693d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      assert(false && "Template instantiation of unknown declaration kind!");
708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
73fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
74fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
75fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
76fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
78ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
80e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
82e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
83e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
84ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
85ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
86ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
87ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
88ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
924f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
934f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
944f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
954f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
964f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
974f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
984f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
994f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1004f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1014f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1024f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1034f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
106ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
107ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
108ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
109ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
110ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
112ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall      DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
119ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
12317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
129ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
1300a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
1310a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
1320a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
1330a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
1340a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
1353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
137b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1393d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
1400a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
141a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
1423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
1477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
1487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
1507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
153390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
1543d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
155eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
1587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
1597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
1607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
1617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
1627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
165251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
166251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
167251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
168251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor                                                        TSK_ImplicitInstantiation);
169251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1703d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    OwningExprResult Init
172ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
1733d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    if (Init.isInvalid())
1743d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      Var->setInvalidDecl();
17583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // FIXME: We're faking all of the comma locations, which is suboptimal.
17783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Do we even need these comma locations?
17883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
17983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      if (PLE->getNumExprs() > 0) {
18083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
18183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
18283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          Expr *E = PLE->getExpr(I)->Retain();
18383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          FakeCommaLocs.push_back(
18483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
18583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        }
186e9f8eb64ed2d41c23db4a9774768613d4e07a865Douglas Gregor        PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
18783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Add the direct initializer to the declaration.
19083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            PLE->getLParenLoc(),
19283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            Sema::MultiExprArg(SemaRef,
19383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                       (void**)PLE->getExprs(),
19483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                           PLE->getNumExprs()),
19583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            FakeCommaLocs.data(),
19683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getRParenLoc());
1971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // When Init is destroyed, it will destroy the instantiated ParenListExpr;
19983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // we've explicitly retained all of its subexpressions already.
20083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    } else
201b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
2023d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                   D->hasCXXDirectInitializer());
20365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
20465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
2053d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
2063d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
2073d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
2083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
2098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
2108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
21107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  DeclaratorInfo *DI = D->getDeclaratorInfo();
21207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
21307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
21407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
21507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
21607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      DI = D->getDeclaratorInfo();
21707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
21807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
2218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
2258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
22607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
2278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
235ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
236ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
239ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
2408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
2418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
244e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
24707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
24807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
2508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
2518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
2528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
253ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
2548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
2558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
256663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
257663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
258f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
259663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
261f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
262f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
265f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
266f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
2678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
270f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
2718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
27502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
27602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
27702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
27802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
27902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
28002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
28102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
28202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
28302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
284fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
28502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
28602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
287fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
28802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
28902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
29002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
29102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
29202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
293a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
294a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
295a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
29602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *NewND = Visit(ND);
29702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
298c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
29902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
30002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
30302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
30402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
3055fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
30602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
30702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
308fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
309fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
313ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
314ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
317ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
3188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
3198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
3208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
32143d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
32243d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
325b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
326b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
3278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
3328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
333741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
3348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
3358dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
33606c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
33717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3400ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
34317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
34417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
348ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
349ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
351ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
353ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
354ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
3578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
3588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
3648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
3658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
3668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
3678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
3698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
37517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
376b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
3778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
3788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
382fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
383c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
384c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
385fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
386fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
3878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
3898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3916477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
3926477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
3936477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
3946477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
3956477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
396ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
397ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
398ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
399ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
401ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
405ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
406ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
407ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
408ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
409ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
411e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
412e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
413ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
415d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
416e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
417e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
418e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
419e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
420e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
421f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
422f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
423e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
424e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
425e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
426e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
427e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
428e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setAccess(D->getAccess());
429e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
430f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
431f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
432f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
433f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
434e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
437ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
438ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
439ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
440ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
441ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
442ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
443ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
444ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
445ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
446ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
447ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
448ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
449ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
450ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
451ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
453e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
455d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
4567974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
4577974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
458ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
459ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
460ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
461ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
462ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
463ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
464ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
465ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
466ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
467ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
468ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
474ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
476ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
477ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
478ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
479ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
480ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
4817974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
4827974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
4837974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
4847974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
485d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
486d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // FIXME: Dig out the out-of-line definition of this function template?
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
489d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
491d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
492ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
493a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
494a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
495a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
496a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
497a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
498a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
499a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
500a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
501a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
502a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
503d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
504d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
506d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
50737d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
508a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
50937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
510a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
511a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
512a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getInstantiatedFromMemberTemplate())
513a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
514a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
515a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
516a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
517a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
518d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
519d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
520d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
521d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
522d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
523d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
524d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
525d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
526d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
528741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
529741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
530d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
531eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
532eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
533eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
534eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
535eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
536d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
537f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
538d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
53902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
54002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
54102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
54202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
54302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
544d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
545d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
54617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
547d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
548d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
549d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
55002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
55102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
55202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
55302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
55402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
555a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
556a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
557127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
558127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
559127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
560127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
561a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
562127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
564d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
565d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
566828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
570127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
572127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
573127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
574127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
575127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
577e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
579e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
580ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
581e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
5822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
583fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
584e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
585e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
586e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                    TemplateArgs);
58702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
589a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getDeclName(), T, D->getDeclaratorInfo(),
590a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
5910130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
59202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
594e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
595e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
596e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
597e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
59802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
599a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
600a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
601a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
602a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
603a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
604a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
605a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
606a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
607a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
608a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
609a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
610a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
611a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
612a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
613a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
614a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
615a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
616a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
617a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
618a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
619a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
62002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
621a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
622e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
623e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
626e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
627a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
628e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
629a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
630a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
631a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
632a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
633a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Sema::LookupResult R;
634a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(),
635a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                              Sema::LookupOrdinaryName, true);
636a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
637a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    PrevDecl = R.getAsSingleDecl(SemaRef.Context);
638a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
639a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
640a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
641a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
642a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
643a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
644a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = 0;
645a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
646a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
647fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
648e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
649e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
650a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
651a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
652a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  NamedDecl *FromFriendD
653a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
654a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FromFriendD->getFriendObjectKind()) {
655a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
656a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
657a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
658a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
659a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
660a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
661a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
662a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
663a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
664a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!Owner->isDependentContext() && !PrevDecl)
665a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
666a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
667a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!TemplateParams)
668a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
669a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
670a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
671a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
672127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
673127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
674127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
675d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                                &TemplateArgs.getInnermost(),
676127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
677fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
678fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
679e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
680e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
6812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
682d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
683d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
684d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
6856b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
6866b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
687d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
6881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
690d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
6916b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
693d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
694d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
6956b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
6996b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7016b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
7026b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
7036b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
7046b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
7056b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
70648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
70748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
7080ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
709ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
7102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
7112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
7122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
7142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
715dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
717dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
71817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
719dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
720dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
721dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
72517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getDeclaratorInfo(),
7261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
7270130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
72817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
72917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
73017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
73117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
73217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
73317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
7340130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
73565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
7361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
73765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
738183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
73965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
74065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
74165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
74265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
74365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       T, Conversion->getDeclaratorInfo(),
7440130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
74565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
746dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
748dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->getDeclName(), T, D->getDeclaratorInfo(),
7490130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
750dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
7516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
752d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
753d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
754d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
756d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
757d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
758d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
759d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
760d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
761d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
762d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
763d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
764d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
765d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
766d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
767d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
769d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
770d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
772d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
773d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  } else if (!FunctionTemplate)
7742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
7752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
7777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
7787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
7797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
7807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
7835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
7845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
785beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
7865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
7875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
7885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
7892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
790dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  NamedDecl *PrevDecl = 0;
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
792d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
793f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    Sema::LookupResult R;
794f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true);
795f36e02d4aff98bf2e52e342e0038d4172fbb5e64John McCall    PrevDecl = R.getAsSingleDecl(SemaRef.Context);
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
797dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
798dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
799dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
800dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
801dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
802dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor      PrevDecl = 0;
803dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
8042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
805d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams)
8066b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // Record this function template specialization.
8076b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
8086b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              FunctionTemplate,
809d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                              &TemplateArgs.getInnermost(),
8106b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              InsertPos);
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
81365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
814fd056bc86a8b22a9421b5d921bbca276d0f9d0f7Douglas Gregor  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
81565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
81665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
817a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
818a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
819dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
8222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
8232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
824615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
825dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
826615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
827615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
82803b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
82917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
83003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
83103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
832bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
83365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
834bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
835bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
8366477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
83758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
83858e4677a948e80c92deeebbcd3bdd9266adda798John McCall  DeclaratorInfo *DI = D->getDeclaratorInfo();
83958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
84058e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
84158e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
84258e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
84358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
84458e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
84558e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
84658e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
84758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
84858e4677a948e80c92deeebbcd3bdd9266adda798John McCall
84958e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
8502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
8512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
85258e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
8532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
85558e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
85658e4677a948e80c92deeebbcd3bdd9266adda798John McCall    = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
85758e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
8582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
8599351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
860f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
861f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
8620ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
8630ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
8640ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
8652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
8662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
8672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
86848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
8692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
8702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
8712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
872e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
873e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
874e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
875e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
876e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
877e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
879e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
880e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
881e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getDepth(), TTPT->getIndex(),
882e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
883e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
884e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
885e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
88633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // FIXME: Do we actually want to perform substitution here? I don't think
88733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // we do.
888e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (D->hasDefaultArgument()) {
889e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultPattern = D->getDefaultArgument();
890e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultInst
891ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstType(DefaultPattern, TemplateArgs,
892ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDefaultArgumentLoc(),
893ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDeclName());
8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
895e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Inst->setDefaultArgument(DefaultInst,
896e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->getDefaultArgumentLoc(),
897e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->defaultArgumentWasInherited() /* preserve? */);
898e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
899e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
900e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
901e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
902e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
90333642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
90433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
90533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
90633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
90733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  DeclaratorInfo *DI = D->getDeclaratorInfo();
90833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
90933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
91033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
91133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
91233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
91333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
91433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
91533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
91633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
91733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
91833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
91933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
92033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
92133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
92233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
92333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
92433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
92533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
92633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
92733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
92833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
92933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
93033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
93133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
93233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
93333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
93433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
93533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
93633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
93733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
93833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
9390dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
9400dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
9440dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
9450dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
9460dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9480dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
9490dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
9500dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  D->getTargetNameLocation(),
9550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                  D->getTargetName(), 0, D->isTypeName());
9560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
9580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                           D);
9590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
9600dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
9610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
962ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
963d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
9647e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
9658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
9668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
9678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
968e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
969e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
970e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
971e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
972e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
973e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
974e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
975ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
976e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
977e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
978e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
979e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
980bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
981e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
982e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
983e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
984e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
985bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
986e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
987e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Invalid = Invalid || !D;
988e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
989e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
990e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
991e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
992e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
993e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
994e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
995e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
996e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
997e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
998e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
999e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1000e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1001e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1002e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1003e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1005e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1006ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1007ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1008ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1009ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1010ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1011ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1012ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1013ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1014ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1015ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1016ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1017ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1018ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1019ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1020ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1021ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1022ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1023ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1024ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1025ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1026ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1027ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1028ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1029ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  const TemplateArgumentList &PartialSpecTemplateArgs
1030ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = PartialSpec->getTemplateInstantiationArgs();
1031ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> InstTemplateArgs;
1032ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecTemplateArgs.size(); I != N; ++I) {
1033ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    TemplateArgument Inst = SemaRef.Subst(PartialSpecTemplateArgs[I],
1034ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                          TemplateArgs);
1035ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Inst.isNull())
1036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1038ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstTemplateArgs.push_back(Inst);
1039ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1040ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1041ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1042ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1043ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1044ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1045ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1046ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1047ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1048ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        /*FIXME:*/PartialSpec->getLocation(),
1049ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.data(),
1050ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size(),
1051ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        /*FIXME:*/PartialSpec->getLocation(),
1052ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1053ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1054ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1055ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1056ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1057ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1058ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1059ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1060ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1061ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1062ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1063ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1064ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1065ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1066ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1067ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1068ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1069ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1070ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1071ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1072ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1073ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1074ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1075ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1076ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1077ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1078ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1079ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1080ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1081ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
1082ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType WrittenTy
1083ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1084ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    InstTemplateArgs.data(),
1085ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    InstTemplateArgs.size(),
1086ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1087ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1088ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1089ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1090ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1091ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1092ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1093ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1094ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1095ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1096ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1097ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1099ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1104ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1105ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1111ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1112ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1113ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1114ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1115ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1117ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1119ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1120ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1121ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1123ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1124ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1125ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1126ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1127ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1128ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1129ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1130ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
1131ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
11325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
1133ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
11345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
11355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
11365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1137ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
11385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1140ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
11415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
11425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
11435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1144ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
11457e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
11460ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
11485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
11495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
11506477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
11515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
11525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
11535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
1155ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
1156ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
1157ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
11585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
11595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
11605545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
11615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
11625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
11635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
11645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
11665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
11675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
11685545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
11695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
11705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
11715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
11725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1173183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
11745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
1176ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1177ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
11785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
11795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
11805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1181beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
11825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
11835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
11845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
11855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1187e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1188e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1189e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1192e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1193e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1194e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1196cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1197cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1198cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1201cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1202cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1203cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1204cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1205cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1206cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1207cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1209cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1211cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1212bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1213cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1214cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1215cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1216cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1218e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1219e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1220e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
12215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
12225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
12235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
12245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
12255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
12285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1229e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1230e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
12335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
123477b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
123577b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
12365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
12375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
12381d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman    Record->setEmpty(false);
12395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
12405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
12415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
12425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
12435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
12445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
12455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
12475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
12485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
12495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1250a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1251a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1252a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1253a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1254b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1255b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1256b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1257b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1258a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1259b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1260b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1261b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1262b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1263b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1264e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1265e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1266e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1267e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1268f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1269b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1270e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1271e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
127254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
127354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
127454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
12756fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
12761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1277251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1278251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1279251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1280251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
12811eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
12823b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
12831eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
12841eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
12856fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
12861eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1287e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1288e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1289e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1290e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1291e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1292e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1293e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1294e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1295e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1296e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1297e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1298e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1299e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1300e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1301e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1302e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
13031eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1304e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
13051eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1306d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1307d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1309d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1311d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
13127ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1313d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1315f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1316f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1317f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1318b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1319b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1320b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1321b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1322b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1323b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1324b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
13251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1326e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1327e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
132854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
132954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
133054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
133354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
133454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
133554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
133654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
133754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1338b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1339b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1340b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1341b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1342b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
13431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1344090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1345090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1346090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1348090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1349090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1350090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1354090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1355e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
135652604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
135752604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
135852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1360e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1361b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1362b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1363aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1364aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1365aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
13661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1367b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1368b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1370b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1372b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1373b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1374b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1375a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1376a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1377a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1378a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1379a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
13807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
13817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
13827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
13837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
13847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
13857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
13867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
13877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
13887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1389e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1390e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1391e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1392e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
13937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
13947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
13957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1396e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1397e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
13987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
13997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
14027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
14037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
14040d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
14050d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14070d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
14087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
14097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
14111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
1412e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
14130d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
1414e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
1415e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
1416e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
1417e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1418e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1419e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
14207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
14217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
14227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1423251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
14241028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1425251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1426251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
1427251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
1428251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1429251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1430251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
14311028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
1432251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1433251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
14367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
14377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
14407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
14417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
14427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
14437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
14447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
14477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
14487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
14497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14511028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
1452ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
14537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
14547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
14557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
14567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
14571028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    Var->setPreviousDeclaration(OldVar);
1458583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1459583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
1460583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1461583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
14627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
14637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
14647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
14677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
14697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
14727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1474a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1475815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1476090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1477090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1478090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1479090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1481090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1482090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1483090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1484090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
148572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
148672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1487090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1488090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1489090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1491090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1492090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1493090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1494090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1495090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1496090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1497090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1498090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1499090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1500090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1501090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1502090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1503090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1504090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1505c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      QualType BaseType(Init->getBaseClass(), 0);
1506c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1507c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman                           New->getDeclName());
1508090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1509090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInit = BuildBaseInitializer(BaseType,
15101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1511090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1512090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getSourceLocation(),
1513090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1514090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1515090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
15169988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15189988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
15199988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1520e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
15219988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
1522e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1523e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1526090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1527090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1528090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1529090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1530090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1531090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1532090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1533090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1534090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1535090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1537090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1538090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1539090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
15401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1541090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1543090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1544090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
15451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       NewInits.data(), NewInits.size());
1546090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1547090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
154852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
154952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
155052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
155152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
155252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
155352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
155452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
155552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
155652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
155752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
155852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
155952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
156052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
156152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
156252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
15630d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
15640d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
15650d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
15660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
15670d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
15680d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
15690d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
15700d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
15710d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
15720d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
15730d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
15740d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
15750d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1576ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
1577ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1578ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
1579ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
1580ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1581ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
1582ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1583ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
1584ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
1585ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1586ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
1587ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
1588ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1589ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1590ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1591ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
159252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
159352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
159452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
159552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
159652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
159752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
159852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
159952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
160052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
160152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
160252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
160352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
160452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
160552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
160652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
160752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
160852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
160952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
161052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
161152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
161252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
161352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
161452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
161552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
161652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
161752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
161852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
161952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
162052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
162152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
162252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
162352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
162452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
162552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
162652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
162752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
162852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
162952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
163052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
16310d8df780aef1acda5962347a32591efc629b6748Anders Carlssonstatic bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
16320d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
16330d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
16340d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
16350d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
16360d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
163752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
163852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
163952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
164052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
164152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
164252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
164352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
164452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
164552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
164652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
164752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
164852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
164952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
165052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
165152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1652815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
16530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
16540d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
16550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
16560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
16570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
16580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
1659815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
16600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
16610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
16621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
166452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
166752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
1668815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
166952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
167052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
1671815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
16727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
167352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
167452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
167552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
167652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
167752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
1678a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
16790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
16800d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
16810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
1682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
1683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1684ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1685ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
1686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1687d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1688d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
1689d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
1691d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
1692d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
1693d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1695815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
1696815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1697815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1698815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1699815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
1701815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
1702815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
1703815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
1704815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
1705815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
1706815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
1707815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1708815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
1709815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1710815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
171102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
171202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
171302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
171402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
1715e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1716e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
171702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
1718e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
171902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
172002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
172102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
172202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1723ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
1724ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
1725815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1726815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
1727815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
1728815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
1729815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
1730815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1731815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
1732815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
1733815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
1734815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
1735815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
1736815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
1737815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1738815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
1739815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
1740815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1741815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
1742815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
1743815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1744815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
1745815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
1746815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
1747ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1748ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
1749e95b40961302c2130968ddfc3ba162e138f2118eDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1750e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
175144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
175244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    // Transform all of the elements of the overloaded function set.
17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    OverloadedFunctionDecl *Result
175444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
17551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
175744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                                                FEnd = Ovl->function_end();
175844c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor         F != FEnd; ++F) {
175944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      Result->addOverload(
1760e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1761e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                               TemplateArgs)));
176244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    }
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
176444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return Result;
17651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1767815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
17682bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
17692bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
17702bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
17712bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
17722bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
1773815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1774e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1775e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
1776e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
1777e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1778e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // If the RecordDecl is actually the injected-class-name or a "templated"
1779e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // declaration for a class template or class template partial
1780e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // specialization, substitute into the injected-class-name of the
1781e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // class template or partial specialization to find the new DeclContext.
1782e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
1783e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1784e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1785e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
1786e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
1787e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1788e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1789e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = Context.getTypeDeclType(Record);
1790e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
1791e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    }
1792e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1793e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
1794e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // Substitute into the injected-class-name to get the type corresponding
1795e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // to the instantiation we want. This substitution should never fail,
1796e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // since we know we can instantiate the injected-class-name or we wouldn't
1797e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // have gotten to the injected-class-name!
1798e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1799e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // instantiation in the common case?
1800e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1801e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1802e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1803e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
1804e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
1805e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
1806e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
1807e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1808e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // We are performing "partial" template instantiation to create the
1809e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // member declarations for the members of a class template
1810e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // specialization. Therefore, D is actually referring to something in
1811e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the current instantiation. Look through the current context,
1812e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // which contains actual instantiations, to find the instantiation of
1813e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      // the "current instantiation" that D refers to.
18141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
181552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
18161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
181752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
1818e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
1819e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
182052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
182152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
182252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(false &&
182452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
1825e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return Record;
182652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
1827e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1828e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
1829e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
1830e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
183152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1832e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
1833e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
1834e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
1835e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
183744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1839815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
1840815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
1841815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
1842815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
1843815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
1844815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
184517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
1846815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
1847815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
1848815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
1849815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
1850815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
1851815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1852815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
1853815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
1854815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1855815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
185717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
185817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
1859815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
18601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1861815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1862815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1863815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1864815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1865815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1866815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1867d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
18681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
1869d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1870d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1871d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1872d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1873b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
18741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
18767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
18771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
1878c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
1879c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
1880c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
18811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18826fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1883b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
18847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
18857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
18861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
18887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
18897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
1890c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
18911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
1892c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
1893c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
1894c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
1895c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
18961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1898d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1899d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1900