SemaTemplateInstantiateDecl.cpp revision 6920cdce298ac9ba50dc7ebb7dea982a300b0664
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
137d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall#include "Lookup.h"
14aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
180c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
20a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
22c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
2383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
2885b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramer  class TemplateDeclInstantiator
29b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
32d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    void InstantiateAttrs(Decl *Tmpl, Decl *New);
35d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
40d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
417e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
44390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
45390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
464f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
474f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
483dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
546477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
56a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
57a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
58d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
59d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
60d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
61615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
63bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
646477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
65e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
667974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
677974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
68d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
69e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
7033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
719106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
73ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
74ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
89fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
90fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
91fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
92fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
9421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
96e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
98e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
99e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
100ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
101b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
102b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const DeclaratorDecl *OldDecl,
103b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        DeclaratorDecl *NewDecl);
104b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const TagDecl *OldDecl,
105b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        TagDecl *NewDecl);
106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
145d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
146d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlssonvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
147d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
148d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
149d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
150d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
151d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
152d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
153d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
154d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
155d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
156d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1574f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1584f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1594f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1604f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1614f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1624f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1634f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1644f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1654f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1664f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1674f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1684f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1693dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1703dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1713dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1723dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1733dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1743dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1753dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1763dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1773dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1783dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
186a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
187ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
188ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
189ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
190ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
192a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
199ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
203d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
204d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
205d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
206d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
207d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
208d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
209d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
210d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
211d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
2125126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2137c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2147c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2155126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2165126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2175126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
218d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
21946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
22017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
2316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
2326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
2336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
2446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
2476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2686b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               llvm::SmallVector<SourceLocation, 4> &CommaLocs,
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                             ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              TemplateArgs, CommaLocs,
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              NewArgs);
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
29928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
30028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
30128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
30228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
30328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             TemplateArgs,
30428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             CommaLocs, NewArgs))
30528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
30628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
30728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
30828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
30928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
31028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
31128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
323ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
324a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3250a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3260a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3270a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3280a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
331b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3333d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3340a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
33516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
33616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3393d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
342b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
343b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
344b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35146460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
35246460a68f6508775e98c19b4bb8454bb471aac24John McCall
353390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
354390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3566826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3576826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
358449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
35960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
36060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
3657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
3687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
370251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
371251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
372251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
373251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
374cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
375251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
37660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
37760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
37860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3791f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3801f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3811f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3821f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3831f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
3866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
3876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
3886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
3896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                CommaLocs, InitArgs, RParenLoc)) {
3906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
3916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
3926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
3936eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
3946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
3976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
3986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
3996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Expr *Init = (Expr*)(InitArgs.take()[0]);
4006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
4016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     SemaRef.Owned(Init),
4026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     false);
4036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
4046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
4056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
40683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4076eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4116eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4126eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4131f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
41465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
41565b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
4163d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4173d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4183d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
422a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
42307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
42407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
42507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
42607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
427a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
42807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
42907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
43707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
446ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
447ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
450ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
455e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
45807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
45907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
464ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
467663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
468663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
469f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
470663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
472d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
473d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
474f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
475f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
478f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
479f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
482f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
48346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
484f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
48902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
49002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
49106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
49232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
49332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
49432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
49532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
49606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
49706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
498fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
49906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
50006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
50106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
50206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
50306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
50406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
50506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
50606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
50706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
50806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
50906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
51032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
511af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
512af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
513af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
514af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
515af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
51606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
51906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
52006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5215fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
52202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
52302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
524fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
525fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
530ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
533ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
53743d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
53843d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
541b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
542b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
5438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
5448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
549741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
5508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
5518dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
55206c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
553b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
55417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
55796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
55896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
55996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
5600ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
5618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
56317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
56417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
5678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
568ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
569ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
571ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
573ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
574ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
5953b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
59617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
597b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
59996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
60096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
60196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
60296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
60396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
60496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
608c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
609fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
610c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
611c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
612fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
613fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6186477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6196477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6206477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6216477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6226477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
623e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
62493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
62593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
626550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
627550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
628550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
629e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
630ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
632d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
633e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
634e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
63593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
63693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
63793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
63893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
63993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
64093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
64193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
64293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
64393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
64493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
64593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
64693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
64793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
64893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
64993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
65093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
65193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
65293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
65393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
65493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
67093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
67193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
67293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
67393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
6821eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
6831eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
687c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
689c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
690c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
691c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
692c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
693c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
694c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
695c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
696c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
697c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
698c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
699c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
700c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
701c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
702c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
703c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
704c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
705c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
706c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
707c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
708c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
709c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
710c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
711c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
712c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
713c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
714c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
720c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
721c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
722c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
723c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
724c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
725c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
727c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
731c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
732c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
73793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
742f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
746b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
74993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
75093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
752ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
754ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
755ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
756ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
757ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
758ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
76093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
76193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
763e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
766f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
767f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
7693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                  Inst->getInjectedClassNameSpecialization(SemaRef.Context));
770ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
771259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
774e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
775259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
776e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
780ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
781dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
782dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  D->getPartialSpecializations(PartialSpecs);
783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
786e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
787e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
789d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
7907974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
7917974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
8157974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
8167974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8177974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8187974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
819d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
820550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
821550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
822550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
823550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
824550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
825895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
826d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
827d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
829d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
831a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
832a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
833a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
834a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
835a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
836a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
837a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
839a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
840a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
841d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
842d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
84346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
84446460a68f6508775e98c19b4bb8454bb471aac24John McCall
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
846d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
84737d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
848a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
84937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
850a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
851a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
852e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
853b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
854b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
855e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
856e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
857e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
858b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
859a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
860a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
861b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
862b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
863a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
864b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
865d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
866d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
867d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
868d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
869d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
870d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
871d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
8726c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
8737c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
8747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
8756c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
8766c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
8776c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
8786c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
879d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
880d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
882741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
883741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
884b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
885b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
886b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
887b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
888b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
889d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
890eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
891eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
892eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
893eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
894eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
895d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
896f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
897d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
89802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
89902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
90002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
90102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
90202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
903d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
904d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
90517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
906d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
907d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
908d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
90902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
91002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
91102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
91202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
91302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9147557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
915a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
916127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
917127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
918127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
919127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
920b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
921127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
923d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
924d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
925828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
9261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
929127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
931127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
932127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
933127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
934127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
936b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
937b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
938b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
939b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
940b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
941b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
94279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
94379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
94479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
94579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
947e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
94821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
94921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
95021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
95221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
953fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
954d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
955d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
956d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
957d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
958d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
959d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
960d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
961d325daa506338ab86f9dd468b48fd010673f49a6John McCall
96268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
96368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
96468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
96568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
96668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
967d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
968d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
969d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
970d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
971d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
972d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
973d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
9747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
9757c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
976d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
97768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
97802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
98021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
98116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
9820130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
983b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
984d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
985d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
986b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
987b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
988b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
989b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
990b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
991b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
992b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
993b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
995e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
996e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
997e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
998838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
99902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1000a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1001a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1002a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1003a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1004a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1005a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1006a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1007a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1008a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1009a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1010a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1011a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1012a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1013a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1014a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1015d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1016a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1017a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1018a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1019a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1020b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1021b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1022d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1023d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1024d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1026d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1027d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1028d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
102966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
103066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1031838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
103266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
103366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1034d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1035d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1036d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1037d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
103802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1039a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1040e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1041e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1043e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1044e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1045af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1046a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10476826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10486826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10496826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1050af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1051af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1052af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1053af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1054af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1055af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1056af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1057af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1058af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1059af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1060af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1061af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1062af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1063af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1064af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1065af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1066af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1067af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1068af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1069af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1070af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1071af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1072af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1073af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1074af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1075af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1076af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1077af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1078af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1079af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1080af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1081af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1082af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1083af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1084af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1085af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1086a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1087a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1088a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
10896826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1090a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1091a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1092a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1093a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1094a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
10956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
10966826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1097a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1098a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10999f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1100af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1101e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1102e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
110376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
110476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
110576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
110676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1107a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1108a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1109d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11106826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
111176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1112a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
111376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1114a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
111576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
111676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
111776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1118a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1119a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
112076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
112176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
112276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
112376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1124e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1125e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
11262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1127d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1128d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1129d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
11306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
11316b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1132d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1135d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
11366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
1138d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
1139d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
11406b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
11446b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11466b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
11476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
11486b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
11496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
11506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1151b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1152b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1153b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1154b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1155b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1156b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
115779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
115879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
115979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
116079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
116148dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
11620ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
116321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
116421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
116521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
116721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
11682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1169b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1170b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1171b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1172b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1173b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1174b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1175b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1176b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1177b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1178b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1179b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1180b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1181b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1182b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1183b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1184b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1185b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1186b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1187b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1188b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1189b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1190b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1191b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
11922dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1193b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1194dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1196dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
119717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1198dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1199dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1200dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
120321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                        Name, T, TInfo,
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
120516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
120616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
120717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
120817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
120917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
121017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
121117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
121217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
121316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       T, Destructor->isInlineSpecified(),
121416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
121565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
121765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
1218183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
121965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
122065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
122165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
122265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
122321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                       T, TInfo,
12240130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
122565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1226dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
122821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                   D->getDeclName(), T, TInfo,
122916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
123016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
123116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1232dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
12336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1234b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1235b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1236b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1237d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1238d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1239d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1241d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1242d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1243d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1244d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1245d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1246d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1247d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1248d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1249d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1250d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1251d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1252d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1254d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1255b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1256b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1257b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1258b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
12591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1260d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
126166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
126266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1263838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
126466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
126566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1266b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
126766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
12682db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
126966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
127066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
12727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
12737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1274b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1275b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1276b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
12787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
12815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
12825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1283838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
12845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
12865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
12872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12886826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
12896826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1291b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1292b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1294dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1295dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1296dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1297dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
12986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
12996826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1300dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
130265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
130365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
13049f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
130565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
130665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
13074ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
13084ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
13094ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
131046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
131146460a68f6508775e98c19b4bb8454bb471aac24John McCall
1312b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1313b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1314b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1316b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1317b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1318b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1319b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1320b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1321b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1322b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1323b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1324b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
13251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
13272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
13282dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1329615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1330dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1331615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1332615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
133303b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
133417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
133503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
133603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1337bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
133865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1339bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1340bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
13416477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1342cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
13432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
13442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1345e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1346e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1347e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1348e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1349e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1350e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1352e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1353e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1354550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1355e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1356e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1357e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1358e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
13590f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
13600f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1361e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1362550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1363550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1364550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1365550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1366e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1367e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1368e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
136933642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
137033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
137133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
137233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1373a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
137433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
137533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
137633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
137733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
137833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
137933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
138033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
138133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
138233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
138333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
138433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
138533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
138633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
138733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
138833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
138933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
139033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
139133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
139233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
139333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
139433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
139533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
139633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
139733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
139833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
139933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
140033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
140133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1402550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1403550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1404550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1405550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
140633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
140733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
140833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
14090dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
14109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
14119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
14129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
14139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
14149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
14159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
14169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
14179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
14189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
14199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
14209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
14219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
14229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
14239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
14259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
14269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
14279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
14289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
14299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
14309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
14329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
14339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
14349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
14369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
14379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
143848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
143948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
144048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
144148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
144248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
144348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
144448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
144548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
144648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
144748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
144848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
144948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
145048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
145148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1452ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1453ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1454ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1455ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14569f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
14579f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
14589f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
14599f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
14609f54ad4381370c6b771424b53d219e661d6d6706John McCall
14619f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
14629f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
14639f54ad4381370c6b771424b53d219e661d6d6706John McCall
1464ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1465ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1466ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1467ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1468ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1469ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1470ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1471ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1472ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1473ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1474ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
14759f54ad4381370c6b771424b53d219e661d6d6706John McCall
14769f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
14779f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
14789f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
14799f54ad4381370c6b771424b53d219e661d6d6706John McCall
14809f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
14819f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
14829f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
14839f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
14849f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
14859f54ad4381370c6b771424b53d219e661d6d6706John McCall
14869f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
14879f54ad4381370c6b771424b53d219e661d6d6706John McCall
14889f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
14899f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1490ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1491ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
14929f54ad4381370c6b771424b53d219e661d6d6706John McCall
1493ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1494ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1495ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1496ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14979f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
14989f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
14999f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
15009f54ad4381370c6b771424b53d219e661d6d6706John McCall
1501323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1502323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
15039f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
15049f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
15059f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
15069f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
15079f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
15087c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
15097c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
15109f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
15119f54ad4381370c6b771424b53d219e661d6d6706John McCall
15129f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
15139f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
15149f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
15159f54ad4381370c6b771424b53d219e661d6d6706John McCall
15169f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
15179f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
15189f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1519323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1520323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1521323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
15229f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1523ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1524ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1525ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1526ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1527ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
15289f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
15299f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1530ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1531ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
15337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
15347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
15357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
15367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
15377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
15387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
15397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
15407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
15427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
15437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
15447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
15467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
15477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
15487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
15497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
15507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
15517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1552ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1553ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
15557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
15567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
15587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
15620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
15630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
15640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
15670dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
15680dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
15719488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
15727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
15737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
15747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
15757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
15760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1577ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1578ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
15800dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
15810dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1582ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1583d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
15847e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
15852fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
15862fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
15872fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
15888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
15898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
15908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1591e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1592e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1593e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1594e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1595e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1596e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1597e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1598ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1599e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1600e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1601e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1602e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1603bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1604e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1605e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1606e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1607e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1608bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1609e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
16109148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1611e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1612e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1613e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1614e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1615e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1616e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1618e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1619e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1620e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1621e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1622e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1623e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1624e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1625e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1626e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
16271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1628e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1631ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1636ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1637ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1638ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1643550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1644550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1645550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1646550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1647550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1648ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1649ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1650ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1652ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1653ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1654ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1657833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1658833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1659833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1660833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1661d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1662833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1663d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1664d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1665ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1666d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1667ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1668ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1669ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1670ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1671ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1672ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1673ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1674ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1675ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1676d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1677ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1678ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1679ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1680ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1681ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1684ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1685ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1688ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1689ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1690ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1691ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1692ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1693ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1694ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1695ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1696ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1697ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1698ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1699ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1700ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1701ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1702ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1703ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1704ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1705ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1706ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
17073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
17083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
17093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
17103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1711d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1712ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1713ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1714ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1715ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1716ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1717ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1718ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1719ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1720ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1722ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1723ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1724ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1725ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1726ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1739ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1742ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1743ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1744ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1745d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
17463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1747dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1748dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                             ClassTemplate->getPartialSpecializations().size());
1749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1750b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1751b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1752b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1753ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1754ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1755ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1756ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1757ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1758ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1762ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
176321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
176421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
176521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
176621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
176721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
176821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
17696cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
17706cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
17716cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
17726cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
177321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
177421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
17755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1776cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1777cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1778895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
17796920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
17806920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
17816920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      TypeLoc NewTL = NewTInfo->getTypeLoc();
17826920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
17836920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
17846920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
17856920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
17866920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
17876920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1788895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1789895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
17906920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1791895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1792cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1793cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1794cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1795cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1796cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
17976920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
17986920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
17996920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
18006920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
18016920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
18026920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
18036920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
18046920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1805cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1806cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
180721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
18085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
18095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1811e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1812e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1813e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
18141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
18151eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1816e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1817e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1818e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
18191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1820cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1821cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1822cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
18241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1825cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1826cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1827cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1828cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1829cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1830cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1831cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
18321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1833cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
18341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1835cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1836bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1837cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1838cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1839f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1840cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1841cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
18421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
18440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
18450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
18470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
18480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
18490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
18500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
18510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
18520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
18530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
18540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
18550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
18560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
18570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
18580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
18590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
18610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
18620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
18640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
18660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
18670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
18680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
18690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
18700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
18710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
18720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
18730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
18740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
18750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
18760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1877264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 Proto->getExtInfo()));
18780ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
18790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1881e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1882e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
18835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
18845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
18855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
18865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
18875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
18891eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
18905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1891e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1892e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
18931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
18955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1896e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1897e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
18985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
18995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
19005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
19015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
19025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1903a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1904a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1905a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1906a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1907b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1908b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1909b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1910b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1911a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1912b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1913b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1914b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1915b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1916b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1917e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1918e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1919e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1920e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1921f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1922b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1923e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1924e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
192554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
192654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
192754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
19286fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
19291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1930251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1931251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1932251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1933251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
19341eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
19353b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
19361eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
19371eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
19386fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
19391eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1940e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1941e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1942e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1943e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1944e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1945e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1946e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1947e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1948e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1949e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1950e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1951e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1952e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1953e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1954e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1955e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
19561eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1957e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
19581eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1959d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1960d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
19611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1962d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
19631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1964d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
19657ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1966d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
19671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1968f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1969f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1970f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1971b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1972b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1973b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1974b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1975b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1976b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1977b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1979e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1980e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
198154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
198260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
198360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
198460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
198560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
198660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
198760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
198860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
198960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
19901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
199154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
199254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
199354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
199454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
199554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
199654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1997b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1998b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1999b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2000b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2001b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2003090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
2004090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2005090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
20061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2007090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2008090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2009090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
20101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
20111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
2013090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
2014e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
201552604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
201652604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
201752604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
20181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
2019e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2020b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
20210c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
20220c01d18094100db92d38daa923c95661512db203John McCall
2023b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2024aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2025aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2026aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
20271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
202860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
202960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
203060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
203160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
203260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2033b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
2034b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
20351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
2036b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2038b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
2039b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2040b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2041a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2042a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2043a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2044a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2045a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
20467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
20477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
20487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
20497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
20507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
20517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
20527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
20537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
20547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2055e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2056e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2057e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2058e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
20597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
20607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
20617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2062e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2063e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
20647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
20657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
20687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
20697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
20700d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
20710d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
20721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20730d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
20747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
20757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
20771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2078e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
20790d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2080e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2081e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2082e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2083e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2084e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2085e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
20887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2089251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
20901028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2091251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2092251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2093251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2094251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2095251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2096251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
20971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2098251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2099251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
21001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
21027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
21037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
21067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
21077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
21087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
21097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
21107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
21111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
21137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
21147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
21157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
21161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21171028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2118ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
21197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
21207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
21217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
21227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2123583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2124583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2125583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2126583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
21277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
21287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
21297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
21301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
21327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
21331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
21347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
21351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
21377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
21381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2139a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2140815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2142090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2144090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
21451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
21479db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
21489db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
215172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
215272f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2154090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
21556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
21579db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
21606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
21616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
21626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
21636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2164090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
21659db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2166090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2168a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2169802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2170802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2171802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2172a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
21739db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2174802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2175802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2176802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2177802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2178a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
21791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2180090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2181802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2182090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2183090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2184090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
21859988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
21861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21879988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
21889988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
21897c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21907c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
21919988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
21927c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21937c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2194e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2197090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2198090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2199802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2200090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2201090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2202090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
22039db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
22049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2205090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
22069db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2207090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2208090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
22091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2210090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2211090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2212090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2214090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
2216090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2217090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
22189db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
22199db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2220090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2221090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
222252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
222352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
222452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
222552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
222652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
222752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
222852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
222952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
223052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
223152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
223252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
223352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
223452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
223552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
223652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
22370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
22380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
22390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
22400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
22410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
22420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
22430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
22440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
22450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
22460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
22470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
22480d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
22490d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2250ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2251ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2252ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2253ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2254ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2255ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2256ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2257ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2258ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2259ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2260ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2261ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2262ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2264ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2265ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
226652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
226752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
226852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
226952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
227052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
227152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
227252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
227352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
227452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
227552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
227652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
227752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
227852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
227952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
228052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
228152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
228252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
228352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
228452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
228552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
228652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
228752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
228852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
228952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
229052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
229152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
229252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
229352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
229452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
229552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
229652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
229752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
229852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
229952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
230052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
230152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
230252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
230352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
230452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2305ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2306ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2307ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2308ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2309ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2310ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2311ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2312ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2313ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2314ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2315ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2316ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
23177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
23187ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
23197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2320ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
23217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
23227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
23237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
23240d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
23250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2326ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
23270d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
23280d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
232952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
233052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
233152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
233252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
233452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
233652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
233752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
233852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
233952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
234052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
234152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
234252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
234352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2344ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2345ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
23470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
23487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
23497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
23507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
23517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
23527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
23537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
23547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
23557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
23567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
23570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
23580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
23590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
23600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2361815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
23620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
23630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
23641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
236652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
236952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2370815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
237152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
237252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
23747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
237552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
237652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
237752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
237852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
237952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2380a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
23810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
23820d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
23830d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2384ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2386ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2387ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2389d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2390d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2391d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
23921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2393d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2394d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2395d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2397ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2398ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2399ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2400ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2401ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2402ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2403815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2404815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2405815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2406815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2407815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
24081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2409815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2410815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2411815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2412815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2413815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2414815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2415815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2416815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2417815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2418815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
241902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
242002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
242102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
242202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
24237c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2424e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
242502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
24267c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
242702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
242802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
242902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
243002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2431ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2432ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2433815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2434815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2435815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2436815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2437815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2438815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2439815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2440815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2441815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2442815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2443815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2444815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2445815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2446815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2447815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2448815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2449815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2450815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2451815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2452815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2453815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2454815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2455ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2456ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
24577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2458e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2459815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2460550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
24616d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2462550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
24632bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
24642bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
24652bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
24662bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2467815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2468e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2469e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2470e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2471e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
24728b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
24738b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
24748b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
24758b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
24768b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2477e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2478e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2479e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2480e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
24813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = ClassTemplate->getInjectedClassNameSpecialization(Context);
2482e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2483e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2484e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
24853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
24873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
24883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
24893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
24903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
249131f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
24923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2493e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2494e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
24958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
24968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
24978b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
24988b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
24998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
25008b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
25018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
25028b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
25038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2504e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2505e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2506e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2507e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2508e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2509e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2510e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2511e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25128b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
25138b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
25148b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
25158b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
25168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
25178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
25188b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
25198b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
25201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
252152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
25221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
25238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2524e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2525e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
252652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
25278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
25288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
25298b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
253052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
253152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
25328b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
25338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
25348b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
25358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
25368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
25378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
25388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
253952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
25408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
254152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
25428b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2543e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2544e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2545e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
254652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2547e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2548e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2549e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25507c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
25511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
255244c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2554815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2555815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2556815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2557815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
25587c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
25593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
25603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
25613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
25627c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
25637c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
25643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
25653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
25663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
25673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
25683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
25697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
25707c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
25717c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2572815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2573815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
257417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2575815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2576815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2577815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2578815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2579815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2580815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2581815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2582815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2583815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2584815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
25851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
258617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
258717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2588815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
25891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25909f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
259100225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
259200225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
25939f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
25949f54ad4381370c6b771424b53d219e661d6d6706John McCall
2595815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2596815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2597815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2598815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2599815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2600d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
26011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2602d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
260360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
260460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
260560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
260660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
260760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
260860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
260960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
261060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
261160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
261260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
261360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
261460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
26151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
26177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
26181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2619c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2620c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2621c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
26221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26236fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2624b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
26257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
26267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
26271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
26297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
26307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2631c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2632291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2633291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2634291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2635291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2636291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2637291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2638291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2639291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2640291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2641291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2642291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2643291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2644291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2645291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2646291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2647291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2648291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2649291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
26501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2651c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2652c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2653c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2654c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
26551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2657d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2658d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
26590c01d18094100db92d38daa923c95661512db203John McCall
26600c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
26610c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
26620c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
26630c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
26640c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
26650c01d18094100db92d38daa923c95661512db203John McCall
26660c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
26670c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
26680c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
26690c01d18094100db92d38daa923c95661512db203John McCall      break;
26700c01d18094100db92d38daa923c95661512db203John McCall    }
26710c01d18094100db92d38daa923c95661512db203John McCall  }
26720c01d18094100db92d38daa923c95661512db203John McCall}
2673