SemaTemplateInstantiateDecl.cpp revision 52604ab71a74b8ec481255dfeea7dc9dba63b1a5
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
18c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
1983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "llvm/Support/Compiler.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class VISIBILITY_HIDDEN TemplateDeclInstantiator
26b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
29d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
35d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
367e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
39390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
414f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
424f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
486477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
4902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
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);
631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    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()) {
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    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  }
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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);
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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());
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // 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());
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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);
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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  }
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (D->getInit()) {
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    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())) {
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // 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      }
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      // Add the direct initializer to the declaration.
17683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            PLE->getLParenLoc(),
17883ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            Sema::MultiExprArg(SemaRef,
17983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                       (void**)PLE->getExprs(),
18083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                                           PLE->getNumExprs()),
18183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            FakeCommaLocs.data(),
18283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor                                            PLE->getRParenLoc());
1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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);
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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);
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
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(),
2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            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);
245f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field)
246f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
249f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
251f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
252f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
253f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
2548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
256f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
257f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
2608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
26202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
26302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
26402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
26502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
26602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
26702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Type *Ty = D->getFriendType()) {
26802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
26902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                                   D->getLocation(), DeclarationName());
27002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (T.isNull()) return 0;
271fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
27202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
27302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = T.getTypePtr();
274fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
27502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
27602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
27702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
27802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
27902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
28002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *NewND = Visit(ND);
28102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
282c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
28302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
28402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
28702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
28802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
2895fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
29002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
29102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
292fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
293fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
2948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
2958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
297ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
298ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
301ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
3028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
3038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
3048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
30543d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
30643d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
3081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
309b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
310b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
3118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
3128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
3168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
317741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
3188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
3198dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
32006c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
32117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
3228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
3238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3240ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
3258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
32717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
32817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
3298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
3308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
3318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
332ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
333ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
335ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
337ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
338ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
3398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
3418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
3428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
3438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
3448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
3458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
3488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
3498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
3508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
3518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
3538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
3548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
3558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
35917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
360b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
3618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
3628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
3638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
366fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
367c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
368c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
369fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
370fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
3766477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
3776477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
3786477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
3796477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
380e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
381e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
382ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
384d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
385e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
386e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
387e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
388e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
389e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
390e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL);
391e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
392e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
393e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
394e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
395e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
396e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setAccess(D->getAccess());
397e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
398e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
399e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
400e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
401e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
402e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
403d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
404d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
405d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // FIXME: Dig out the out-of-line definition of this function template?
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
410d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // FIXME: Handle instantiation of nested function templates that aren't
413d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // member function templates. This could happen inside a FriendDecl.
414d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(isa<CXXMethodDecl>(D->getTemplatedDecl()));
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXMethodDecl *InstMethod
416d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    = cast_or_null<CXXMethodDecl>(
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()),
418d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                    InstParams));
419d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!InstMethod)
420d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
421d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
423d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
424d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate();
425d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!");
426d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  InstTemplate->setInstantiatedFromMemberTemplate(D);
427d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  Owner->addDecl(InstTemplate);
428d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
429d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
430d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
431d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
432d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
433d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
434d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
435d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
436d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
438741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
439741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
440d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
441eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
442eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
443eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
444eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
445eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
446d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
447d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Record->setInstantiationOfMemberClass(D);
448d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
44902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
45002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
45102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
45202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
45302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
454d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
455d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
45617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
457d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
458d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
459d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
46002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
46102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
46202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
46302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
46402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
465e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
466127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
467127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
468127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
469127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
470127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
471127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
473d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
474d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
475828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
479127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
481127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
482127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
483127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
484127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
486e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
489ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (T.isNull())
4912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
492fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
493e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Build the instantiated method declaration.
49402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext());
49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
4961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
497a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getDeclName(), T, D->getDeclaratorInfo(),
498a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
499a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                           D->isInline(), D->hasWrittenPrototype());
50002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
503e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
504e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
505e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
50602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
50702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
50802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state and add it to the owner.
50902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) {
51002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    bool WasDeclared = (FOK == Decl::FOK_Declared);
51102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Function->setObjectOfFriendDecl(WasDeclared);
51202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!Owner->isDependentContext())
513ab88d97734f1260402a0c6a8f6b77bed7ed4e295John McCall      DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false);
514f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall
515f181d8a44f5837213eeaee6d71f584b1ab2849cdJohn McCall    Function->setInstantiationOfMemberFunction(D);
51602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
518e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
519e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
522e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
523e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  NamedDecl *PrevDecl = 0;
524e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration,
525e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
526e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
527127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (FunctionTemplate) {
528127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // Record this function template specialization.
529127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    Function->setFunctionTemplateSpecialization(SemaRef.Context,
530127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                FunctionTemplate,
531d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                                &TemplateArgs.getInnermost(),
532127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                InsertPos);
533fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  }
534fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
535e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
536e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
5372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
538d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
539d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
540d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
5416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
5426b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
543d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
546d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
5476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
549d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
550d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
5516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
5556b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
5586b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
5596b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
5606b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
5616b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
56248dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
56348dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
5640ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
565ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType T = SubstFunctionType(D, Params);
5662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T.isNull())
5672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
5682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
5692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
5702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
571dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
573dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
57417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
575dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
576dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
577dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
58117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->getDeclaratorInfo(),
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
58317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                        Constructor->isInline(), false);
58417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
58517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
58617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
58717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
58817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
58917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
59017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       T, Destructor->isInline(), false);
59165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
59365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
59465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                      T->getAsFunctionType()->getResultType());
59565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
59665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
59765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
59865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
59965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       T, Conversion->getDeclaratorInfo(),
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                       Conversion->isInline(),
60165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
602dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
604dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->getDeclName(), T, D->getDeclaratorInfo(),
605dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                   D->isStatic(), D->isInline());
606dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
6076b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
617d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
618d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
619d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
620d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
621d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
622d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
623d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
625d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
626d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
628d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
629d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  } else if (!FunctionTemplate)
6306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setInstantiationOfMemberFunction(D);
6312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
6337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
6347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
6357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
6367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
6395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
6405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
641beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  Method->setParams(SemaRef.Context, Params.data(), Params.size());
6425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
6435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
6445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
6452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
646dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  NamedDecl *PrevDecl = 0;
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
6491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrevDecl = SemaRef.LookupQualifiedName(Owner, Name,
650dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                           Sema::LookupOrdinaryName, true);
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
652dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
653dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
654dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
655dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
656dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
657dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor      PrevDecl = 0;
658dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
6592dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
660d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams)
6616b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // Record this function template specialization.
6626b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
6636b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              FunctionTemplate,
664d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                              &TemplateArgs.getInnermost(),
6656b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                              InsertPos);
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
66865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
66965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration,
67065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
67165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
67265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl))
673dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
6762dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
6772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
678615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
679dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
680615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
681615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
68203b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
68317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
68403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
68503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
686bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
68765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
688bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
689bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
6906477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
691ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs,
6927e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                                           D->getLocation(), D->getDeclName());
6932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (OrigT.isNull())
6942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
6952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType T = SemaRef.adjustParameterType(OrigT);
6972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
6982dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
6992dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  ParmVarDecl *Param = 0;
7002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  if (T == OrigT)
7012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
702a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getIdentifier(), T, D->getDeclaratorInfo(),
703a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                D->getStorageClass(), 0);
7042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  else
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner,
7062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                        D->getLocation(), D->getIdentifier(),
707a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        T, D->getDeclaratorInfo(), OrigT,
708a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                        D->getStorageClass(), 0);
7092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7109351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
7119351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  if (Expr *Arg = D->getDefaultArg())
7129351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson    Param->setUninstantiatedDefaultArg(Arg);
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
7152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
7162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
71748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
7182dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
7192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
7202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
7212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorDecl *
7222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorTemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
7232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Since parameter types can decay either before or after
7242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // instantiation, we simply treat OriginalParmVarDecls as
7252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // ParmVarDecls the same way, and create one or the other depending
7262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // on what happens after template instantiation.
7272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return VisitParmVarDecl(D);
7282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
7292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
731e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
735e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
7361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
739e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getDepth(), TTPT->getIndex(),
740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
744e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (D->hasDefaultArgument()) {
745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultPattern = D->getDefaultArgument();
746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    QualType DefaultInst
747ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstType(DefaultPattern, TemplateArgs,
748ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDefaultArgumentLoc(),
749ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                          D->getDeclName());
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Inst->setDefaultArgument(DefaultInst,
752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->getDefaultArgumentLoc(),
753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             D->defaultArgumentWasInherited() /* preserve? */);
754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
755e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
756e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
757e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
7590dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
7600dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonTemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) {
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
7640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
7650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
7660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7680dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
7690dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
7700dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.BuildUsingDeclaration(D->getLocation(), SS,
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  D->getTargetNameLocation(),
7750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                  D->getTargetName(), 0, D->isTypeName());
7760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
7771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
7780d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                           D);
7790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
7800dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
7810dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
782ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
783d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
7847e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
7858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
7868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
791e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
792e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
795ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
797e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
800e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  typedef llvm::SmallVector<Decl*,8> ParamVector;
801e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
802e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
803e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
804e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
805e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *D = Visit(*PI);
806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Invalid = Invalid || !D;
808e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
812e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
813e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
814e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
815e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
816e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
817e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
818e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
819e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
820e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
821e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
822e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
823e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
825e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
826ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \brief Does substitution on the type of the given function, including
827ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// all of the function parameters.
8285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
829ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \param D The function whose type will be the basis of the substitution
8305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
8315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \param Params the instantiated parameter declarations
8325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
833ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall/// \returns the instantiated function's type if successful, a NULL
8345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// type if there was an error.
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
836ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
8375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
8385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  bool InvalidDecl = false;
8395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
840ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Substitute all of the function's formal parameter types.
8417e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
8420ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTys;
8431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionDecl::param_iterator P = D->param_begin(),
8445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                 PEnd = D->param_end();
8455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor       P != PEnd; ++P) {
8466477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
8475545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->getType()->isVoidType()) {
8485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
8495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
851ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                PInst->getType(),
852ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                diag::err_abstract_type_in_decl,
853ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump                                                Sema::AbstractParamType))
8545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
8555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      Params.push_back(PInst);
8575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      ParamTys.push_back(PInst->getType());
8585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      if (PInst->isInvalidDecl())
8605545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        InvalidDecl = true;
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else
8625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      InvalidDecl = true;
8635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
8645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: Deallocate dead declarations.
8665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InvalidDecl)
8675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8685545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType();
8705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  assert(Proto && "Missing prototype?");
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualType ResultType
872ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
873ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                        D->getLocation(), D->getDeclName());
8745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (ResultType.isNull())
8755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    return QualType();
8765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
877beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
8785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
8795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                   D->getLocation(), D->getDeclName());
8805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
8815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
883e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
884e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
885e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
888e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
889e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
890e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
892cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
893cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
894cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
897cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
898cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
899cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
900cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
901cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
902cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
903cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
905cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
907cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
908bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
909cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
910cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
911cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
912cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
914e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
915e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
916e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
9175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
9185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
9195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
9205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
9215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
9245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
925e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
926e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
9295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
93077b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson  if (Tmpl->isVirtualAsWritten()) {
93177b7f1d4fb782c9152f91b76f9f8b1d1af21bd35Anders Carlsson    New->setVirtualAsWritten(true);
9325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAggregate(false);
9335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPOD(false);
9341d954f6a0057cb55a3a5d483904a3c57d03c996fEli Friedman    Record->setEmpty(false);
9355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setPolymorphic(true);
9365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
9375545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (Tmpl->isPure()) {
9385545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    New->setPure();
9395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Record->setAbstract(true);
9405545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  }
9415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
9425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
9435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
9445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
9455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
946a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
947a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
948a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
949a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
950b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
951b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
952b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
953b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
954a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
955b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
956b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
957b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
958b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
959b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
960f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
961b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
962b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         bool Recursive) {
96354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
96454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
96554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
9666fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9681eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
9691637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor  const FunctionDecl *PatternDecl = 0;
9705ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor  if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) {
9715ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor    while (Primary->getInstantiatedFromMemberTemplate())
9725ec178ff237d77e7acf5e747c19bf4f2de77a779Douglas Gregor      Primary = Primary->getInstantiatedFromMemberTemplate();
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9741637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Primary->getTemplatedDecl();
9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
9761637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor    PatternDecl = Function->getInstantiatedFromMemberFunction();
9771eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
9781eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
9796fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
9801eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
9811eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (!Pattern)
9821eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
9831eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
984d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
985d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
987d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
9881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
989d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
990d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor      PatternDecl->isOutOfLine() && !PatternDecl->isInline())
991d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
993f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
994f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
995f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
996b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
997b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
998b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
999b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1000b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1001b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1002b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1004e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1005e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
100654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
100754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // recorded.
100854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  LocalInstantiationScope Scope(*this);
10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
101154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
101254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
101354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
101454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
101554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1016b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1017b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1018b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1019b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1020b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
10211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1022090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1023090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1024090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1026090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1027090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1028090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
10301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1032090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1033e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
103452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
103552604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
103652604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1038e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1039b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1040b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1041aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1042aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1043aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1045b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1046b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1048b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1050b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1051b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1052b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1053a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1054a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1055a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1056a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1057a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
10587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
10597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
10607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
10617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
10627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
10637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
10647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
10657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
10667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
10677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
10687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
10697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
10707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 bool Recursive) {
10717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
10727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
10757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // FIXME: Do we have to look for specializations separately?
10767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
10777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  bool FoundOutOfLineDef = false;
10787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
10791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(Def->isStaticDataMember() && "Not a static data member?");
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (VarDecl::redecl_iterator RD = Def->redecls_begin(),
10817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                             RDEnd = Def->redecls_end();
10827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor       RD != RDEnd; ++RD) {
10837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (RD->getLexicalDeclContext()->isFileContext()) {
10847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      Def = *RD;
10857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      FoundOutOfLineDef = true;
10867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
10877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (!FoundOutOfLineDef) {
10907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
10917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
10947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
10957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
10967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1097d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // FIXME: extern templates
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
11007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
11017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
11047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
11057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
11067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
11077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
11087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
11117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
11127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
11137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1115ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
11167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
11197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
11207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
11217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
11227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
11237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
11267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
11287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
11317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1133a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1134815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1135090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
1136090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
1138090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
1141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
1143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
114472f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
114572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
1146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
1147090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1148090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    // Instantiate all the arguments.
1151090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1152090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson         Args != ArgsEnd; ++Args) {
1153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1154090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1155090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (NewArg.isInvalid())
1156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        New->setInvalidDecl();
1157090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      else
1158090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        NewArgs.push_back(NewArg.takeAs<Expr>());
1159090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1160090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1161090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
1162090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1163090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
1164c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      QualType BaseType(Init->getBaseClass(), 0);
1165c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman      BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1166c5573a81a83c4173c92c7e91b01371b7223d88c4Eli Friedman                           New->getDeclName());
1167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1168090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInit = BuildBaseInitializer(BaseType,
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
1170090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
1171090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getSourceLocation(),
1172090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
1173090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
1174090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
11759988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11779988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
11789988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
1179cdc83c777973fa56b6f828bfe88210290ca58d62Anders Carlsson        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit));
11809988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
11819988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember()));
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
1184090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
1185090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
1186090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
1187090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1188090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1189090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (NewInit.isInvalid())
1190090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
1191090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    else {
1192090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
1193090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1195090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
1196090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
1197090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1199090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
1201090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
1202090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       NewInits.data(), NewInits.size());
1204090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
1205090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
120652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
120752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
120852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
120952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
121052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
121152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
121252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
121352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
121452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
121552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
121652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
121752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
121852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
121952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
122052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
122152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
122252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
122352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
122452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
122552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
122652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
122752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
122852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
122952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
123052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
123152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
123252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
123352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
123452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
123552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
123652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
123752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
123852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
123952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
124052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
124152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
124252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
124352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
124452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
124552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
124652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
124752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
124852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
124952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
125052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
125152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
125252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
125352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
125452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
125552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
125652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
125752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
125852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
125952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
12600d8df780aef1acda5962347a32591efc629b6748Anders Carlssonstatic bool isInstantiationOf(UnresolvedUsingDecl *Pattern,
12610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
12620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
12630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
12640d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
12650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
126652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
126752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
126852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
126952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
127052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
127152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
127252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
127352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
127452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
127552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
127652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
127752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
127852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
127952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
128052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
1281815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
12820d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
12830d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) {
12840d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
12850d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
12860d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
12870d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
1288815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
12890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
12900d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
129352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
129652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
1297815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
129852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
129952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
1300815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
13017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
130252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
130352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
130452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
130552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
130652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
1307a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
1308d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1309d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
1310d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
1312d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
1313d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
1314d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1316815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
1317815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1318815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1319815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1320815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
1322815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
1323815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
1324815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
1325815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
1326815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
1327815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
1328815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1329815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
1330815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1331815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
133202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
133302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
133402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
133502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
133602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDeclContext *Sema::FindInstantiatedContext(DeclContext* DC) {
133702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
133802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl* ID = FindInstantiatedDecl(D);
133902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
134002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
134102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
134202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1343ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
1344ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
1345815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
1347815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
1348815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
1349815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
1350815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1351815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
1352815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
1353815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
1354815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
1355815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
1356815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
1357815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1358815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
1359815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
1360815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1361815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
1362815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
1363815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
1364815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
1365815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
1366815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
1367ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1368ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
136944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D) {
137044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor  if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
137144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    // Transform all of the elements of the overloaded function set.
13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    OverloadedFunctionDecl *Result
137344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
13741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
137544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
137644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                                                FEnd = Ovl->function_end();
137744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor         F != FEnd; ++F) {
137844c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor      Result->addOverload(
137944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor                  AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F)));
138044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    }
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return Result;
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
13841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1385815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
13862bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) {
13872bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
13882bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
13892bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
13902bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
1391815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
139252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ClassTemplateDecl *ClassTemplate
139452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall          = Record->getDescribedClassTemplate()) {
139552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // When the declaration D was parsed, it referred to the current
139652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // instantiation. Therefore, look through the current context,
139752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // which contains actual instantiations, to find the
139852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // instantiation of the "current instantiation" that D refers
139952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // to. Alternatively, we could just instantiate the
140052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // injected-class-name with the current template arguments, but
140152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      // such an instantiation is far more expensive.
14021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
140352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
140552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
140652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall          if (isInstantiationOf(ClassTemplate, Spec->getSpecializedTemplate()))
140752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
140852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
140952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(false &&
141152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
141252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
141352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
141402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  ParentDC = FindInstantiatedContext(ParentDC);
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
141644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1418815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
1419815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
1420815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
1421815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
1422815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
1423815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
142417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
1425815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
1426815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
1427815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
1428815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
1429815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
1430815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1431815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
1432815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
1433815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
1434815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
14351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
143617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
143717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
1438815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1440815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    assert(Result && "Unable to find instantiation of declaration!");
1441815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
1442815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
1443815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
1444815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
1445815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
1446d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
1448d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
1449d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregorvoid Sema::PerformPendingImplicitInstantiations() {
1450d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  while (!PendingImplicitInstantiations.empty()) {
1451d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor    PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
1452b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.pop_front();
14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
14557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
1457c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
1458c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
1459c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14616fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
1462b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
14637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
14647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
14677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
14687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
1469c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
1471c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
1472c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
1473c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
1474c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
1477d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
1478d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
1479