SemaTemplateInstantiateDecl.cpp revision 60406bede202b66ebdd98cac0c38d20f9698aeca
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//===----------------------------------------------------------------------===/
12e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "Sema.h"
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "Lookup.h"
147cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/ASTConsumer.h"
15aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTContext.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
190c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/ExprCXX.h"
208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/Basic/PrettyStackTrace.h"
21a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/Lex/Preprocessor.h"
2221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall
23c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlssonusing namespace clang;
2483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  class TemplateDeclInstantiator
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
2985b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramer    DeclContext *Owner;
30b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    const MultiLevelTemplateArgumentList &TemplateArgs;
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    void InstantiateAttrs(Decl *Tmpl, Decl *New);
33d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  public:
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
37d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
387e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump
41390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
429a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    // declarations with automatically-generated ones from
434f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    // clang/AST/DeclNodes.def.
444f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
453dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceDecl(NamespaceDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
486206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    Decl *VisitFieldDecl(FieldDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
526477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitFriendDecl(FriendDecl *D);
5302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFunctionDecl(FunctionDecl *D,
54a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
55a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
56d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
57d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
58d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
59615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
6003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
61bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
626477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
63e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplatePartialSpecializationDecl(
647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
657974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
66d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
67e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
6833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
699106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
7048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDecl(UsingDecl *D);
71ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
72ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Base case. FIXME: Remove once we can instantiate everything.
768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitDecl(Decl *D) {
7748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
7848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      return 0;
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    const LangOptions &getLangOptions() {
875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor      return SemaRef.getLangOptions();
8821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    }
895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
90e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // Helper functions for instantiating methods.
915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    QualType SubstFunctionType(FunctionDecl *D,
92e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
93e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
94ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
95b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
96b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    TemplateParameterList *
97b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      SubstTemplateParams(TemplateParameterList *List);
98b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
99b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool InstantiateClassTemplatePartialSpecialization(
100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// FIXME: Is this too simple?
1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
108b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall       TmplAttr = TmplAttr->getNext()) {
109b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
110b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // FIXME: Is cloning correct for all attributes?
111b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
112b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    New->addAttr(NewAttr);
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallDecl *
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  assert(false && "Translation units cannot be instantiated");
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return D;
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallDecl *
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  assert(false && "Namespaces cannot be instantiated");
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return D;
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  bool Invalid = false;
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (DI->getType()->isDependentType()) {
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                           D->getLocation(), D->getDeclName());
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (!DI) {
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      Invalid = true;
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1394ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  }
1401d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall
1411d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  // Create the new typedef
142cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  TypedefDecl *Typedef
143cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
144cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                          D->getIdentifier(), DI);
1454ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  if (Invalid)
1464ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    Typedef->setInvalidDecl();
147cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
1484ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1491d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(Prev, TemplateArgs);
1504ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1514ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  }
152cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
15360d7b3a319d84d688752be3870615ac0f111fb16John McCall  Owner->addDecl(Typedef);
154cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
155cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  return Typedef;
156cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt}
157cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt
158cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt/// \brief Instantiate the arguments provided as part of initialization.
159cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt///
160cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt/// \returns true if an error occurred, false otherwise.
161cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Huntstatic bool InstantiateInitializationArguments(Sema &SemaRef,
162cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                               Expr **Args, unsigned NumArgs,
163cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                           const MultiLevelTemplateArgumentList &TemplateArgs,
164cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
165cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
1664ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth  for (unsigned I = 0; I != NumArgs; ++I) {
1674ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // When we hit the first defaulted argument, break out of the loop:
1684ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // we don't pass those default arguments on.
1694ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (Args[I]->isDefaultArgument())
170d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson      break;
1711d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall
172d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
173d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    if (Arg.isInvalid())
174d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson      return true;
175d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1764f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
1774f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    InitArgs.push_back(Arg.release());
1784f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1794f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
1804f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    FakeCommaLocs.push_back(
1814f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
1824f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  }
1834f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1844f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return false;
1854f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1864f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1874f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
1883dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  // Do substitution on the type of the declaration
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                         TemplateArgs,
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                         D->getTypeSpecStartLoc(),
1923dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                         D->getDeclName());
1933dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  if (!DI)
1943dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    return 0;
1953dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1963dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  // Build the instantiated declaration
1973dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
1983dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getLocation(), D->getIdentifier(),
1993dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 DI->getType(), DI,
2003dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getStorageClass());
2013dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Var->setThreadSpecified(D->isThreadSpecified());
2023dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
205a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  // If we are instantiating a static data member defined
206836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  // out-of-line, the instantiation will have the same lexical
207836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  // context (which will be a namespace scope) as the template.
208ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (D->isOutOfLine())
209ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    Var->setLexicalDeclContext(D->getLexicalDeclContext());
210ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall
2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // FIXME: In theory, we could have a previous declaration for variables that
212a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  // are not static data members.
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Redeclaration = false;
214b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  // FIXME: having to fake up a LookupResult is dumb.
215b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                        Sema::LookupOrdinaryName);
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (D->isOutOfLine()) {
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
221ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    Owner->makeDeclVisibleInContext(Var);
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  } else {
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Owner->addDecl(Var);
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
225d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
226d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  // Link instantiations of static data members back to the template from
227d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  // which they were instantiated.
228d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (Var->isStaticDataMember())
229d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
230d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor                                                     TSK_ImplicitInstantiation);
231d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
232d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (D->getInit()) {
233d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
2345126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
2357c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    else
2367c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
2375126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2385126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    // Extract the initializer, skipping through any temporary-binding
2395126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    // expressions and look at the subexpression as it was written.
2401d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Expr *DInit = D->getInit();
241d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(DInit))
24246460a68f6508775e98c19b4bb8454bb471aac24John McCall      DInit = Binder->getSubExpr();
24317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(DInit))
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      DInit = ICE->getSubExprAsWritten();
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (ParenListExpr *PLE = dyn_cast<ParenListExpr>(DInit)) {
2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // The initializer is a parenthesized list of expressions that is
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // type-dependent. Instantiate each of the expressions; we'll be
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // performing direct initialization with them.
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getExprs(),
2546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getNumExprs(),
255ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                                              TemplateArgs,
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
2586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              PLE->getLParenLoc(),
2606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
2616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
26260d7b3a319d84d688752be3870615ac0f111fb16John McCall                                              PLE->getRParenLoc());
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      }
2646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else if (CXXConstructExpr *Construct =dyn_cast<CXXConstructExpr>(DInit)) {
2656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // The initializer resolved to a constructor. Instantiate the constructor
2666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      // arguments.
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      llvm::SmallVector<SourceLocation, 4> CommaLocs;
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      if (!InstantiateInitializationArguments(SemaRef,
2716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getArgs(),
2726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              Construct->getNumArgs(),
2736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              TemplateArgs,
2746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs, InitArgs)) {
2756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        if (D->hasCXXDirectInitializer()) {
2766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor          SourceLocation FakeLParenLoc =
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor            SemaRef.PP.getLocForEndOfToken(D->getLocation());
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          SourceLocation FakeRParenLoc = CommaLocs.empty()? FakeLParenLoc
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                                          : CommaLocs.back();
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                                FakeLParenLoc,
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                                move_arg(InitArgs),
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                                CommaLocs.data(),
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                                FakeRParenLoc);
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        } else if (InitArgs.size() == 1) {
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          Expr *Init = (Expr*)(InitArgs.take()[0]);
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                       SemaRef.Owned(Init),
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                       false);
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        } else {
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          assert(InitArgs.size() == 0);
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor          SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        }
294ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall      }
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    } else {
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      OwningExprResult Init
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not happy about invalidating decls just because of a bad
3006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // initializer, unless it affects the type.
3016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (Init.isInvalid())
3026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Var->setInvalidDecl();
3036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      else
3046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
3056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     D->hasCXXDirectInitializer());
3066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    }
3076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
3096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
3106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
3116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return Var;
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  bool Invalid = false;
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  TypeSourceInfo *DI = D->getTypeSourceInfo();
3186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (DI->getType()->isDependentType())  {
3196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs,
3206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                           D->getLocation(), D->getDeclName());
3216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!DI) {
32228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      DI = D->getTypeSourceInfo();
32328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      Invalid = true;
32428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    } else if (DI->getType()->isFunctionType()) {
32528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // C++ [temp.arg.type]p3:
32628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      //   If a declaration acquires a function type through a type
32728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      //   dependent on a template-parameter and this causes a
32828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      //   declaration that does not use the syntactic form of a
32928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      //   function declarator to have function type, the program is
33028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      //   ill-formed.
33128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
33228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        << DI->getType();
33328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      Invalid = true;
33428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3366b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
33760d7b3a319d84d688752be3870615ac0f111fb16John McCall  Expr *BitWidth = D->getBitWidth();
3386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Invalid)
3396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    BitWidth = 0;
3406b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  else if (BitWidth) {
3416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // The bit-width expression is not potentially evaluated.
3426b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
3436b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    OwningExprResult InstantiatedBitWidth
3453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3469901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3479901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      Invalid = true;
3489901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      BitWidth = 0;
3499901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    } else
3509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
3529901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
353ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
354a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                            DI->getType(), DI,
3550a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                            cast<RecordDecl>(Owner),
3560a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                            D->getLocation(),
3570a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                            D->isMutable(),
3580a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                            BitWidth,
3593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                            D->getTypeSpecStartLoc(),
3603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                            D->getAccess(),
361b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor                                            0);
3623d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  if (!Field) {
3633d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
3640a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall    return 0;
36516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  }
36616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor
3673d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  InstantiateAttrs(D, Field);
3683d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Invalid)
370b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    Field->setInvalidDecl();
371b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
372b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!Field->getDeclName()) {
373b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Keep track of where this decl came from.
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
3757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  Field->setImplicit(D->isImplicit());
3787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  Owner->addDecl(Field);
3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38046460a68f6508775e98c19b4bb8454bb471aac24John McCall  return Field;
381c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor}
382c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
383c070cc602d6eefea881f71a60de09e05b54c3fddDouglas GregorDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
3844469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  FriendDecl::FriendUnion FU;
385390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump
386390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // Handle friend type expressions by simply substituting template
3873d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  // parameters into the pattern type.
3886826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  if (Type *Ty = D->getFriendType()) {
3896826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
390449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                                   D->getLocation(), DeclarationName());
39160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    if (T.isNull()) return 0;
39260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor
3936826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    assert(getLangOptions().CPlusPlus0x || T->isRecordType());
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FU = T.getTypePtr();
3957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
396ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara  // Handle everything else by appropriate substitution.
397ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara  } else {
3987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    NamedDecl *ND = D->getFriendDecl();
3997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(ND && "friend decl must be a decl or a type!");
4007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
401f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
402f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    // will inject the thing that the friend references into the current
4037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // owner, which is wrong.
4041d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Decl *NewND;
4058dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
406251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    // Hack to make this work almost well pending a rewrite.
407251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    if (ND->getDeclContext()->isRecord())
408251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor      NewND = SemaRef.FindInstantiatedDecl(ND, TemplateArgs);
409251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    else if (D->wasSpecialization()) {
410cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor      // Totally egregious hack to work around PR5866
411251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor      return 0;
41260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    } else
41360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor      NewND = Visit(ND);
41460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    if (!NewND) return 0;
4151f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4161f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    FU = cast<NamedDecl>(NewND);
4171f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  }
4181f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4191f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor  FriendDecl *FD =
4206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
4216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                       D->getFriendLoc());
4226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  FD->setAccess(AS_public);
423ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  Owner->addDecl(FD);
4246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return FD;
4256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
4266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
4276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
429d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall
4306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  // The expression in a static assertion is not potentially evaluated.
4316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  OwningExprResult InstantiatedAssertExpr
4346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
4359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  if (InstantiatedAssertExpr.isInvalid())
4369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall    return 0;
4376b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
4386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
439d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  D->getMessage()->Retain();
44083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor  Decl *StaticAssert
4416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
4426b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                           move(InstantiatedAssertExpr),
4436b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                           move(Message)).getAs<Decl>();
4446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return StaticAssert;
4456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
4466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4471f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
44865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
449d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                    D->getLocation(), D->getIdentifier(),
4503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                    D->getTagKeywordLoc(),
4515764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor                                    /*PrevDecl=*/0);
4525764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
4535764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  Enum->setAccess(D->getAccess());
454bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  Owner->addDecl(Enum);
4553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Enum->startDefinition();
4563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
4586206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4596206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  EnumConstantDecl *LastEnumConst = 0;
4606206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
4616206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara         ECEnd = D->enumerator_end();
4626206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara       EC != ECEnd; ++EC) {
4636206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    // The specified value for the enumerator.
4646206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    OwningExprResult Value = SemaRef.Owned((Expr *)0);
4656206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    if (Expr *UninstValue = EC->getInitExpr()) {
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // The enumerator's value expression is not potentially evaluated.
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      EnterExpressionEvaluationContext Unevaluated(SemaRef,
468a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                                   Action::Unevaluated);
469836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor
470836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
47107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    }
47207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall
47307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    // Drop the initial value and continue.
474a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    bool isInvalid = false;
47507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (Value.isInvalid()) {
47607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Value = SemaRef.Owned((Expr *)0);
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    EnumConstantDecl *EnumConst
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
48407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
487b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor        EnumConst->setInvalidDecl();
488b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor      Enum->setInvalidDecl();
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
4928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->addDecl(EnumConst);
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
495ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
496ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  }
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49860d7b3a319d84d688752be3870615ac0f111fb16John McCall  // FIXME: Fixup LBraceLoc and RBraceLoc
499ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
5018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                        Sema::DeclPtrTy::make(Enum),
5028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                        &Enumerators[0], Enumerators.size(),
5038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                        0, 0);
504e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson
5058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
50707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall
50807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCallDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
5108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return 0;
5118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
513ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroffnamespace {
5148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  class SortDeclByLocation {
5158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    SourceManager &SourceMgr;
516663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor
517663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  public:
518f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    explicit SortDeclByLocation(SourceManager &SourceMgr)
519663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor      : SourceMgr(SourceMgr) { }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5211d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    bool operator()(const Decl *X, const Decl *Y) const {
522d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
523f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson                                                 Y->getLocation());
524f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    }
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  };
526f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson}
527f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson
528f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders CarlssonDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
5299901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Create a local instantiation scope for this class template, which
5309901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // will contain the instantiations of the template parameters.
5319901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
5329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
5339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
5348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (!InstParams)
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NULL;
536f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson
53746460a68f6508775e98c19b4bb8454bb471aac24John McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
538f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  CXXRecordDecl *RecordInst
5398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
5408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                            Pattern->getLocation(), Pattern->getIdentifier(),
5418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                            Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
5428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                            /*DelayTypeCreation=*/true);
54302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
54402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  ClassTemplateDecl *Inst
54506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
54632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                                D->getIdentifier(), InstParams, RecordInst, 0);
54732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
54832f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (D->getFriendObjectKind())
54932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    Inst->setObjectOfFriendDecl(true);
55006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  else
55106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Inst->setAccess(D->getAccess());
552fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  Inst->setInstantiatedFromMemberTemplate(D);
55306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
55406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // Trigger creation of the type for the instantiation.
55506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  SemaRef.Context.getTypeDeclType(RecordInst);
55606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
55706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // Finish handling of friends.
55806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (Inst->getFriendObjectKind()) {
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return Inst;
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
56106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
56206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  Owner->addDecl(Inst);
56306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
56432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  // First, we sort the partial specializations by location, so
565af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // that we instantiate them in the order they were declared.
566af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
567af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
568af2094e7cecadf36667deb61a83587ffdd979bd3John McCall         P = D->getPartialSpecializations().begin(),
569af2094e7cecadf36667deb61a83587ffdd979bd3John McCall         PEnd = D->getPartialSpecializations().end();
57006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor       P != PEnd; ++P)
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PartialSpecs.push_back(&*P);
57202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
57306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
57406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
5755fee110ac106370f75592df024001de73edced2aJohn McCall  // Instantiate all of the partial specializations of this member class
57602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // template.
57702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
578fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
579fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Inst;
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
583ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas GregorDecl *
584ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                   ClassTemplatePartialSpecializationDecl *D) {
58660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
587ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // of the class template and return that.
5908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  DeclContext::lookup_result Found
59160d7b3a319d84d688752be3870615ac0f111fb16John McCall    = Owner->lookup(ClassTemplate->getDeclName());
59243d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  if (Found.first == Found.second)
593d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
5949ae2f076ca5ab1feb3ba95629099ec2319833701John McCall
5959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ClassTemplateDecl *InstClassTemplate
5968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (!InstClassTemplate)
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
601741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
6028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
6038dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
60406c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor       P != PEnd; ++P) {
605b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
60617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      return &*P;
6078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
60996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  return 0;
61096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor}
61196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
612d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *
6138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
6148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create a local instantiation scope for this function template, which
61517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  // will contain the instantiations of the template parameters and then get
61617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  // merged with the local instantiation scope for the function template
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // itself.
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
61960d7b3a319d84d688752be3870615ac0f111fb16John McCall
620ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
621ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
623ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    return NULL;
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  FunctionDecl *Instantiated = 0;
626ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
6288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                                                 InstParams));
6298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else
6308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
6318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                                          D->getTemplatedDecl(),
6328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                                                InstParams));
6338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (!Instantiated)
6351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return 0;
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Link the instantiated function template declaration to the function
6389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  // template from which it was instantiated.
6398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  FunctionTemplateDecl *InstTemplate
6408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = Instantiated->getDescribedFunctionTemplate();
6418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  InstTemplate->setAccess(D->getAccess());
6428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  assert(InstTemplate &&
6438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
6448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Link the instantiation back to the pattern *unless* this is a
6468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // non-definition friend declaration.
6473b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
64817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      !(InstTemplate->getFriendObjectKind() &&
649d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        !D->getTemplatedDecl()->isThisDeclarationADefinition()))
6508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
65196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
65296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  // Add non-friends into the owner.
65396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (!InstTemplate->getFriendObjectKind())
65496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    Owner->addDecl(InstTemplate);
65596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  return InstTemplate;
65696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor}
6578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXRecordDecl *PrevDecl = 0;
660c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  if (D->isInjectedClassName())
661fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan    PrevDecl = cast<CXXRecordDecl>(Owner);
662c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  else if (D->getPreviousDeclaration()) {
663d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getPreviousDeclaration(),
664de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                                                   TemplateArgs);
665fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan    if (!Prev) return 0;
6668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    PrevDecl = cast<CXXRecordDecl>(Prev);
6678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  CXXRecordDecl *Record
6706477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
6716477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor                            D->getLocation(), D->getIdentifier(),
6726477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
6736477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  Record->setImplicit(D->isImplicit());
6746477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  // FIXME: Check against AS_none is an ugly hack to work around the issue that
675e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // the tag decls introduced by friend class declarations don't have an access
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specifier. Remove once this area of the code gets sorted out.
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (D->getAccess() != AS_none)
678550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor    Record->setAccess(D->getAccess());
679550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (!D->isInjectedClassName())
6802a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
681e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
682ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // If the original function was part of a friend declaration,
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // inherit its namespace state.
684d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
685e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
686e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  Owner->addDecl(Record);
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  return Record;
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall}
69293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall/// Normal class members are of more specific types and therefore
69493ba8579c341d5329175f1413cdc3b35a36592d2John McCall/// don't make it here.  This function serves two purposes:
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall///   1) instantiating function templates
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall///   2) substituting friend declarations
69793ba8579c341d5329175f1413cdc3b35a36592d2John McCall/// FIXME: preserve function definitions in case #2
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCallDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                       TemplateParameterList *TemplateParams) {
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Check whether there is already a function template specialization for
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // this declaration.
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  void *InsertPos = 0;
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (FunctionTemplate && !TemplateParams) {
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    llvm::FoldingSetNodeID ID;
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    FunctionTemplateSpecializationInfo::Profile(ID,
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                             TemplateArgs.getInnermost().getFlatArgumentList(),
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                       TemplateArgs.getInnermost().flat_size(),
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                SemaRef.Context);
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    FunctionTemplateSpecializationInfo *Info
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                                   InsertPos);
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // If we already have a function template specialization, return it.
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Info)
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return Info->Function;
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool MergeWithParentScope = (TemplateParams != 0) ||
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    !(isa<Decl>(Owner) &&
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  llvm::SmallVector<ParmVarDecl *, 4> Params;
72693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  QualType T = SubstFunctionType(D, Params);
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (T.isNull())
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    return 0;
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Build the instantiated method declaration.
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                    TemplateArgs);
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  FunctionDecl *Function =
7341eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
7351eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor                           D->getDeclName(), T, D->getTypeSourceInfo(),
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                           D->getStorageClass(),
73793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                           D->isInlineSpecified(), D->hasWrittenPrototype());
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  Function->setLexicalDeclContext(Owner);
739c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Attach the parameters
741c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
742c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    Params[P]->setOwningFunction(Function);
743c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  Function->setParams(SemaRef.Context, Params.data(), Params.size());
744c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
745c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  if (TemplateParams) {
746c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // Our resulting instantiation is actually a function template, since we
747c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // are substituting only the outer template parameters. For example, given
748c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //
749c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //   template<typename T>
750c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //   struct X {
751c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //     template<typename U> friend void f(T, U);
752c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //   };
753c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //
754c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //   X<int> x;
755c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    //
756c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
757c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
758c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // template.
759c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // Build the function template itself.
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                    Function->getLocation(),
762c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                    Function->getDeclName(),
763c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                    TemplateParams, Function);
764c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
765c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
766c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  } else if (FunctionTemplate) {
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Record this function template specialization.
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Function->setFunctionTemplateSpecialization(SemaRef.Context,
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                FunctionTemplate,
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                &TemplateArgs.getInnermost(),
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                InsertPos);
772c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  }
773c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
774c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  if (InitFunctionInstantiation(Function, D))
775c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    Function->setInvalidDecl();
776c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
777c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  bool Redeclaration = false;
778c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  bool OverloadableAttrRequired = false;
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
783c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
784c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    // Look only into the namespace where the friend would be declared to
78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // find a previous declaration. This is the innermost enclosing namespace,
78693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // as described in ActOnFriendFunctionDecl.
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(Previous, DC);
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // In C++, the previous declaration we find might be a tag type
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    // (class or enum). In this case, the new declaration will hide the
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // tag type. Note that this does does not apply if we're declaring a
792e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    // typedef (C++ [dcl.typedef]p4).
79393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Previous.isSingleTagDecl())
794f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor      Previous.clear();
795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
79793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
798b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                   false, Redeclaration,
799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                   /*FIXME:*/OverloadableAttrRequired);
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If the original function was part of a friend declaration,
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // inherit its namespace state and add it to the owner.
803e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  NamedDecl *FromFriendD
804ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (FromFriendD->getFriendObjectKind()) {
806ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    NamedDecl *ToFriendD = 0;
807ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    NamedDecl *PrevDecl;
808ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (TemplateParams) {
809ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      ToFriendD = cast<NamedDecl>(FunctionTemplate);
810ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      PrevDecl = FunctionTemplate->getPreviousDeclaration();
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      ToFriendD = Function;
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevDecl = Function->getPreviousDeclaration();
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
815e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Owner->isDependentContext() && !PrevDecl)
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
818f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
819f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor    if (!TemplateParams)
8203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
82124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  }
822ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
823259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  return Function;
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall}
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
826e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas GregorDecl *
827259571e27e513cfaf691cc7447e09b31a47d5438Douglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
828e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor                                      TemplateParameterList *TemplateParams) {
829e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (FunctionTemplate && !TemplateParams) {
832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We are creating a function template specialization from a function
833dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor    // template. Check whether there is already a function template
834dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor    // specialization for this particular set of template arguments.
835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    llvm::FoldingSetNodeID ID;
836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    FunctionTemplateSpecializationInfo::Profile(ID,
837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
838e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                      TemplateArgs.getInnermost().flat_size(),
839e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                SemaRef.Context);
840e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
841d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplateSpecializationInfo *Info
8427974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
8437974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                                                   InsertPos);
844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // If we already have a function template specialization, return it.
846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Info)
847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return Info->Function;
848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    !(isa<Decl>(Owner) &&
852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType T = SubstFunctionType(D, Params);
857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (T.isNull())
858cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis    return 0;
8597974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8607974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  // Build the instantiated method declaration.
8617974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
862d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  CXXMethodDecl *Method = 0;
863550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
864550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  DeclarationName Name = D->getDeclName();
865550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
866550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
8672a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
868895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
869d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                        Constructor->getLocation(),
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Name, T,
872d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                        Constructor->getTypeSourceInfo(),
873ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Constructor->isExplicit(),
874a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                        Constructor->isInlineSpecified(), false);
875a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
876a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
877a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
878a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
879a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
880a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       Destructor->getLocation(), Name,
881a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       T, Destructor->isInlineSpecified(), false);
882a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
883a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    CanQualType ConvTy
884d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor      = SemaRef.Context.getCanonicalType(
885d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      T->getAs<FunctionType>()->getResultType());
88646460a68f6508775e98c19b4bb8454bb471aac24John McCall    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
88746460a68f6508775e98c19b4bb8454bb471aac24John McCall                                                                      ConvTy);
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
889d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                       Conversion->getLocation(), Name,
89037d68185088947322a97eabdc1c0714b0debd929Douglas Gregor                                       T, Conversion->getTypeSourceInfo(),
891a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       Conversion->isInlineSpecified(),
89237d68185088947322a97eabdc1c0714b0debd929Douglas Gregor                                       Conversion->isExplicit());
893a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  } else {
894a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
895e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall                                   D->getDeclName(), T, D->getTypeSourceInfo(),
896b1a56e767cfb645fcb25027ab728dd5824d92615John McCall                                   D->isStatic(), D->isInlineSpecified());
897b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
898e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
899e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (TemplateParams) {
900e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall    // Our resulting instantiation is actually a function template, since we
901b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    // are substituting only the outer template parameters. For example, given
902a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
903a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
904b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    //   struct X {
905b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    //     template<typename U> void f(T, U);
906a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
907b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    //
908d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
909d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
910d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
911d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
912d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    // Build the function template itself.
913d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
914d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                                                    Method->getLocation(),
9156c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                    Method->getDeclName(),
9167c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                    TemplateParams, Method);
9177c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    if (D->isOutOfLine())
9186c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
9196c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    Method->setDescribedFunctionTemplate(FunctionTemplate);
9206c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  } else if (FunctionTemplate) {
9216c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    // Record this function template specialization.
922d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Method->setFunctionTemplateSpecialization(SemaRef.Context,
923d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor                                              FunctionTemplate,
9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              &TemplateArgs.getInnermost(),
925741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                              InsertPos);
926741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor  } else {
927b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    // Record that this is an instantiation of a member function.
928b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
929b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  }
930b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
931b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // If we are instantiating a member function defined
932d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  // out-of-line, the instantiation will have the same lexical
933eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // context (which will be a namespace scope) as the template.
934eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->isOutOfLine())
935eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Method->setLexicalDeclContext(D->getLexicalDeclContext());
936eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman
937eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // Attach the parameters
938d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
939f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Params[P]->setOwningFunction(Method);
940d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Method->setParams(SemaRef.Context, Params.data(), Params.size());
94102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
94202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (InitMethodInstantiation(Method, D))
94302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Method->setInvalidDecl();
94402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
94502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
9469901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
9479901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
9489901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
9499901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner);
9509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
9519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    // In C++, the previous declaration we find might be a tag type
952d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    // (class or enum). In this case, the new declaration will hide the
95317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis    // tag type. Note that this does does not apply if we're declaring a
954d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
955d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    if (Previous.isSingleTagDecl())
956d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor      Previous.clear();
95702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
95802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
95902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  bool Redeclaration = false;
96002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  bool OverloadableAttrRequired = false;
96102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
9627557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
963a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
964127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (D->isPure())
965127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
966127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
967127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
968b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      !Method->getFriendObjectKind())
96924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    Owner->addDecl(Method);
97024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Method;
9722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis}
9732c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis
9742c853e401ca406d417eb916e867226050e7be06bArgyrios KyrtzidisDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return VisitCXXMethodDecl(D);
976127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor}
9772c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis
9782c853e401ca406d417eb916e867226050e7be06bArgyrios KyrtzidisDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
979127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  return VisitCXXMethodDecl(D);
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
981b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
982b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCallDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
983b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  return VisitCXXMethodDecl(D);
984b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall}
985b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
986b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCallParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
98779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  QualType T;
988b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor  TypeSourceInfo *DI = D->getTypeSourceInfo();
98979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  if (DI) {
99079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
9912a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                           D->getDeclName());
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (DI) T = DI->getType();
993e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  } else {
99421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
99521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                          D->getDeclName());
99621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    DI = 0;
9972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  }
99821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall
999fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall  if (T.isNull())
1000d325daa506338ab86f9dd468b48fd010673f49a6John McCall    return 0;
1001d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1002d325daa506338ab86f9dd468b48fd010673f49a6John McCall  T = SemaRef.adjustParameterType(T);
1003d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1004d325daa506338ab86f9dd468b48fd010673f49a6John McCall  // Allocate the parameter
1005d325daa506338ab86f9dd468b48fd010673f49a6John McCall  ParmVarDecl *Param
1006d325daa506338ab86f9dd468b48fd010673f49a6John McCall    = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1007d325daa506338ab86f9dd468b48fd010673f49a6John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
100868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
100968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // Mark the default argument as being uninstantiated.
101068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->hasUninstantiatedDefaultArg())
101168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
101268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  else if (Expr *Arg = D->getDefaultArg())
1013d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Param->setUninstantiatedDefaultArg(Arg);
1014d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1015d325daa506338ab86f9dd468b48fd010673f49a6John McCall  // Note: we don't try to instantiate function parameters until after
1016d325daa506338ab86f9dd468b48fd010673f49a6John McCall  // we've instantiated the function's type. Therefore, we don't have
1017d325daa506338ab86f9dd468b48fd010673f49a6John McCall  // to check for 'void' parameter types here.
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1019d325daa506338ab86f9dd468b48fd010673f49a6John McCall  return Param;
10207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor}
10217c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
1022d325daa506338ab86f9dd468b48fd010673f49a6John McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
102368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall                                                    TemplateTypeParmDecl *D) {
102402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // TODO: don't always clone when decls are refcounted.
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const Type* T = D->getTypeForDecl();
102621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(T->isTemplateTypeParmType());
102716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
10280130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor
1029b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  TemplateTypeParmDecl *Inst =
1030d325daa506338ab86f9dd468b48fd010673f49a6John McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1031d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1032b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                 TTPT->getName(),
1033b1a56e767cfb645fcb25027ab728dd5824d92615John McCall                                 D->wasDeclaredWithTypename(),
1034b1a56e767cfb645fcb25027ab728dd5824d92615John McCall                                 D->isParameterPack());
1035b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1036b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (D->hasDefaultArgument())
1037b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1038b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1039b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Introduce this template parameter's instantiation into the instantiation
10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // scope.
1041e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1042e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1043e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Inst;
1044838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor}
104502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1046ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1047a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                 NonTypeTemplateParmDecl *D) {
1048a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Substitute into the type of the non-type template parameter.
1049a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  QualType T;
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  TypeSourceInfo *DI = D->getTypeSourceInfo();
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (DI) {
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                           D->getDeclName());
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (DI) T = DI->getType();
1055a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  } else {
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                          D->getDeclName());
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    DI = 0;
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (T.isNull())
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    return 0;
1062d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1063a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Check that this type is acceptable for a non-type template parameter.
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  bool Invalid = false;
1065a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1066a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (T.isNull()) {
1067b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    T = SemaRef.Context.IntTy;
1068b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    Invalid = true;
1069d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1070d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1071d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NonTypeTemplateParmDecl *Param
1072d325daa506338ab86f9dd468b48fd010673f49a6John McCall    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1073d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                      D->getDepth() - 1, D->getPosition(),
1074d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                      D->getIdentifier(), T, DI);
1075d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Invalid)
107666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    Param->setInvalidDecl();
107766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
107824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
107924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
1080838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
108124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  // scope.
108224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
108324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  return Param;
108466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor}
1085d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1086d325daa506338ab86f9dd468b48fd010673f49a6John McCallDecl *
1087d325daa506338ab86f9dd468b48fd010673f49a6John McCallTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1088d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                  TemplateTemplateParmDecl *D) {
108902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Instantiate the template parameter list of the template template parameter.
1090a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1091e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *InstParams;
1092e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  {
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Perform the actual substitution of template parameters within a new,
1094e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // local instantiation scope.
1095e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
1096af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    InstParams = SubstTemplateParams(TempParams);
1097a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (!InstParams)
10986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      return NULL;
10996826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  }
11006826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1101af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // Build the template template parameter.
1102af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  TemplateTemplateParmDecl *Param
1103af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1104af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                       D->getDepth() - 1, D->getPosition(),
1105af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                       D->getIdentifier(), InstParams);
1106af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Param->setDefaultArgument(D->getDefaultArgument());
1107af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // Introduce this template parameter's instantiation into the instantiation
1109af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // scope.
1110af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1111af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1112af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  return Param;
1113af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1114af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1115af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1116af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // Using directives are never dependent, so they require no explicit
1117af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1118af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  UsingDirectiveDecl *Inst
1119af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1120af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                 D->getNamespaceKeyLocation(),
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                 D->getQualifierRange(), D->getQualifier(),
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                 D->getIdentLocation(),
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                 D->getNominatedNamespace(),
1124af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                 D->getCommonAncestor());
1125af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Owner->addDecl(Inst);
1126af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  return Inst;
1127af2094e7cecadf36667deb61a83587ffdd979bd3John McCall}
1128af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1129af2094e7cecadf36667deb61a83587ffdd979bd3John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1130af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // The nested name specifier is non-dependent, so no transformation
1131af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // is required.
1132af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1133af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // We only need to do redeclaration lookups if we're in a class
1134af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // scope (in fact, it's not really even possible in non-class
1135af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // scopes).
1136af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool CheckRedeclaration = Owner->isRecord();
1137a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1138a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1139a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
11406826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1141a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1142a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       D->getLocation(),
1143a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       D->getNestedNameRange(),
1144a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       D->getUsingLocation(),
1145a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       D->getTargetNestedNameDecl(),
11466826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                                       D->getDeclName(),
11476826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                                       D->isTypeName());
1148a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1149a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  CXXScopeSpec SS;
11509f54ad4381370c6b771424b53d219e661d6d6706John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1151af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  SS.setRange(D->getNestedNameRange());
1152e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1153e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (CheckRedeclaration) {
115476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    Prev.setHideTags(false);
115576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
115676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
115776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    // Check for invalid redeclarations.
1158a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1159a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                            D->isTypeName(), SS,
1160d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                            D->getLocation(), Prev))
11616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      NewUD->setInvalidDecl();
116276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1163a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
116476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1165a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!NewUD->isInvalidDecl() &&
116676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
116776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                                      D->getLocation()))
116876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    NewUD->setInvalidDecl();
1169238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1170238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1171238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  NewUD->setAccess(D->getAccess());
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  Owner->addDecl(NewUD);
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
117406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // Don't process the shadow decls for an invalid decl.
1175238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  if (NewUD->isInvalidDecl())
1176238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    return NewUD;
1177238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1178238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  bool isFunctionScope = Owner->isFunctionOrMethod();
1179238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1180238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  // Process the shadow decls.
1181238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1182238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor         I != E; ++I) {
1183238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    UsingShadowDecl *Shadow = *I;
1184238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    NamedDecl *InstTarget =
1185238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1186238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                                   TemplateArgs));
1187238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1188238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (CheckRedeclaration &&
1189238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
119006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      continue;
1191238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1192238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    UsingShadowDecl *InstShadow
1193238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1194238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1195238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1196238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (isFunctionScope)
1197238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
1198238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  }
1199238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1200238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  return NewUD;
1201a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor}
1202a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
120376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
120476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
120576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  return 0;
120676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall}
1207e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1208e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorDecl * TemplateDeclInstantiator
12092dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1210d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  NestedNameSpecifier *NNS =
1211d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1212d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                     D->getTargetNestedNameRange(),
12136b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                     TemplateArgs);
12146b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  if (!NNS)
1215d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXScopeSpec SS;
1218d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  SS.setRange(D->getTargetNestedNameRange());
121924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  SS.setScopeRep(NNS);
122024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
12222c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12232c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                  D->getUsingLoc(), SS, D->getLocation(),
12242c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                  D->getDeclName(), 0,
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  /*instantiation*/ true,
12266b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                  /*typename*/ true, D->getTypenameLoc());
12272c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis  if (UD)
12282c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
12296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
12306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  return UD;
1231b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall}
1232b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1233b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCallDecl * TemplateDeclInstantiator
1234b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1235b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *NNS =
1236b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
123779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor                                     D->getTargetNestedNameRange(),
123879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor                                     TemplateArgs);
123979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  if (!NNS)
12402a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    return 0;
124148dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12420ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  CXXScopeSpec SS;
124321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  SS.setRange(D->getTargetNestedNameRange());
124421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  SS.setScopeRep(NNS);
124521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall
12462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  NamedDecl *UD =
124721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
12482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor                                  D->getUsingLoc(), SS, D->getLocation(),
12495f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                  D->getDeclName(), 0,
12505f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                  /*instantiation*/ true,
12515f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                  /*typename*/ false, SourceLocation());
12525f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  if (UD)
12535f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
12545f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12555f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  return UD;
12565f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor}
12575f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12585f970eee81372dfc6a1457c3d6d052af04e32a38Douglas GregorDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
12595f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
12605f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
12615f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  return Instantiator.Visit(D);
12625f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor}
12635f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12645f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor/// \brief Instantiates a nested template parameter list in the current
12655f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor/// instantiation context.
12665f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor///
12675f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor/// \param L The parameter list to instantiate
12685f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor///
12695f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor/// \returns NULL if there was an error
1270b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCallTemplateParameterList *
1271b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1272b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  // Get errors for all the parameters before bailing out.
1273b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool Invalid = false;
1274b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1275b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  unsigned N = L->size();
1276b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  ParamVector Params;
1278b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  Params.reserve(N);
1279b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1280b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall       PI != PE; ++PI) {
1281b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1282b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Params.push_back(D);
1283b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Invalid = Invalid || !D || D->isInvalidDecl();
1284b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1285b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1286b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  // Clean up if we had an error.
1287b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Invalid) {
1288b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1289b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall         PI != PE; ++PI)
1290b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      if (*PI)
1291b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall        (*PI)->Destroy(SemaRef.Context);
1292b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    return NULL;
12932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  }
1294b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1295dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  TemplateParameterList *InstL
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
12972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                    L->getLAngleLoc(), &Params.front(), N,
12982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                    L->getRAngleLoc());
129917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return InstL;
13001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
13012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Instantiate the declaration of a class template partial
130316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor/// specialization.
130416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor///
130517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
130617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor// specialized by the instantiation of \p PartialSpec.
13072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara///
13082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara/// \param PartialSpec the (uninstantiated) class template partial
130916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor/// specialization that we are instantiating.
131065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
131165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \returns true if there was an error, false otherwise.
13122577743c5650c646fb705df01403707e94f2df04Abramo Bagnarabool
13130130f3cc4ccd5f46361c48d5fe94133d74619424Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
131465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1315dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
13162577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // Create a local instantiation scope for this class template partial
13172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // specialization, which will contain the instantiations of the template
131816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  // parameters.
131916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
132016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor
1321dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  // Substitute into the template parameters of the class template partial
13226b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  // specialization.
1323b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1324b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1325b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!InstParams)
1326d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return true;
1327d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1328d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // Substitute into the template arguments of the class template partial
13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // specialization.
1330d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  const TemplateArgumentLoc *PartialSpecTemplateArgs
1331d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    = PartialSpec->getTemplateArgsAsWritten();
1332d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1333d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1334d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1335d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  for (unsigned I = 0; I != N; ++I) {
1336d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    TemplateArgumentLoc Loc;
1337d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1338d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor      return true;
1339d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    InstTemplateArgs.addArgument(Loc);
1340d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  }
1341d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
13421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1343d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // Check that the template argument list is well-formed for this
1344b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  // class template.
1345b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1346b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                        InstTemplateArgs.size());
1347b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
13481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        PartialSpec->getLocation(),
1349d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                        InstTemplateArgs,
135066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                        false,
135166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                        Converted))
135224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    return true;
135324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor
1354838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  // Figure out where to insert this class template partial specialization
135524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  // in the member template's set of class template partial specializations.
135624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  llvm::FoldingSetNodeID ID;
135724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
135866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                  Converted.getFlatArguments(),
1359b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                  Converted.flatSize(),
136066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                  SemaRef.Context);
13612db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor  void *InsertPos = 0;
136266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
136366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
13641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                                     InsertPos);
13657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
13667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Build the canonical type that describes the converted template
1367b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  // arguments of the class template partial specialization.
1368b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  QualType CanonType
1369b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1370b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                  Converted.getFlatArguments(),
13717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                    Converted.flatSize());
13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Build the fully-sugared type for this class template
13745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // specialization as the user wrote in the specialization
13755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1376838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  // from the specialization's declaration the way that the user
13775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // actually wrote the specialization, rather than formatting the
13785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // name based on the "canonical" representation used to store the
13795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // template arguments in the specialization.
13802dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  QualType WrittenTy
13812577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
13822577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                                    InstTemplateArgs,
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    CanonType);
1384b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1385b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (PrevDecl) {
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We've already seen a partial specialization with the same template
1387dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // parameters and template arguments. This can happen, for example, when
1388dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // substituting the outer template arguments ends up causing two
1389dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // class template partial specializations of a member class template
1390dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // to have identical forms, e.g.,
13916826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    //
13926826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    //   template<typename T, typename U>
1393dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    //   struct Outer {
13942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    //     template<typename X, typename Y> struct Inner;
139565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
139665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
13979f54ad4381370c6b771424b53d219e661d6d6706John McCall    //   };
139865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //
139965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
14004ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    //                          // have the same signature.
14014ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
14024ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor      << WrittenTy;
140346460a68f6508775e98c19b4bb8454bb471aac24John McCall    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
140446460a68f6508775e98c19b4bb8454bb471aac24John McCall      << SemaRef.Context.getTypeDeclType(PrevDecl);
1405b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    return true;
1406b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1407b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1408b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1409b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  // Create the class template partial specialization declaration.
1410b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1411b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1412b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     PartialSpec->getLocation(),
1413b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     InstParams,
1414b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     ClassTemplate,
1415b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     Converted,
1416b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     InstTemplateArgs,
1417b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                     0);
1418bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
14192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
14202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
14212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Add this partial specialization to the set of class template partial
1422615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  // specializations.
1423dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1424615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor                                                        InsertPos);
1425615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor  return false;
142603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
142717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor
142803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor/// \brief Does substitution on the type of the given function, including
142903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor/// all of the function parameters.
1430bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor///
143165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param D The function whose type will be the basis of the substitution
1432bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor///
1433bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor/// \param Params the instantiated parameter declarations
14346477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
1435cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor/// \returns the instantiated function's type if successful, a NULL
14362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor/// type if there was an error.
14372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas GregorQualType
1438e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
1439e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1440e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool InvalidDecl = false;
1441efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor
1442efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  // Substitute all of the function's formal parameter types.
1443efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::SmallVector<QualType, 4> ParamTys;
1445e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (FunctionDecl::param_iterator P = D->param_begin(),
1446e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 PEnd = D->param_end();
1447efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor       P != PEnd; ++P) {
1448efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor    if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
1449e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (PInst->getType()->isVoidType()) {
1450e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1451e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        PInst->setInvalidDecl();
14520f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor      } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
14530f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor                                                PInst->getType(),
1454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                diag::err_abstract_type_in_decl,
1455550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                                Sema::AbstractParamType))
1456550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor        PInst->setInvalidDecl();
1457550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1458550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      Params.push_back(PInst);
1459e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      ParamTys.push_back(PInst->getType());
1460e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1461e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (PInst->isInvalidDecl())
146233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor        InvalidDecl = true;
146333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    } else
146433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor      InvalidDecl = true;
146533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1466a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall
146733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // FIXME: Deallocate dead declarations.
146833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (InvalidDecl)
146933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return QualType();
147033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
147133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
147233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  assert(Proto && "Missing prototype?");
147333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType ResultType
147433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
147533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                        D->getLocation(), D->getDeclName());
147633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (ResultType.isNull())
147733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return QualType();
147833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
147933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
148033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                   Proto->isVariadic(), Proto->getTypeQuals(),
148133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                   D->getLocation(), D->getDeclName());
148233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
148333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
148433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor/// \brief Initializes the common fields of an instantiation function
148533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
148633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor///
148733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor/// \returns true if there was an error
148833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregorbool
148933642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
149033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                    FunctionDecl *Tmpl) {
149133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Tmpl->isDeleted())
149233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    New->setDeleted();
149333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1494d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  // If we are performing substituting explicitly-specified template arguments
1495550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // or deduced template arguments into a function template and we reach this
1496550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // point, we are now past the point where SFINAE applies and have committed
1497550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // to keeping the new function template specialization. We therefore
1498550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // convert the active template instantiation for the function template
149933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // into a template instantiation for this specific function template
150033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
150133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // further errors in the declaration itself.
15020dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
15039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
15049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
15059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
15069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (FunctionTemplateDecl *FunTmpl
15079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
15089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
15099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor             "Deduction from the wrong function template?");
15109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      (void) FunTmpl;
15112a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
15129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
15139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      --SemaRef.NonInstantiationEntries;
15149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    }
15159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
15189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  assert(Proto && "Function template without prototype?");
15199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
15219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      Proto->getNoReturnAttr()) {
1522d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara    // The function has an exception specification or a "noreturn"
15234469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor    // attribute. Substitute into each of the exception types.
15249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
15259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
15269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      // FIXME: Poor location information!
15279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      QualType T
15289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
15299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                            New->getLocation(), New->getDeclName());
15309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      if (T.isNull() ||
153148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
153248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        continue;
153348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
153448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      Exceptions.push_back(T);
153548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    }
153648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
153748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    // Rebuild the function type
153848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
153948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    const FunctionProtoType *NewProto
154048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      = New->getType()->getAs<FunctionProtoType>();
154148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
154248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
154348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                 NewProto->arg_type_begin(),
154448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                 NewProto->getNumArgs(),
1545ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                                 NewProto->isVariadic(),
1546ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                                 NewProto->getTypeQuals(),
1547ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                                 Proto->hasExceptionSpec(),
1548ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                                 Proto->hasAnyExceptionSpec(),
1549ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                                 Exceptions.size(),
15509f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                 Exceptions.data(),
15519f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                 Proto->getNoReturnAttr()));
15529f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
15539f54ad4381370c6b771424b53d219e661d6d6706John McCall
15549f54ad4381370c6b771424b53d219e661d6d6706John McCall  return false;
1555ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara}
1556ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
15579f54ad4381370c6b771424b53d219e661d6d6706John McCall/// \brief Initializes common fields of an instantiated method
1558ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// declaration (New) from the corresponding fields of its template
1559ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// (Tmpl).
1560ed97649e9574b9d854fa4d6109c9333ae0993554John McCall///
1561ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// \returns true if there was an error
1562ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnarabool
1563ed97649e9574b9d854fa4d6109c9333ae0993554John McCallTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
1564ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                                  CXXMethodDecl *Tmpl) {
1565ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (InitFunctionInstantiation(New, Tmpl))
1566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return true;
1567ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15689f54ad4381370c6b771424b53d219e661d6d6706John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
15699f54ad4381370c6b771424b53d219e661d6d6706John McCall  New->setAccess(Tmpl->getAccess());
15709f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (Tmpl->isVirtualAsWritten())
15719f54ad4381370c6b771424b53d219e661d6d6706John McCall    Record->setMethodAsVirtual(New);
15729f54ad4381370c6b771424b53d219e661d6d6706John McCall
15739f54ad4381370c6b771424b53d219e661d6d6706John McCall  // FIXME: attributes
15749f54ad4381370c6b771424b53d219e661d6d6706John McCall  // FIXME: New needs a pointer to Tmpl
15759f54ad4381370c6b771424b53d219e661d6d6706John McCall  return false;
15769f54ad4381370c6b771424b53d219e661d6d6706John McCall}
15779f54ad4381370c6b771424b53d219e661d6d6706John McCall
15789f54ad4381370c6b771424b53d219e661d6d6706John McCall/// \brief Instantiate the definition of the given function from its
15799f54ad4381370c6b771424b53d219e661d6d6706John McCall/// template.
15809f54ad4381370c6b771424b53d219e661d6d6706John McCall///
15819f54ad4381370c6b771424b53d219e661d6d6706John McCall/// \param PointOfInstantiation the point at which the instantiation was
15829f54ad4381370c6b771424b53d219e661d6d6706John McCall/// required. Note that this is not precisely a "point of instantiation"
1583ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// for the function, but it's close.
1584ed97649e9574b9d854fa4d6109c9333ae0993554John McCall///
15859f54ad4381370c6b771424b53d219e661d6d6706John McCall/// \param Function the already-instantiated declaration of a
1586ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// function template specialization or member function of a class template
1587ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// specialization.
1588ed97649e9574b9d854fa4d6109c9333ae0993554John McCall///
1589ed97649e9574b9d854fa4d6109c9333ae0993554John McCall/// \param Recursive if true, recursively instantiates any functions that
15909f54ad4381370c6b771424b53d219e661d6d6706John McCall/// are required by this instantiation.
15919f54ad4381370c6b771424b53d219e661d6d6706John McCall///
15929f54ad4381370c6b771424b53d219e661d6d6706John McCall/// \param DefinitionRequired if true, then we are performing an explicit
15939f54ad4381370c6b771424b53d219e661d6d6706John McCall/// instantiation where the body of the function is required. Complain if
1594323c310efa0abd7a786b0303501186b5f33eb8d7John McCall/// there is no such body.
1595323c310efa0abd7a786b0303501186b5f33eb8d7John McCallvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
15969f54ad4381370c6b771424b53d219e661d6d6706John McCall                                         FunctionDecl *Function,
15979f54ad4381370c6b771424b53d219e661d6d6706John McCall                                         bool Recursive,
15989f54ad4381370c6b771424b53d219e661d6d6706John McCall                                         bool DefinitionRequired) {
15999f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (Function->isInvalidDecl())
16009f54ad4381370c6b771424b53d219e661d6d6706John McCall    return;
16017c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
16027c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  assert(!Function->getBody() && "Already instantiated!");
16039f54ad4381370c6b771424b53d219e661d6d6706John McCall
16049f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Never instantiate an explicit specialization.
16059f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
16069f54ad4381370c6b771424b53d219e661d6d6706John McCall    return;
16079f54ad4381370c6b771424b53d219e661d6d6706John McCall
16089f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Find the function body that we'll be substituting.
16099f54ad4381370c6b771424b53d219e661d6d6706John McCall  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
16109f54ad4381370c6b771424b53d219e661d6d6706John McCall  Stmt *Pattern = 0;
16119f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (PatternDecl)
1612323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    Pattern = PatternDecl->getBody(PatternDecl);
1613323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1614323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  if (!Pattern) {
16159f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (DefinitionRequired) {
1616ed97649e9574b9d854fa4d6109c9333ae0993554John McCall      if (Function->getPrimaryTemplate())
1617ed97649e9574b9d854fa4d6109c9333ae0993554John McCall        Diag(PointOfInstantiation,
1618ed97649e9574b9d854fa4d6109c9333ae0993554John McCall             diag::err_explicit_instantiation_undefined_func_template)
1619ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          << Function->getPrimaryTemplate();
1620ed97649e9574b9d854fa4d6109c9333ae0993554John McCall      else
16219f54ad4381370c6b771424b53d219e661d6d6706John McCall        Diag(PointOfInstantiation,
16229f54ad4381370c6b771424b53d219e661d6d6706John McCall             diag::err_explicit_instantiation_undefined_member)
1623ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          << 1 << Function->getDeclName() << Function->getDeclContext();
1624ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (PatternDecl)
16267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        Diag(PatternDecl->getLocation(),
16277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall             diag::note_explicit_instantiation_here);
16287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
16297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return;
16317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  }
16327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  // C++0x [temp.explicit]p9:
16347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  //   Except for inline functions, other explicit instantiation declarations
16357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  //   have the effect of suppressing the implicit instantiation of the entity
16367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  //   to which they refer.
16377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (Function->getTemplateSpecializationKind()
1638ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara        == TSK_ExplicitInstantiationDeclaration &&
1639ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara      !PatternDecl->isInlined())
1640ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    return;
16417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1643ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  if (Inst)
16447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return;
16457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16464469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  // If we're performing recursive template instantiation, create our own
1647ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // queue of pending implicit instantiations that we will instantiate later,
1648ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // while we're still within our own instantiation context.
16497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
16507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (Recursive)
16517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
16527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Introduce a new scope where local variable instantiations will be
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // recorded, unless we're actually a member function within a local
16570dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  // class, in which case we need to merge our results with the parent
16580dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  // scope (of the enclosing function).
16590dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  bool MergeWithParentScope = false;
16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
16610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    MergeWithParentScope = Rec->isLocalClass();
16620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
16630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  LocalInstantiationScope Scope(*this, MergeWithParentScope);
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1665ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Introduce the instantiated function parameters into the local
1666ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // instantiation scope.
1667ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
16699488ea120e093068021f944176c3d610dd540914John McCall                            Function->getParamDecl(I));
1670ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
16717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  // Enter the scope of this instantiation. We don't use
16727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  // PushDeclContext because we don't have a scope.
16734469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  DeclContext *PreviousContext = CurContext;
1674ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CurContext = Function;
1675ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  MultiLevelTemplateArgumentList TemplateArgs =
16770dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    getTemplateInstantiationArgs(Function);
16780dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1679ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // If this is a constructor, instantiate the member initializers.
1680d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor  if (const CXXConstructorDecl *Ctor =
16817e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
16822fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
16832fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor                               TemplateArgs);
16842fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  }
16858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
16868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Instantiate the function body.
16878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1688e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1689e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Body.isInvalid())
1690e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Function->setInvalidDecl();
1691e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1692e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1693e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                          /*IsInstantiation=*/true);
1694e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1695ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  CurContext = PreviousContext;
1696e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1697e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  DeclGroupRef DG(Function);
1698e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Consumer.HandleTopLevelDecl(DG);
1699e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1700bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // This class may have local implicit instantiations that need to be
1701e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // instantiation within this scope.
1702e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1703e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Scope.Exit();
1704e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1705bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Recursive) {
1706e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    // Instantiate any pending implicit instantiations found during the
17079148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    // instantiation of this template.
1708e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    PerformPendingImplicitInstantiations();
1709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1710e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    // Restore the set of pending implicit instantiations.
1711ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1712e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1714e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiate the definition of the given variable from its
1716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// template.
1717e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param PointOfInstantiation the point at which the instantiation was
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// required. Note that this is not precisely a "point of instantiation"
1720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// for the function, but it's close.
1721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1722ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param Var the already-instantiated declaration of a static member
1723ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// variable of a class template specialization.
1724ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1725ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1726ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// are required by this instantiation.
1727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// is required. Complain if there is no such definition.
1731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
1732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                          SourceLocation PointOfInstantiation,
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 VarDecl *Var,
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 bool Recursive,
1735550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                                 bool DefinitionRequired) {
1736550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (Var->isInvalidDecl())
1737550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor    return;
17382a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
1739550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Find the out-of-line definition of this static data member.
1740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
1741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
1742ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
1743ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Def = Def->getOutOfLineDefinition();
1744ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!Def) {
1746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We did not find an out-of-line definition of this static data member,
1747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
1748ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // instantiate this definition (or provide a specialization for it) in
1749833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    // another translation unit.
1750833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    if (DefinitionRequired) {
1751833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Def = Var->getInstantiatedFromStaticDataMember();
1752833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Diag(PointOfInstantiation,
1753d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall           diag::err_explicit_instantiation_undefined_member)
1754833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        << 2 << Var->getDeclName() << Var->getDeclContext();
1755d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1756d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    }
1757ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1758d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    return;
1759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Never instantiate an explicit specialization.
1762ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1763ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return;
1764ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1765ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // C++0x [temp.explicit]p9:
1766ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
1767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
1768d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  //   to which they refer.
1769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Var->getTemplateSpecializationKind()
1770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
1771ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return;
1772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1774ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Inst)
1775ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return;
1776ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1777cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  // If we're performing recursive template instantiation, create our own
1778cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  // queue of pending implicit instantiations that we will instantiate later,
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // while we're still within our own instantiation context.
1780ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Recursive)
1782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Enter the scope of this instantiation. We don't use
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // PushDeclContext because we don't have a scope.
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext *PreviousContext = CurContext;
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  CurContext = Var->getDeclContext();
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  VarDecl *OldVar = Var;
1790ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
1791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
1792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  CurContext = PreviousContext;
1793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
17943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Var) {
17953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Var->setPreviousDeclaration(OldVar);
17963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
17973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(MSInfo && "Missing member specialization information?");
1798d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                       MSInfo->getPointOfInstantiation());
1800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    DeclGroupRef DG(Var);
1801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
1802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Recursive) {
1805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // Instantiate any pending implicit instantiations found during the
1806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // instantiation of this template.
1807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PerformPendingImplicitInstantiations();
1808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // Restore the set of pending implicit instantiations.
1810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorvoid
1815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorSema::InstantiateMemInitializers(CXXConstructorDecl *New,
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 const CXXConstructorDecl *Tmpl,
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs) {
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<MemInitTy*, 4> NewInits;
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1821ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all the initializers.
1822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
1823ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            InitsEnd = Tmpl->init_end();
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       Inits != InitsEnd; ++Inits) {
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    CXXBaseOrMemberInitializer *Init = *Inits;
1826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
182713c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
182813c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor
182913c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    // Instantiate all the arguments.
1830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         Args != ArgsEnd; ++Args) {
1832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1834d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall      if (NewArg.isInvalid())
18353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        New->setInvalidDecl();
1836dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor      else
1837cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis        NewArgs.push_back(NewArg.takeAs<Expr>());
1838b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    }
1839b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1840b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    MemInitResult NewInit;
1841b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1842ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Init->isBaseInitializer()) {
18434469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
18444469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor                                            TemplateArgs,
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            Init->getSourceLocation(),
1846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            New->getDeclName());
1847cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis      if (!BaseTInfo) {
1848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        New->setInvalidDecl();
1849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        continue;
1850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      }
185121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall
185221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
185321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                     (Expr **)NewArgs.data(),
185421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                     NewArgs.size(),
185521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                     Init->getLParenLoc(),
185621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                     Init->getRParenLoc(),
18576cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                     New->getParent());
18586cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    } else if (Init->isMemberInitializer()) {
18596cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall      FieldDecl *Member;
18606cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall
186121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      // Is this an anonymous union?
186221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
18635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
1864cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor      else
1865cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1866895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                      TemplateArgs));
18676920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor
18686920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
18696920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                       NewArgs.size(),
18706920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                       Init->getSourceLocation(),
18716920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                       Init->getLParenLoc(),
18726920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                       Init->getRParenLoc());
18736920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    }
18746920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor
18756920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (NewInit.isInvalid())
1876895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor      New->setInvalidDecl();
1877895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    else {
18786920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      // FIXME: It would be nice if ASTOwningVector had a release function.
1879895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor      NewArgs.take();
1880cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor
1881cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor      NewInits.push_back((MemInitTy *)NewInit.get());
1882cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1883cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
1884cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor
18856920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor  // Assign all the initializers to the new constructor.
18866920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor  ActOnMemInitializers(DeclPtrTy::make(New),
18876920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                       /*FIXME: ColonLoc */
18886920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                       SourceLocation(),
18896920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                       NewInits.data(), NewInits.size());
18906920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor}
18916920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor
18926920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor// TODO: this could be templated if the various decl types used the
1893cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor// same method name.
1894cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregorstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
189521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              ClassTemplateDecl *Instance) {
18965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  Pattern = Pattern->getCanonicalDecl();
18975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
18981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  do {
1899e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Instance = Instance->getCanonicalDecl();
1900e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (Pattern == Instance) return true;
1901e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } while (Instance);
19031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1904e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1905e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1906e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1908cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor                              FunctionTemplateDecl *Instance) {
1909cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
1910cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
19111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  do {
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Instance = Instance->getCanonicalDecl();
1913cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    if (Pattern == Instance) return true;
1914cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
1915cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  } while (Instance);
1916cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
1917cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  return false;
1918cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor}
1919cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor
19201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool
1921cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                  ClassTemplatePartialSpecializationDecl *Instance) {
1923cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  Pattern
1924bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1925cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  do {
1926cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
1927f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor                                                Instance->getCanonicalDecl());
1928cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    if (Pattern == Instance)
1929cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      return true;
19301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Instance = Instance->getInstantiatedFromMember();
19310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  } while (Instance);
19320ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19330ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  return false;
19340ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor}
19350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregorstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
19370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                              CXXRecordDecl *Instance) {
19380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
19390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  do {
19410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    Instance = Instance->getCanonicalDecl();
19420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    if (Pattern == Instance) return true;
19430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    Instance = Instance->getInstantiatedFromMemberClass();
19440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  } while (Instance);
19450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  return false;
19470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor}
19480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregorstatic bool isInstantiationOf(FunctionDecl *Pattern,
19500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                              FunctionDecl *Instance) {
19510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
19520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  do {
19540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    Instance = Instance->getCanonicalDecl();
19550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    if (Pattern == Instance) return true;
19560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    Instance = Instance->getInstantiatedFromMemberFunction();
19570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  } while (Instance);
19580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  return false;
19600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor}
19610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregorstatic bool isInstantiationOf(EnumDecl *Pattern,
19630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                              EnumDecl *Instance) {
19640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
1965264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
19660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  do {
19670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    Instance = Instance->getCanonicalDecl();
19681d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    if (Pattern == Instance) return true;
19697cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor    Instance = Instance->getInstantiatedFromMemberEnum();
1970e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  } while (Instance);
1971e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1972e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
19735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19755545e166a956a20d7a6b58408e251a1119025485Douglas Gregorstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
19765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              UsingShadowDecl *Instance,
19775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              ASTContext &C) {
19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
19791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
19805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1981e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregorstatic bool isInstantiationOf(UsingDecl *Pattern,
1982e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              UsingDecl *Instance,
19831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              ASTContext &C) {
19845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
19855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1986e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian
1987e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanianstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
19885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              UsingDecl *Instance,
19895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                              ASTContext &C) {
19905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
19915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1993a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregorstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
1994a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor                              UsingDecl *Instance,
1995a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor                              ASTContext &C) {
1996a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
1997b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor}
1998b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
1999b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregorstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2000b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                              VarDecl *Instance) {
2001a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor  assert(Instance->isStaticDataMember());
2002b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
2003b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  Pattern = Pattern->getCanonicalDecl();
2004b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
2005b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  do {
2006b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    Instance = Instance->getCanonicalDecl();
2007e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (Pattern == Instance) return true;
2008e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    Instance = Instance->getInstantiatedFromStaticDataMember();
2009e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  } while (Instance);
2010e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2011f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  return false;
2012b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor}
2013e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2014e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor// Other is the prospective instantiation
201506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis// D is the prospective pattern
201654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
201754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (D->getKind() != Other->getKind()) {
2018251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    if (UnresolvedUsingTypenameDecl *UUD
2019251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2020251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
20216cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor        return isInstantiationOf(UUD, UD, Ctx);
20221eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor      }
20233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor    }
20241eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
20251eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    if (UnresolvedUsingValueDecl *UUD
20266fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
20271eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2028e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        return isInstantiationOf(UUD, UD, Ctx);
2029e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      }
2030e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2031e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2032e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    return false;
2033e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
2034e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2035e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2036e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
2037e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2038e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2039e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2040e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2041e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2042cfe833be882f600206f1587f157b025b368497d7Douglas Gregor    return isInstantiationOf(cast<EnumDecl>(D), Enum);
204358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
204458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
204562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    if (Var->isStaticDataMember())
204658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2047e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
204858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
20491eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2050e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20511eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2052d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2053d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2055d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2057d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor                             PartialSpec);
20587ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor
2059d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
20601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (!Field->getDeclName()) {
2061f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      // This is an unnamed field.
2062f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2063e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor        cast<FieldDecl>(D);
2064e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    }
2065b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2066b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
2067b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
206862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2069b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
207062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
20711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
20729679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor
20739679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2074d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2075e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor}
207654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
207760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregortemplate<typename ForwardIterator>
207860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
207960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor                                      NamedDecl *D,
208060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor                                      ForwardIterator first,
208160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor                                      ForwardIterator last) {
208260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  for (; first != last; ++first)
208360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
208460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      return cast<NamedDecl>(*first);
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  return 0;
20878a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne}
20888a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne
20898a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne/// \brief Finds the instantiation of the given declaration context
20908a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne/// within the current instantiation.
20918a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne///
20928a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne/// \returns NULL if there was an error
20938a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter CollingbourneDeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
20948a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne                          const MultiLevelTemplateArgumentList &TemplateArgs) {
209554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
2096b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor    Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
2097b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor    return cast_or_null<DeclContext>(ID);
2098b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  } else return DC;
2099b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor}
2100b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Find the instantiation of the given declaration within the
2102e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor/// current instantiation.
2103090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson///
2104090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson/// This routine is intended to be used when \p D is a declaration
21051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// referenced from within a template, that needs to mapped into the
2106090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson/// corresponding declaration within an instantiation. For example,
2107090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson/// given:
2108090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson///
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \code
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// template<typename T>
211154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor/// struct X {
211260d7b3a319d84d688752be3870615ac0f111fb16John McCall///   enum Kind {
2113e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor///     KnownValue = sizeof(T)
211452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor///   };
211552604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor///
211652604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor///   bool getKind() const { return KnownValue; }
21179ae2f076ca5ab1feb3ba95629099ec2319833701John McCall/// };
2118e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor///
2119b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor/// template struct X<int>;
21200c01d18094100db92d38daa923c95661512db203John McCall/// \endcode
21210c01d18094100db92d38daa923c95661512db203John McCall///
2122b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2123aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2124aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2125aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
21261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this mapping from within the instantiation of X<int>.
212760406bede202b66ebdd98cac0c38d20f9698aecaDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
212860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
212962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  DeclContext *ParentDC = D->getDeclContext();
213060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
213160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2132b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor      ParentDC->isFunctionOrMethod()) {
2133b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // D is a local of some kind. Look into the map of local
21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // declarations to their instantiations.
213562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
21361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2137b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor
213862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2139b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    if (!Record->isDependentContext())
2140a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor      return D;
2141a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2142a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor    // If the RecordDecl is actually the injected-class-name or a "templated"
2143a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor    // declaration for a class template or class template partial
2144a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor    // specialization, substitute into the injected-class-name of the
21457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // class template or partial specialization to find the new DeclContext.
21467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    QualType T;
21477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
21487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
21497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (ClassTemplate) {
21507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      T = ClassTemplate->getInjectedClassNameType(Context);
21517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
21527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
21537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      T = Context.getTypeDeclType(Record);
2154e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
2155e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2156e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2157e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (!T.isNull()) {
21587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // Substitute into the injected-class-name to get the type corresponding
21597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // to the instantiation we want. This substitution should never fail,
21607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // since we know we can instantiate the injected-class-name or we wouldn't
2161e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      // have gotten to the injected-class-name!
2162e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
21637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // instantiation in the common case?
21647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
21667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
21677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      if (!T->isDependentType()) {
21687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
21690d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor        return T->getAs<RecordType>()->getDecl();
21700d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      }
21711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21720d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      // We are performing "partial" template instantiation to create the
21737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // member declarations for the members of a class template
21747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // specialization. Therefore, D is actually referring to something in
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // the current instantiation. Look through the current context,
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // which contains actual instantiations, to find the instantiation of
2177e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      // the "current instantiation" that D refers to.
21780d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      for (DeclContext *DC = CurContext; !DC->isFileContext();
2179e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           DC = DC->getParent()) {
2180e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        if (ClassTemplateSpecializationDecl *Spec
2181e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor              = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2182e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          if (isInstantiationOf(ClassTemplate,
218358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                Spec->getSpecializedTemplate()))
218458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth            return Spec;
218562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      }
218658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
218758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      assert(false &&
218858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth             "Unable to find declaration for the current instantiation");
21897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      return Record;
21907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
21917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2192251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    // Fall through to deal with other dependent record types (e.g.,
21931028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor    // anonymous unions in class templates).
2194251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  }
2195251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2196251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (!ParentDC->isDependentContext())
2197251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return D;
2198251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2199251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
22001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (!ParentDC)
2201251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return 0;
2202251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
22031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ParentDC != D->getDeclContext()) {
22047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We performed some kind of instantiation in the parent context,
22057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so now we need to look into the instantiated parent context to
22067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // find the instantiation of the declaration D.
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    NamedDecl *Result = 0;
22087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (D->getDeclName()) {
22097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
22107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
221162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    } else {
22127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      // Since we don't have a name for the entity we're looking for,
221362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      // our only option is to walk through all of the declarations to
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // find that name. This will occur in a few cases:
22157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      //
22167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      //   - anonymous struct/union within a template
22177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      //   - unnamed class/struct/union/enum within a template
22187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      //
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // FIXME: Find a better way to find these instantiations!
22201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor      Result = findInstantiationOf(Context, D,
2221ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                   ParentDC->decls_begin(),
22227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                   ParentDC->decls_end());
22237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
22247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // UsingShadowDecls can instantiate to nothing because of using hiding.
2226583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D))
2227583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor           && "Unable to find instantiation of declaration!");
2228583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor
2229583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    D = Result;
22307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
22317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return D;
22331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
22347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \brief Performs template instantiation for all implicit template
22361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// instantiations we have seen until this point.
223762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruthvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
22381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  while (!PendingLocalImplicitInstantiations.empty() ||
22397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
224062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingImplicitInstantiation Inst;
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2242a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
2243815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Inst = PendingImplicitInstantiations.front();
2244090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      PendingImplicitInstantiations.pop_front();
2245090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else {
2246090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      Inst = PendingLocalImplicitInstantiations.front();
2247090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      PendingLocalImplicitInstantiations.pop_front();
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
2249090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
22509db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    // Instantiate function definitions
22519db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
2252090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2253090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                            Function->getLocation(), *this,
225472f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            Context.getSourceManager(),
225572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                           "instantiating function definition");
2256090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2257090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      if (!Function->getBody())
22586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
2259ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall      continue;
22609db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    }
22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate static data member definitions.
22636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
22646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
22656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
22666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2267090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                          Var->getLocation(), *this,
22689db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                          Context.getSourceManager(),
2269090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                          "instantiating static data member "
2270090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                          "definition");
2271a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall
2272802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2273802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor  }
2274802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor}
2275a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall