SemaTemplateInstantiateDecl.cpp revision 72f6d678c8de9f3a770e8ae5fc4979abf3940668
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
18c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
1983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  class VISIBILITY_HIDDEN TemplateDeclInstantiator
26b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
29d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
35d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
367e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
39390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
414f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
424f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
486477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
4902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
50e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D);
51d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
52d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
53d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
54615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
5503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
56bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
576477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
59e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
60d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
61e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D);
630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitDecl(Decl *) {
663d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      assert(false && "Template instantiation of unknown declaration kind!");
678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
70fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
71fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
72fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
73fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
75ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    QualType SubstFunctionType(FunctionDecl *D,
765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
77e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
79e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
80e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
81ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
854f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
864f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
884f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
894f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
904f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
914f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
924f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
934f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
944f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
954f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
964f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getUnderlyingType();
1008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType()) {
101ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    T = SemaRef.SubstType(T, TemplateArgs,
102ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getLocation(), D->getDeclName());
1038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (T.isNull()) {
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = SemaRef.Context.IntTy;
1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                          D->getIdentifier(), T);
1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
11617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
117bc221637f5ed3538b8495dd13b831c11e821c712Douglas Gregor
1188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1213d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
122ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
123ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SemaRef.SubstType(D->getType(), TemplateArgs,
124ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                 D->getTypeSpecStartLoc(),
125ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                 D->getDeclName());
1263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (T.isNull())
1273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
1283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
129b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
1303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1313d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
132a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                 T, D->getDeclaratorInfo(),
133a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
1343d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
1353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
1363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
1373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a static data member defined
1397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
1407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
1427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
1437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
144390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
145390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
1463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
147eaaebc7cf10dc1a2016183a262ad3256bc468759Chris Lattner  SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
1487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
1507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
1517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
1527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
1537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
1547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    OwningExprResult Init
158ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
1593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    if (Init.isInvalid())
1603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      Var->setInvalidDecl();
16183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
16283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // FIXME: We're faking all of the comma locations, which is suboptimal.
16383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Do we even need these comma locations?
16483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
16583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      if (PLE->getNumExprs() > 0) {
16683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
16783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
16883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          Expr *E = PLE->getExpr(I)->Retain();
16983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor          FakeCommaLocs.push_back(
17083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
17183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor        }
172e9f8eb64ed2d41c23db4a9774768613d4e07a865Douglas Gregor        PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
17383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
17483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor
17583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Add the direct initializer to the declaration.
17683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
17783ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getLParenLoc(),
17883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            Sema::MultiExprArg(SemaRef,
17983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                       (void**)PLE->getExprs(),
18083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                           PLE->getNumExprs()),
18183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            FakeCommaLocs.data(),
18283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getRParenLoc());
18383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor
18483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // When Init is destroyed, it will destroy the instantiated ParenListExpr;
18583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // we've explicitly retained all of its subexpressions already.
18683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor    } else
187b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
1883d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                   D->hasCXXDirectInitializer());
18965b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
19065b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
1913d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
1927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Link instantiations of static data members back to the template from
1937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // which they were instantiated.
1947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isStaticDataMember())
1957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D);
1967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
1983d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
1993d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  QualType T = D->getType();
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (T->isDependentType())  {
204ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    T = SemaRef.SubstType(T, TemplateArgs,
205ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getLocation(), D->getDeclName());
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (!T.isNull() && T->isFunctionType()) {
2078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
2088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
2098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
2108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        << T;
2158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      T = QualType();
2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
2218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
224ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
225ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
226ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
228ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
2298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
2308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
2318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
233e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T,
237a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                            D->getDeclaratorInfo(),
2388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            cast<RecordDecl>(Owner),
2398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
2408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
2418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
242ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Field) {
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Invalid)
2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Field->setInvalidDecl();
2488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
249d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
250d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // Keep track of where this decl came from.
251d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
252d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
253d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
25417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    Owner->addDecl(Field);
2558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
26002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
26102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
26202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
26302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
26402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
26502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
26602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
26702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
26802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
269fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
27002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
27102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
272fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
27302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
27402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
27502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
27602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
27702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
27802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *NewND = Visit(ND);
27902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
280c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
28102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
28202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
28302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
28402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
28502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
28602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
2875fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
28802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
28902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
290fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
291fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
2928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
2938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
2948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
295ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
296ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
297ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
2988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
299ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
3008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
3018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
3028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
30343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
30443d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
3058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Decl *StaticAssert
306b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
307b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
308b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
3098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
3108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
3138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
3148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
315741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
3168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
3178dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
31806c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
31917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
3208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
3218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3220ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
32517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
32617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
3278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
3288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
330ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
331ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
332ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      EnterExpressionEvaluationContext Unevaluated(SemaRef,
333ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
334ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor
335ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
336ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
3378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
3408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    EnumConstantDecl *EnumConst
3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
3478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
3488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
3498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
35717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
358b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
3608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
363c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
364fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
365c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
366c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
367fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
368fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
3698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3736477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
3746477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
3756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
3766477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
3776477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
378e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
379e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
380ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
381d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstParams)
382d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
383e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
384e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
385e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
386e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
387e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
388e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
389e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
390e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
391e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
392e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
393e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
394e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setAccess(D->getAccess());
395e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
396e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
397e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
398e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
399e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
400e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
401d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
402d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
403d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
404d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
405d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstParams)
406d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
407d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // FIXME: Handle instantiation of nested function templates that aren't
409d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // member function templates. This could happen inside a FriendDecl.
410d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
411d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  CXXMethodDecl *InstMethod
412d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    = cast_or_null<CXXMethodDecl>(
413d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
414d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                    InstParams));
415d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstMethod)
416d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
417d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
418d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // Link the instantiated function template declaration to the function
419d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
420d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
421d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
422d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  InstTemplate->setInstantiatedFromMemberTemplate(D);
423d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  Owner->addDecl(InstTemplate);
424d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
425d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
426d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
427d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
428d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
429d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
430d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
431d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
432d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
433d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
434741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
435741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
436d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
437eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
438eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
439eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
440eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
441eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
442d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
443d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Record->setInstantiationOfMemberClass(D);
444d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
44502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
44602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
44702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
44802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
44902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
450d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
451d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
45217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
453d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
454d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
455d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
45602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
45702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
45802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
45902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
46002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
461e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
462127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
463127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
464127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
465127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
466127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
467127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
468127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
469d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
470d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
471828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
472127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
473127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    FunctionTemplateSpecializationInfo *Info
474127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
475127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
476127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
477127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
478127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
479127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
480127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
481e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
482e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
483e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
484e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
485ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
486e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
4872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
488fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
489e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
49002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext());
49102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
49202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
493a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getDeclName(), T, D->getDeclaratorInfo(),
494a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
495a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                           D->isInline(), D->hasWrittenPrototype());
49602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
49702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
499e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
500e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
501e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
50202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
50302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
50402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state and add it to the owner.
50502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
50602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    bool WasDeclared = (FOK == Decl::FOK_Declared);
50702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Function->setObjectOfFriendDecl(WasDeclared);
50802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!Owner->isDependentContext())
509ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall      DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false);
510f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall
511f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall    Function->setInstantiationOfMemberFunction(D);
51202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
514e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
515e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
516e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
517e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
518e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
519e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
520e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
521e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
522e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
523127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
524127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
525127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
526127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
527d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                                &TemplateArgs.getInnermost(),
528127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
529fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
530fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
531e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
532e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
5332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
534d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
535d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
536d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
5376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
5386b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
539d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
540d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are creating a function template specialization from a function
541d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // template. Check whether there is already a function template
542d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
5436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
5446b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
545d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
546d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
5476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
5486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
5496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    FunctionTemplateSpecializationInfo *Info
5506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
5516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
5526b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
5536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
5546b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
5556b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
5566b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
5576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
55848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
55948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
5600ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
561ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
5622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
5632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
5642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
5662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
567dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
568dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
569dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
57017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
571dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
572dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
573dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
574dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
57517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getLocation(),
576dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                        Name, T,
57717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getDeclaratorInfo(),
57817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->isExplicit(),
57917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->isInline(), false);
58017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
58117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
58217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
58317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
58417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
58517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
58617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       T, Destructor->isInline(), false);
58765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
58865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    CanQualType ConvTy
58965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
59065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                      T->getAsFunctionType()->getResultType());
59165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
59265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
59365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
59465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
59565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       T, Conversion->getDeclaratorInfo(),
59665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isInline(),
59765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
598dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
599dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
600dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->getDeclName(), T, D->getDeclaratorInfo(),
601dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->isStatic(), D->isInline());
602dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
6036b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
607d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
611d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
617d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
618d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
619d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
620d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getDeclName(),
621d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
622d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
623d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
624d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
625d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  } else if (!FunctionTemplate)
6266b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setInstantiationOfMemberFunction(D);
6272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we are instantiating a member function defined
6297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
6307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
6317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
6327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
6337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
6345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
6355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
6365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
637beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
6385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
6405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
6412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
642dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  NamedDecl *PrevDecl = 0;
643dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
644d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
645dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
646dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                           Sema::LookupOrdinaryName, true);
647dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
648dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
649dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
650dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
651dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
652dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
653dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor      PrevDecl = 0;
654dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
6552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
656d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams)
6576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // Record this function template specialization.
6586b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
6596b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              FunctionTemplate,
660d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                              &TemplateArgs.getInnermost(),
6616b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              InsertPos);
66265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
66365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
66465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
66565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
66665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
66765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
66865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
669dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
670dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor
6712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
6722dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
6732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
674615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
675dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
676615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
677615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
67803b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
67917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
68003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
68103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
682bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
68365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
684bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
685bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
6866477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
687ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
6887e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                                           D->getLocation(), D->getDeclName());
6892dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (OrigT.isNull())
6902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
6912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType T = SemaRef.adjustParameterType(OrigT);
6932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
6952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  ParmVarDecl *Param = 0;
6962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T == OrigT)
6972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
698a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getIdentifier(), T, D->getDeclaratorInfo(),
699a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getStorageClass(), 0);
7002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  else
7012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
7022dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                        D->getLocation(), D->getIdentifier(),
703a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        T, D->getDeclaratorInfo(), OrigT,
704a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        D->getStorageClass(), 0);
7052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7069351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
7079351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  if (Expr *Arg = D->getDefaultArg())
7089351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson    Param->setUninstantiatedDefaultArg(Arg);
7099351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson
7102dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
7112dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
7122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
71348dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
7142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
7152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
7162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl *
7182dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
7192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Since parameter types can decay either before or after
7202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // instantiation, we simply treat OriginalParmVarDecls as
7212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // ParmVarDecls the same way, and create one or the other depending
7222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // on what happens after template instantiation.
7232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return VisitParmVarDecl(D);
7242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
7252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
726e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
728e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
731e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
735e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getDepth(), TTPT->getIndex(),
736e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
739e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (D->hasDefaultArgument()) {
741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultPattern = D->getDefaultArgument();
742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultInst
743ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstType(DefaultPattern, TemplateArgs,
744ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDefaultArgumentLoc(),
745ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDeclName());
746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Inst->setDefaultArgument(DefaultInst,
748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->getDefaultArgumentLoc(),
749e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->defaultArgumentWasInherited() /* preserve? */);
750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
7550dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
7560dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
7570dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  NestedNameSpecifier *NNS =
7580dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
7590dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     D->getTargetNestedNameRange(),
7600dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
7610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
7620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
7630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
7640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
7650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
7660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
7670dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
7680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  NamedDecl *UD =
7690d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
7700d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                  D->getTargetNameLocation(),
7710d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                  D->getTargetName(), 0, D->isTypeName());
7720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
7730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
7740d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                           D);
7750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
7760dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
7770dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
778ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
779d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
7807e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
7818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
7828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
784e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
785e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
786e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
787e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
791ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
792e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  typedef llvm::SmallVector<Decl*,8> ParamVector;
797e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
800e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
801e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *D = Visit(*PI);
802e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
803e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Invalid = Invalid || !D;
804e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
805e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
808e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
812e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
813e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
814e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
815e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
816e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
817e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
818e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
819e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
820e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
821e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
822ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
823ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
8245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
825ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
8265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
8275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
8285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
829ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
8305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
8315545e166a956a20d7a6b58408e251a1119025485Douglas GregorQualType
832ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
8335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
8345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
8355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
836ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
8377e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
8380ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
8395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (FunctionDecl::param_iterator P = D->param_begin(),
8405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
8415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
8426477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
8435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
8445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
8455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
846ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
847ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
848ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
849ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
8505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
8515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
8535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
8545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
8565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
8575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    } else
8585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
8595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
8605545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8615545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
8625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
8635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
8665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
8675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  QualType ResultType
868ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
869ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
8705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
8715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
873beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
8745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
8755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
8765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
8775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
878e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Initializes the common fields of an instantiation function
879e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
881e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
882e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorbool
883e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
884e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
885e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
886e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
887cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
888cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
889cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
890cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
891cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // to keeping the new function template specialization. We therefore
892cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // convert the active template instantiation for the function template
893cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
894cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
895cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
896cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
897cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
898cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
899cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
900cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    if (FunctionTemplateDecl *FunTmpl
901cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
902cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
903cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
904bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
905cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
906cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
907cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
908cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
909cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
910e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
911e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
912e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
9135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
9145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
9155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
9165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
9175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
9185545e166a956a20d7a6b58408e251a1119025485Douglas Gregorbool
9195545e166a956a20d7a6b58408e251a1119025485Douglas GregorTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
9205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
921e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
922e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
923e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
9245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
9255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
92677b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
92777b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
9285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
9295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
9301d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman    Record->setEmpty(false);
9315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
9325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
9335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
9345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
9355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
9365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
9375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
9385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
9395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
9405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
9415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
942a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
943a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
944a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
945a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
946b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
947b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
948b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
949b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
950a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
951b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
952b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
953b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
954b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
955b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
956f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
957b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
958b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         bool Recursive) {
95954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
96054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
96154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
9626fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
963d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
9641eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
9651637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  const FunctionDecl *PatternDecl = 0;
9665ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor  if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
9675ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor    while (Primary->getInstantiatedFromMemberTemplate())
9685ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
9695ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor
9701637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Primary->getTemplatedDecl();
9715ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor  } else
9721637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Function->getInstantiatedFromMemberFunction();
9731eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
9741eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
9756fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
9761eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
9771eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (!Pattern)
9781eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
9791eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
980f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
981f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
982f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
983b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
984b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
985b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
986b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
987b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
988b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
989b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
990b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
991e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
992e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
99354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
99454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
99554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
99654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
99754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
99854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
99954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
100054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
100154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
100254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1003b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1004b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1005b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1006b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1007b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1008090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  MultiLevelTemplateArgumentList TemplateArgs =
1009090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1010090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1011090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
1012090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  if (const CXXConstructorDecl *Ctor =
1013090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1014090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1015090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
1016090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
1017090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
101854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1019090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1020e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
1021e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1022e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1023b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1024b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1025aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1026aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1027aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
1028b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
1029b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1030b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
1031b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // instantiation of this template.
1032b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
1033b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
1034b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1035b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1036b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1037a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1038a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1039a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1040a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1041a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
10427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
10457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
10467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
10477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
10487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
10497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
10507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
10517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
10527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
10537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
10547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 bool Recursive) {
10557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
10567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
10597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // FIXME: Do we have to look for specializations separately?
10607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
10617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  bool FoundOutOfLineDef = false;
10627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
10637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
10647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
10657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                             RDEnd = Def->redecls_end();
10667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor       RD != RDEnd; ++RD) {
10677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext()) {
10687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      Def = *RD;
10697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      FoundOutOfLineDef = true;
10707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
10717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (!FoundOutOfLineDef) {
10747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
10757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
10767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiate this definition (or provide a specialization for it) in
10777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // another translation unit.
10787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
10827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
10837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
10867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
10877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
10887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
10897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
10907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
10917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
10937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
10947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
10957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
10967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1097ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
10987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
10997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
11017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
11037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
11047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
11057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
11067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
11087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
11097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // instantiation of this template.
11107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
11117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
11137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
11147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
1115a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1116815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1117090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1118090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1119090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1120090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
1121090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1122090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1123090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1124090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1125090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
112672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
112772f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1128090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1129090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1130090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
1131090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1132090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1134090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1135090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1136090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1138090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1139090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1144090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1145090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1146c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      QualType BaseType(Init->getBaseClass(), 0);
1147c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1148c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman                           New->getDeclName());
1149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInit = BuildBaseInitializer(BaseType,
1151090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     (Expr **)NewArgs.data(),
1152090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getSourceLocation(),
1154090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1155090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
11579988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
11589988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson
11599988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
11609988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1161cdc83c777973fa56b6f828bfe88210290ca58d62Anders Carlsson        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit));
11629988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
11639988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember()));
1164090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1165090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1166090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1168090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1169090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1170090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1171090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1172090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1173090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1174090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1175090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
1176090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1177090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1178090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1179090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
1180090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1181090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
1182090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  ActOnMemInitializers(DeclPtrTy::make(New),
1183090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1184090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
1185090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       NewInits.data(), NewInits.size());
1186090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1187090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
118852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
118952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
119052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
119152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
119252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
119352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
119452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
119552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
119652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
119752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
119852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
119952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
120052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
120152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
120252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
120352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
120452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
120552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
120652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
120752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
120852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
120952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
121052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
121152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
121252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
121352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
121452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
121552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
121652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
121752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
121852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
121952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
122052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
122152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
122252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
122352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
122452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
122552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
122652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
122752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
122852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
122952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
123052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
123152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
123252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
123352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
123452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
123552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
123652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
123752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
123852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
123952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
124052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
124152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
12420d8df780aef1acda5962347a32591efc629b6748Anders Carlssonstatic bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
12430d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
12440d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
12450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
12460d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
12470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
124852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
124952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
125052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
125152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
125252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
125352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
125452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
125552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
125652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
125752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
125852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
125952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
126052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
126152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
126252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1263815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
12640d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
12650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
12660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
12670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
12680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
12690d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
1270815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
12710d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
12720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
12730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
127452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
127552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
1276a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
127752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
127852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
1279815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
128052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
128152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
1282815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
12837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
128452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
128552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
128652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
128752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
128852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
1289a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
1290d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1291d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
1292d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
1293d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
1294d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
1295d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
1296d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
1297d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
1298815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
1299815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1300815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1301815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1302815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
1303815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
1304815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
1305815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
1306815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
1307815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
1308815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
1309815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
1310815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1311815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
1312815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1313815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
131402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
131502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
131602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
131702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
131802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDeclContext *Sema::FindInstantiatedContext(DeclContext* DC) {
131902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
132002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl* ID = FindInstantiatedDecl(D);
132102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
132202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
132302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
132402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1325ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
1326ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
1327815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1328815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
1329815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
1330815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
1331815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
1332815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1333815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
1334815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
1335815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
1336815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
1337815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
1338815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
1339815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1340815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
1341815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
1342815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1343815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
1344815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
1345815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
1347815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
1348815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
1349ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1350ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
135144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D) {
135244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
135344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    // Transform all of the elements of the overloaded function set.
135444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    OverloadedFunctionDecl *Result
135544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
135644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor
135744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
135844c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                                                FEnd = Ovl->function_end();
135944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor         F != FEnd; ++F) {
136044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      Result->addOverload(
136144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                  AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F)));
136244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    }
136344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor
136444c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return Result;
136544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  }
136644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor
1367815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
13682bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
13692bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
13702bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
13712bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
13722bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
1373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
137452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
137552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (ClassTemplateDecl *ClassTemplate
137652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall          = Record->getDescribedClassTemplate()) {
137752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // When the declaration D was parsed, it referred to the current
137852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // instantiation. Therefore, look through the current context,
137952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // which contains actual instantiations, to find the
138052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // instantiation of the "current instantiation" that D refers
138152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // to. Alternatively, we could just instantiate the
138252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // injected-class-name with the current template arguments, but
138352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // such an instantiation is far more expensive.
138452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      for (DeclContext *DC = CurContext; !DC->isFileContext();
138552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
138652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall        if (ClassTemplateSpecializationDecl *Spec
138752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
138852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall          if (isInstantiationOf(ClassTemplate, Spec->getSpecializedTemplate()))
138952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
139052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
139152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
139252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      assert(false &&
139352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
139452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
139552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
139602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  ParentDC = FindInstantiatedContext(ParentDC);
139744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  if (!ParentDC)
139844c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
139944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor
1400815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
1401815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
1402815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
1403815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
1404815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
1405815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
140617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
1407815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
1408815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
1409815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
1410815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
1411815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
1412815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1413815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
1414815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
1415815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1416815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
1417815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D,
141817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
141917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
1420815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
142152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1422815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1423815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1424815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1425815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1426815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1427815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1428d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
1429d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// \brief Performs template instantiation for all implicit template
1430d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1431d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1432d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1433d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1434b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
1435d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
14367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
14377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
1438c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
1439c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
1440c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
1441c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
1442c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
14436fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1444b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
14457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
14467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
1447d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
14487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
14497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
14507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
1451c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
1452c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
1453c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
1454c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
1455c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
1456c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
1457c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
14587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1459d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1460d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1461