SemaTemplateInstantiateDecl.cpp revision c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8
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"
1883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h"
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  class VISIBILITY_HIDDEN TemplateDeclInstantiator
25b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
287e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor    const TemplateArgumentList &TemplateArgs;
298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
347e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                             const TemplateArgumentList &TemplateArgs)
357e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
37390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
39390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
404f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
414f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
476477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
48fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    Decl *VisitFriendClassDecl(FriendClassDecl *D);
49e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
50d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
51d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
52d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
53615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
55bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
566477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
58e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
59d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
60e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitDecl(Decl *) {
643d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      assert(false && "Template instantiation of unknown declaration kind!");
658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
68fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
69fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
70fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
71fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
73ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
75e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
77e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
78e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
79ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
834f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
844f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
854f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
864f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
884f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
894f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
904f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
914f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
924f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
934f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
944f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getUnderlyingType();
988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType()) {
99ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    T = SemaRef.SubstType(T, TemplateArgs,
100ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getLocation(), D->getDeclName());
1018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (T.isNull()) {
1028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = SemaRef.Context.IntTy;
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                          D->getIdentifier(), T);
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
11417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
115bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
1168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
120ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
121ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
122ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                 D->getTypeSpecStartLoc(),
123ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                 D->getDeclName());
1243d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (T.isNull())
1253d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
127b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
130a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                 T, D->getDeclaratorInfo(),
131a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
1323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1333d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1343d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a static data member defined
1377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
1387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
1407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
1417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
142390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
143390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
1443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
145eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
1467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
1487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
1497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
1507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
1517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
1527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1543d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    OwningExprResult Init
156ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
1573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    if (Init.isInvalid())
1583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      Var->setInvalidDecl();
15983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
16083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // FIXME: We're faking all of the comma locations, which is suboptimal.
16183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Do we even need these comma locations?
16283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
16383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      if (PLE->getNumExprs() > 0) {
16483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
16583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
16683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          Expr *E = PLE->getExpr(I)->Retain();
16783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          FakeCommaLocs.push_back(
16883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
16983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        }
170e9f8eb64ed2d41c23db4a9774768613d4e07a865Douglas Gregor        PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
17183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
17283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor
17383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Add the direct initializer to the declaration.
17483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
17583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getLParenLoc(),
17683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            Sema::MultiExprArg(SemaRef,
17783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                       (void**)PLE->getExprs(),
17883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                           PLE->getNumExprs()),
17983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            FakeCommaLocs.data(),
18083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getRParenLoc());
18183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor
18283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // When Init is destroyed, it will destroy the instantiated ParenListExpr;
18383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // we've explicitly retained all of its subexpressions already.
18483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    } else
185b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
1863d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                   D->hasCXXDirectInitializer());
18765b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
18865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
1893d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Link instantiations of static data members back to the template from
1917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // which they were instantiated.
1927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isStaticDataMember())
1937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
1947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1953d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
1963d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
1973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getType();
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType())  {
202ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    T = SemaRef.SubstType(T, TemplateArgs,
203ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getLocation(), D->getDeclName());
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (!T.isNull() && T->isFunctionType()) {
2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
2078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
2088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
2098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
2108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        << T;
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = QualType();
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
2218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
222ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
223ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
224ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
226ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
2278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
2288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
2308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
231e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
235a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                            D->getDeclaratorInfo(),
2368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            cast<RecordDecl>(Owner),
2378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
2388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
2398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
240ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
2418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
2428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Field) {
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Invalid)
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Field->setInvalidDecl();
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
24717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    Owner->addDecl(Field);
2488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
253fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCallDecl *TemplateDeclInstantiator::VisitFriendClassDecl(FriendClassDecl *D) {
254fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  QualType T = D->getFriendType();
255fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  if (T->isDependentType())  {
256ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    T = SemaRef.SubstType(T, TemplateArgs, D->getLocation(),
257ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          DeclarationName());
258fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    assert(T.isNull() || getLangOptions().CPlusPlus0x || T->isRecordType());
259fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
260fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
261fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  // FIXME: the target context might be dependent.
262fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  DeclContext *DC = D->getDeclContext();
263fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  assert(DC->isFileContext());
264fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
265fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  FriendClassDecl *NewD =
266fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    FriendClassDecl::Create(SemaRef.Context, DC, D->getLocation(), T,
267fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall                            D->getFriendLoc());
268c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman  NewD->setLexicalDeclContext(Owner);
269c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
270fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  Owner->addDecl(NewD);
271fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  return NewD;
272fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
273fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
2748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
2758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
2768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
277ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
278ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
279ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
281ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
2828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
2838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
2848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
28543d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
28643d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
2878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Decl *StaticAssert
288b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
289b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
290b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
2918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
2928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
2958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
2968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
297741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
2988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
2998dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
30006c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
30117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
3028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
3038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3040ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
3058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
30717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
30817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
3098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
312ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
313ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
314ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      EnterExpressionEvaluationContext Unevaluated(SemaRef,
315ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
316ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
317ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
318ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
3198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
3218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
3228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
3248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
3258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    EnumConstantDecl *EnumConst
3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
3308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
3318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
3338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
3348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
3358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
3368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
33917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
340b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
345c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
346fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
347c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
348c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
349fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
350fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3556477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
3566477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
3576477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
3586477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
3596477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
360e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
361e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
362ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
363d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstParams)
364d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
365e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
366e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
367e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
368e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
369e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
370e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
371e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
372e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
373e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
374e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
375e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
376e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setAccess(D->getAccess());
377e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
378e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
379e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
380e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
381e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
382e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
383d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
384d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
385d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
386d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
387d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstParams)
388d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
389d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
390d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // FIXME: Handle instantiation of nested function templates that aren't
391d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // member function templates. This could happen inside a FriendDecl.
392d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
393d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  CXXMethodDecl *InstMethod
394d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    = cast_or_null<CXXMethodDecl>(
395d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
396d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                    InstParams));
397d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstMethod)
398d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
399d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
400d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // Link the instantiated function template declaration to the function
401d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
402d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
403d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
404d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  InstTemplate->setInstantiatedFromMemberTemplate(D);
405d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  Owner->addDecl(InstTemplate);
406d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
407d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
409d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
410d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
411d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
412d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
413d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
414d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
415d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
416741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
417741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
418d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
419d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setAccess(D->getAccess());
420d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
421d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Record->setInstantiationOfMemberClass(D);
422d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
42317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
424d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
425d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
426d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
427e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
428127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
429127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
430127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
431127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
432127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
433127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
434127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
435127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                          TemplateArgs.getFlatArgumentList(),
436828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                TemplateArgs.flat_size(),
437828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
438127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
439127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo *Info
440127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
441127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
442127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
443127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
444127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
445127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
446127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
447e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
448e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
449e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
450e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
451ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
452e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
4532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
454fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
455e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
456fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  FunctionDecl *Function;
457fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  if (FriendFunctionDecl* FFD = dyn_cast<FriendFunctionDecl>(D)) {
458fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // The new decl's semantic context.  FIXME:  this might need
459fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // to be instantiated.
460fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    DeclContext *DC = D->getDeclContext();
461fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
462fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // This assert is bogus and exists only to catch cases we don't
463fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // handle yet.
464fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    assert(!DC->isDependentContext());
465fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
466fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    Function =
467fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      FriendFunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
468a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                 D->getDeclName(), T, D->getDeclaratorInfo(),
469a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                 D->isInline(), FFD->getFriendLoc());
470fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    Function->setLexicalDeclContext(Owner);
471fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  } else {
472fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    Function =
473fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      FunctionDecl::Create(SemaRef.Context, Owner, D->getLocation(),
474a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getDeclName(), T, D->getDeclaratorInfo(),
475a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
476a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                           D->isInline(), D->hasWrittenPrototype());
477fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
478e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
479e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
480e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
481e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
482e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
483e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
484e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
485e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
486e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
487e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
488e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
489e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
491e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
492e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
493127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
494127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
495127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
496127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
497127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                &TemplateArgs,
498127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
499fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
500fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
501fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  // If this was a friend function decl, it's a member which
502fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  // needs to be added.
503fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  if (isa<FriendFunctionDecl>(Function)) {
504fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // If the new context is still dependent, this declaration
505fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    // needs to remain hidden.
506fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    if (Owner->isDependentContext())
507fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      Owner->addHiddenDecl(Function);
508fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    else
509fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      Owner->addDecl(Function);
510fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
511127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
512e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
5142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
515d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
516d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
517d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
5186b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
5196b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
520d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
521d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are creating a function template specialization from a function
522d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // template. Check whether there is already a function template
523d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
5246b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
5256b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
5266b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                          TemplateArgs.getFlatArgumentList(),
5276b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                TemplateArgs.flat_size(),
5286b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
5296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
5306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    FunctionTemplateSpecializationInfo *Info
5316b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
5326b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
5336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
5346b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
5356b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
5366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
5376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
5386b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
53948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
54048dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
5410ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
542ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
5432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
5442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
5452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
5472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
548dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
549dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
550dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
55117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
552dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
553dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
554dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
555dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
55617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getLocation(),
557dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                        Name, T,
55817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getDeclaratorInfo(),
55917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->isExplicit(),
56017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->isInline(), false);
56117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
56217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
56317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
56417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
56517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
56617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
56717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       T, Destructor->isInline(), false);
56865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
56965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    CanQualType ConvTy
57065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
57165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                      T->getAsFunctionType()->getResultType());
57265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
57365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
57465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
57565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
57665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       T, Conversion->getDeclaratorInfo(),
57765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isInline(),
57865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
579dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
580dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
581dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->getDeclName(), T, D->getDeclaratorInfo(),
582dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->isStatic(), D->isInline());
583dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
5846b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
585d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
586d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
587d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
588d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
589d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
590d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
591d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
592d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
593d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
594d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
595d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
596d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
597d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
598d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
599d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
600d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
601d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getDeclName(),
602d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
603d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  } else if (!FunctionTemplate)
6076b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setInstantiationOfMemberFunction(D);
6082dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a member function defined
6107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
6117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
6127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
6137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
6147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
6155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
6165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
6175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
618beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
6195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
6215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
6222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
623dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  NamedDecl *PrevDecl = 0;
624dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
625d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
626dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
627dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                           Sema::LookupOrdinaryName, true);
628dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
629dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
630dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
631dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
632dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
633dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
634dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor      PrevDecl = 0;
635dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
6362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
637d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams)
6386b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // Record this function template specialization.
6396b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
6406b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              FunctionTemplate,
6416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              &TemplateArgs,
6426b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              InsertPos);
64365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
64465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
64565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
64665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
64765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
64865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
64965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
650dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
651dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
6522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
6532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
6542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
655615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
656dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
657615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
658615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
65903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
66017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
66103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
66203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
663bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
66465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
665bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
666bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
6676477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
668ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
6697e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                                           D->getLocation(), D->getDeclName());
6702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (OrigT.isNull())
6712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
6722dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType T = SemaRef.adjustParameterType(OrigT);
6742dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
6762dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  ParmVarDecl *Param = 0;
6772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T == OrigT)
6782dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
679a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getIdentifier(), T, D->getDeclaratorInfo(),
680a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getStorageClass(), 0);
6812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  else
6822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
6832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                        D->getLocation(), D->getIdentifier(),
684a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        T, D->getDeclaratorInfo(), OrigT,
685a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        D->getStorageClass(), 0);
6862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6879351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
6889351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  if (Expr *Arg = D->getDefaultArg())
6899351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson    Param->setUninstantiatedDefaultArg(Arg);
6909351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson
6912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
6922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
6932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
69448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
6952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
6962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
6972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6982dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl *
6992dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
7002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Since parameter types can decay either before or after
7012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // instantiation, we simply treat OriginalParmVarDecls as
7022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // ParmVarDecls the same way, and create one or the other depending
7032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // on what happens after template instantiation.
7042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return VisitParmVarDecl(D);
7052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
7062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
707e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
708e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
710e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
711e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
712e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
714e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getDepth(), TTPT->getIndex(),
717e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
719e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
721e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (D->hasDefaultArgument()) {
722e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultPattern = D->getDefaultArgument();
723e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultInst
724ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstType(DefaultPattern, TemplateArgs,
725ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDefaultArgumentLoc(),
726ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDeclName());
727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
728e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Inst->setDefaultArgument(DefaultInst,
729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->getDefaultArgumentLoc(),
730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->defaultArgumentWasInherited() /* preserve? */);
731e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
735e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
736ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
737ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                      const TemplateArgumentList &TemplateArgs) {
7387e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
7398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
7408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
744e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
749ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  typedef llvm::SmallVector<Decl*,8> ParamVector;
755e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
756e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
757e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
759e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *D = Visit(*PI);
760e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
761e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Invalid = Invalid || !D;
762e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
763e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
764e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
765e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
766e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
767e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
768e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
769e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
770e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
771e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
773e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
774e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
775e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
776e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
778e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
779e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
780ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
781ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
7825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
783ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
7845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
7855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
7865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
787ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
7885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
7895545e166a956a20d7a6b58408e251a1119025485Douglas GregorQualType
790ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
7915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
7925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
7935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
794ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
7957e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
7960ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
7975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(),
7985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
7995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
8006477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
8015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
8025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
8035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
804ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
805ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
806ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
807ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
8085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
8095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
8115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
8125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
8145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
8155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    } else
8165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
8175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
8185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
8205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
8215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
8245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
8255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  QualType ResultType
826ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
827ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
8285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
8295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
831beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
8325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
8335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
8345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
8355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
836e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Initializes the common fields of an instantiation function
837e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
838e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
839e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
840e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool
841e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
842e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
843e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
844e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
845cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
846cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
847cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
848cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
849cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // to keeping the new function template specialization. We therefore
850cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // convert the active template instantiation for the function template
851cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
852cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
853cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
854cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
855cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
856cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
857cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
858cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    if (FunctionTemplateDecl *FunTmpl
859cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
860cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
861cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
862bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
863cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
864cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
865cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
866cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
867cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
868e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
869e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
870e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
8715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
8725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
8735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
8745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
8755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
8765545e166a956a20d7a6b58408e251a1119025485Douglas Gregorbool
8775545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
8785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
879e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
881e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
8825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
8835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
88477b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
88577b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
8865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
8875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
8881d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman    Record->setEmpty(false);
8895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
8905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
8915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
8925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
8935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
8945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
8955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
8975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
8985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
8995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
900a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
901a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
902a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
903a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
904b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
905b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
906b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
907b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
908a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
909b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
910b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
911b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
912b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
913b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
914f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
915b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
916b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         bool Recursive) {
91754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
91854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
91954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
9206fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
921d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
9221eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
9231637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  const FunctionDecl *PatternDecl = 0;
9241637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate())
9251637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Primary->getTemplatedDecl();
9261637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  else
9271637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Function->getInstantiatedFromMemberFunction();
9281eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
9291eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
9306fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
9311eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
9321eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (!Pattern)
9331eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
9341eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
935f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
936f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
937f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
938b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
939b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
940b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
941b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
942b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
943b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
944b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
945b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
946e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
947e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
94854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
94954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
95054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
95154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
95254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
95354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
95454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
95554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
95654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
95754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
958b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
959b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
960b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
961b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
962b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
96354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
96454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  OwningStmtResult Body
965ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SubstStmt(Pattern, getTemplateInstantiationArgs(Function));
966e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
967e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
968e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
969b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
970b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
971aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
972aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
973aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
974b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
975b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
976b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
977b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // instantiation of this template.
978b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
979b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
980b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
981b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
982b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
983a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
984a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
985a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
986a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
987a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
9887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
9897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
9907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
9917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
9927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
9937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
9947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
9957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
9967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
9977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
9987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
9997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
10007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 bool Recursive) {
10017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
10027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
10057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // FIXME: Do we have to look for specializations separately?
10067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
10077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  bool FoundOutOfLineDef = false;
10087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
10097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
10107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
10117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                             RDEnd = Def->redecls_end();
10127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor       RD != RDEnd; ++RD) {
10137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext()) {
10147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      Def = *RD;
10157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      FoundOutOfLineDef = true;
10167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
10177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (!FoundOutOfLineDef) {
10207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
10217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
10227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiate this definition (or provide a specialization for it) in
10237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // another translation unit.
10247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
10287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
10297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
10327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
10337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
10347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
10357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
10367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
10377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
10397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
10407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
10417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
10427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#if 0
10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Instantiate the initializer of this static data member.
10457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  OwningExprResult Init
10467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiateExpr(Def->getInit(), getTemplateInstantiationArgs(Var));
10477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Init.isInvalid()) {
10487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // If instantiation of the initializer failed, mark the declaration invalid
10497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // and don't instantiate anything else that was triggered by this
10507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiation.
10517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setInvalidDecl();
10527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
10547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
10557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Type-check the initializer.
10607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Init.get())
10617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    AddInitializerToDecl(DeclPtrTy::make(Var), move(Init),
10627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                         Def->hasCXXDirectInitializer());
10637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  else
10647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    ActOnUninitializedDecl(DeclPtrTy::make(Var), false);
10657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#else
1066ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
10677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
10687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor#endif
10697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
10717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
10737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
10747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
10757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
10787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
10797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiation of this template.
10807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
10817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
10837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
10847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1085a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1087815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
1088815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (D->getKind() != Other->getKind())
1089815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    return false;
1090815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1091815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
109297fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Record->getInstantiatedFromMemberClass()->getCanonicalDecl()
109397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
1094815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1095815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
109697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl()
109797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
1098815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
10998dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
110097fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl()
110197fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             == D->getCanonicalDecl();
1102815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
11037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
11047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (Var->isStaticDataMember())
11057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      return Var->getInstantiatedFromStaticDataMember()->getCanonicalDecl()
11067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor               == D->getCanonicalDecl();
11077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1108815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  // FIXME: How can we find instantiations of anonymous unions?
1109815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1110815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
1111815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1112815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1113815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1114815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
1115815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
1116815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
1117815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
1118815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
1119815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
1120815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
1121815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
1122815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1123815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
1124815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1125815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1126ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
1127ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
1128815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1129815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
1130815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
1131815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
1132815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
1133815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1134815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
1135815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
1136815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
1137815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
1138815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
1139815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
1140815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1141815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
1142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
1143815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1144815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
1145815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
1146815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1147815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
1148815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
1149815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
1150ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1151ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
1152ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallNamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) {
1153815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
11542bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
11552bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
11562bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
11572bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
11582bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
1159815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
11602bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) {
1161ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    ParentDecl = FindInstantiatedDecl(ParentDecl);
1162815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (!ParentDecl)
1163815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return 0;
1164815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1165815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    ParentDC = cast<DeclContext>(ParentDecl);
1166815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1167815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1168815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
1169815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
1170815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
1171815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
1172815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
1173815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
117417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
1175815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
1176815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
1177815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
1178815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
1179815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
1180815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1181815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
1182815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
1183815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1184815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
1185815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D,
118617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
118717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
1188815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
1189815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1190815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1191815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1192815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1193815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
1194ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor    if (ClassTemplateDecl *ClassTemplate
1195ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor          = Record->getDescribedClassTemplate()) {
1196ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // When the declaration D was parsed, it referred to the current
1197ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // instantiation. Therefore, look through the current context,
1198ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // which contains actual instantiations, to find the
1199ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // instantiation of the "current instantiation" that D refers
1200ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // to. Alternatively, we could just instantiate the
1201ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // injected-class-name with the current template arguments, but
1202ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      // such an instantiation is far more expensive.
1203ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      for (DeclContext *DC = CurContext; !DC->isFileContext();
1204ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor           DC = DC->getParent()) {
1205ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor        if (ClassTemplateSpecializationDecl *Spec
1206ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
120797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis          if (Spec->getSpecializedTemplate()->getCanonicalDecl()
120897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis              == ClassTemplate->getCanonicalDecl())
1209ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor            return Spec;
1210ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      }
1211ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor
1212ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor      assert(false &&
1213ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor             "Unable to find declaration for the current instantiation");
1214815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
1215815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1216815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1217815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1218d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
1219d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// \brief Performs template instantiation for all implicit template
1220d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1221d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1222d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1223d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1224b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
1225d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
12267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
12277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
12286fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1229b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
12307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
12317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
1232d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
12337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
12347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
12357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
12367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1237d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1238d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1239