SemaTemplateInstantiateDecl.cpp revision d325daa506338ab86f9dd468b48fd010673f49a6
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
2035126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2047c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2057c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2065126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2075126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2085126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
20946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
21017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2166eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2176eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2186eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2196eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2206eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
2216eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
2226eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
2236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2246eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
2346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
2376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
2386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
2396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2456b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2466b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2486b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2506b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2516b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2526b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               llvm::SmallVector<SourceLocation, 4> &CommaLocs,
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                             ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2686b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              TemplateArgs, CommaLocs,
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              NewArgs);
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
28928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
29028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
29128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
29228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
29328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             TemplateArgs,
29428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             CommaLocs, NewArgs))
29528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
29628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
29728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
29828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
29928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
30028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
30128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
3056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3123d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
313ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
314a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3150a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3160a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3170a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3180a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
321b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3233d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3240a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
325a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis                                 D->getStorageClass());
3263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
330b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
331b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
332b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
333b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
34146460a68f6508775e98c19b4bb8454bb471aac24John McCall
342390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
343390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3456826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3466826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
347449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
34860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
34960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
3547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
3577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
359251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
360251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
361251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
362251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
363cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
364251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
36560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
36660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
36760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3681f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3691f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3701f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3711f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3721f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
3756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
3766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
3776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
3786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                CommaLocs, InitArgs, RParenLoc)) {
3796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
3806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
3816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
3826eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
3836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
3866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
3876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
3886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Expr *Init = (Expr*)(InitArgs.take()[0]);
3896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
3906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     SemaRef.Owned(Init),
3916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     false);
3926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
3936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
3946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
39583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
3966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4006eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4016eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4021f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
40365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
40465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
4053d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4063d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4073d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
411a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
41207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
41307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
41407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
41507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
416a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
41707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
41807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
42607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
435ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
436ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
439ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
444e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
44707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
44807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
453ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
456663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
457663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
458f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
459663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
461d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
462d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
463f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
464f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
466f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
467f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
468f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
47246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
473f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
47802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
47902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl::FriendUnion FU;
48002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
48102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
48202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // parameters into the pattern type.
48332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
48432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
48532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
48632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
48732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    if (!InstTy) return 0;
488fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
48932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    // This assertion is valid because the source type was necessarily
49032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    // an elaborated-type-specifier with a record tag.
49132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    assert(getLangOptions().CPlusPlus0x || InstTy->getType()->isRecordType());
49232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
49332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    FU = InstTy;
494fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle everything else by appropriate substitution.
49602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else {
49702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    NamedDecl *ND = D->getFriendDecl();
49802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    assert(ND && "friend decl must be a decl or a type!");
49902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
500a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // FIXME: We have a problem here, because the nested call to Visit(ND)
501a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // will inject the thing that the friend references into the current
502a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // owner, which is wrong.
503e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    Decl *NewND;
504e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall
505e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall    // Hack to make this work almost well pending a rewrite.
50663644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor    if (ND->getDeclContext()->isRecord()) {
50793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
50893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // templated friend declarations. This doesn't produce a correct AST;
50993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // however this is sufficient for some AST analysis. The real solution
51093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // must be put in place during the pending rewrite. See PR5848.
51193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
51263644fae3970d4146200e4cb25dd9aace34a3398Douglas Gregor    } else if (D->wasSpecialization()) {
5137557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      // Totally egregious hack to work around PR5866
5147557a1348d2821dce126a778aa7acd7a00b814fdDouglas Gregor      return 0;
51593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
516e129d44aab6324aa2094d68730a7843c41a4e45fJohn McCall      NewND = Visit(ND);
51793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
51802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    if (!NewND) return 0;
519c5c54f2c7bbc000dbcaee5e0acec2dbb0c0f0cf8Eli Friedman
52002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FU = cast<NamedDecl>(NewND);
52102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
52402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
52502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall                       D->getFriendLoc());
5265fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
52702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
52802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
529fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
530fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
535ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
538ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
54243d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
54343d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
546b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
547b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
5488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
5498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
554741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
5558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
5568dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
55706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
558b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
55917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
56296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
56396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
56496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
5650ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
5668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
56817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
56917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
5728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
573ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
574ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
576ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
578ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
579ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6003b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
60117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
602b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
6038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
60496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
60596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
60696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
60796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
60896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
60996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
614fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
615c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
616c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
617fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
618fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6236477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6246477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6256477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6266477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6276477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
628ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregornamespace {
629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  class SortDeclByLocation {
630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SourceManager &SourceMgr;
631ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  public:
633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    explicit SortDeclByLocation(SourceManager &SourceMgr)
634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      : SourceMgr(SourceMgr) { }
635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
636ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool operator()(const Decl *X, const Decl *Y) const {
637ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
638ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                 Y->getLocation());
639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  };
641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
643e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
64493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
64593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
646550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
647550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
648550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
649e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
650ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
652d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
653e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
654e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
67193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
67293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
67393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
69293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
69493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
69793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall        << Pattern->getDeclName() << Pattern->getQualifierRange();
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                  /*Complain=*/true,
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                  Sema::TPL_TemplateMatch))
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
724e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
726e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
728f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
732b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
743e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
746f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
747f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
7493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                  Inst->getInjectedClassNameSpecialization(SemaRef.Context));
750f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
751259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
754e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
755259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
756e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
75746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Inst->setAccess(D->getAccess());
758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // First, we sort the partial specializations by location, so
761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // that we instantiate them in the order they were declared.
762ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
763ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
764ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         P = D->getPartialSpecializations().begin(),
765ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = D->getPartialSpecializations().end();
766ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P)
767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    PartialSpecs.push_back(&*P);
768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  std::sort(PartialSpecs.begin(), PartialSpecs.end(),
769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            SortDeclByLocation(SemaRef.SourceMgr));
770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
771ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
774ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
775ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
776e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
778e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
779d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
7807974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
7817974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
790ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
8057974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
8067974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8077974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8087974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
809d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
810550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
811550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
812550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
813550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
814550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
815550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
816d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
817d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
819d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
821a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
822a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
823a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
824a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
825a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
826a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
827a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
828a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
829a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
830a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
831d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
832d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
83346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
83446460a68f6508775e98c19b4bb8454bb471aac24John McCall
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
836d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
83737d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
83937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
840a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
841a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
842e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
843e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
844e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
845e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
846e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall      !(InstTemplate->getFriendObjectKind() &&
847e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall        !D->getTemplatedDecl()->isThisDeclarationADefinition()))
848a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
849a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
850a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // Add non-friends into the owner.
851a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!InstTemplate->getFriendObjectKind())
852a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
853d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
854d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
855d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
856d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
857d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
858d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
859d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
8606c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
8617c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
8627c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
8636c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
8646c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
8656c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
8666c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
867d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
868d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
870741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
871741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
872b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
873b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
874b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
875b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
876b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
877d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
878eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
879eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
880eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
881eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
882eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
883d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
884f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
885d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
88602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
88702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
88802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
88902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
89002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
891d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
892d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
89317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
894d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
895d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
896d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
89702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
89802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
89902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
90002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
90102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9027557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
903a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
904127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
905127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
906127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
907d325daa506338ab86f9dd468b48fd010673f49a6John McCall
908d325daa506338ab86f9dd468b48fd010673f49a6John McCall  bool isFriend;
909d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (FunctionTemplate)
910d325daa506338ab86f9dd468b48fd010673f49a6John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
911d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else
912d325daa506338ab86f9dd468b48fd010673f49a6John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
913d325daa506338ab86f9dd468b48fd010673f49a6John McCall
914127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
915d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (!isFriend && FunctionTemplate && !TemplateParams) {
916127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
918d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
919d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
920828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
924127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
927127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
928127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
929127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
93279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
93379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
93479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
936e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
93721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
93821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
93921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
94121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
942fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
943d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
944d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
945d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
946d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
947d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
948d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
949d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
950d325daa506338ab86f9dd468b48fd010673f49a6John McCall
95168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
95268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
95368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
95468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
95568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
956d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
957d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
958d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
959d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
960d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
961d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
962d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
9637c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
9647c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
965d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
96668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
96702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
96921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
970a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                           D->getStorageClass(),
9710130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
972b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
973d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
974d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
975b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
97602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Function->setLexicalDeclContext(Owner);
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
978e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
979e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
980e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
981838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
98202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
983a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
984a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
985a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
986a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
987a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
988a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
989a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
990a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
991a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
992a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
993a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
994a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
995a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
996a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
997a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
998d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
999a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1000a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1001a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1002a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1003a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1004d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1005d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1006d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1007d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1008d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1009d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1010d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
101166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
101266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1013838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
101466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
101566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1016d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1017d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1019d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
102002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1021a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1022e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1023e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1026e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1027a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10286826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10296826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10306826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1031a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams || !FunctionTemplate) {
1032a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1033a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1034a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
10356826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1036a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1037a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1038a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1039a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1040a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
10416826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
10426826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1043a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1044a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10459f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
10469f54ad4381370c6b771424b53d219e661d6d6706John McCall                                   false, Redeclaration,
1047e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1048e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
1049a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1051d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    NamedDecl *ToFriendD = 0;
10536826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    if (TemplateParams) {
1055a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = cast<NamedDecl>(FunctionTemplate);
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    } else {
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      ToFriendD = Function;
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    }
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
1062d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC->makeDeclVisibleInContext(ToFriendD, /*Recoverable=*/ false);
1063a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1065e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1066e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
10672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1068d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1069d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1070d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
10716b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
10726b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1073d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1076d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
10776b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
1079d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
1080d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
10816b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
10821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
10841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
10856b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10876b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
10886b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
10896b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
10906b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
10916b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
109279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
109379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
109479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
109579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
109648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
10970ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
109821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
109921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
110021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
110221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
11032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
11042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
11052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1106dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1108dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
110917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1110dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1111dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1112dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
111521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                        Name, T, TInfo,
11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
11170130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                        Constructor->isInlineSpecified(), false);
111817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
111917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
112017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
112117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
112217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
112317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
11240130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       T, Destructor->isInlineSpecified(), false);
112565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
112765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
1128183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
112965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
113065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
113165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
113265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
113321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                       T, TInfo,
11340130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
113565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1136dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
113821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                   D->getDeclName(), T, TInfo,
11390130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                   D->isStatic(), D->isInlineSpecified());
1140dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
11416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Method))
1144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1145b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1146d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1147d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1148d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1150d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1151d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1152d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1153d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1154d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1155d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1156d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1157d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1158d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1159d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1160d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1161d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
11621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1163d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1164d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    if (D->isOutOfLine())
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1166d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
116766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
116866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1169838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
117066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
117166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
117266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else {
117366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
11742db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
117566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
117666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
11787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
11797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
11807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
11817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
11845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
11855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1186838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
11875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
11885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
11895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
11902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
11916826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
11926826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1194d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (!FunctionTemplate || TemplateParams) {
11956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, Owner);
11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1197dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1198dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1199dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1200dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
12016826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
12026826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1203dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
12042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
120565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
120665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
12079f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
120865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
120965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
12104ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
12114ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
12124ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
121346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
121446460a68f6508775e98c19b4bb8454bb471aac24John McCall
12156826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
1216a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      !Method->getFriendObjectKind())
1217dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Owner->addDecl(Method);
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12192dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
12202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
12212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1222615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1223dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1224615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1225615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
122603b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
122717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
122803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
122903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1230bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
123165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1232bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1233bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
12346477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
123558e4677a948e80c92deeebbcd3bdd9266adda798John McCall  QualType T;
1236a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
123758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (DI) {
123858e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
123958e4677a948e80c92deeebbcd3bdd9266adda798John McCall                           D->getDeclName());
124058e4677a948e80c92deeebbcd3bdd9266adda798John McCall    if (DI) T = DI->getType();
124158e4677a948e80c92deeebbcd3bdd9266adda798John McCall  } else {
124258e4677a948e80c92deeebbcd3bdd9266adda798John McCall    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
124358e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getDeclName());
124458e4677a948e80c92deeebbcd3bdd9266adda798John McCall    DI = 0;
124558e4677a948e80c92deeebbcd3bdd9266adda798John McCall  }
124658e4677a948e80c92deeebbcd3bdd9266adda798John McCall
124758e4677a948e80c92deeebbcd3bdd9266adda798John McCall  if (T.isNull())
12482dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
12492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
125058e4677a948e80c92deeebbcd3bdd9266adda798John McCall  T = SemaRef.adjustParameterType(T);
12512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Allocate the parameter
125358e4677a948e80c92deeebbcd3bdd9266adda798John McCall  ParmVarDecl *Param
12547a9813ced7455b8a33a807489ca77a4f809c8a73John McCall    = ParmVarDecl::Create(SemaRef.Context,
12557a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          SemaRef.Context.getTranslationUnitDecl(),
12567a9813ced7455b8a33a807489ca77a4f809c8a73John McCall                          D->getLocation(),
125758e4677a948e80c92deeebbcd3bdd9266adda798John McCall                          D->getIdentifier(), T, DI, D->getStorageClass(), 0);
12582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12599351c173cd538f7f7c28af1494ac7e68b815b0e8Anders Carlsson  // Mark the default argument as being uninstantiated.
1260f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor  if (D->hasUninstantiatedDefaultArg())
1261f43d0b37c1d3f529d0f206e786ae6de61c5f364aDouglas Gregor    Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
12620ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor  else if (Expr *Arg = D->getDefaultArg())
12630ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor    Param->setUninstantiatedDefaultArg(Arg);
12640ed093069ae9b000278a2df01f372438d9c63ec1Douglas Gregor
12652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Note: we don't try to instantiate function parameters until after
12662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // we've instantiated the function's type. Therefore, we don't have
12672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // to check for 'void' parameter types here.
126848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
12692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Param;
12702dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
12712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1272e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1273e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1274e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1275e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1276e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1277e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1279e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1280e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1281550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1282e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1283e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1284e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1285e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
12860f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
12870f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1288e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1289550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1290550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1291550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1292550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1293e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1294e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1295e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
129633642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
129733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
129833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
129933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1300a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
130133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
130233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
130333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
130433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
130533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
130633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
130733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
130833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
130933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
131033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
131133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
131233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
131333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
131433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
131533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
131633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
131733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
131833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
131933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
132033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
132133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
132233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
132333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
132433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
132533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
132633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
132733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
132833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1329550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1330550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1331550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1332550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
133333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
133433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
133533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
13360dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
13379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
13389106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
13399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
13409106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
13419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
13429106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
13439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
13449106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
13459106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
13469106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
13479106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
13489106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
13499106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
13509106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
13519106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
13529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
13539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
13549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
13559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
13569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
13579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
13589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
13599106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
13609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
13619106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
13629106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
13639106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
13649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
136548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
136648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
136748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
136848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
136948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
137048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
137148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
137248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
137348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
137448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
137548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
137648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
137748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
137848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1379ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1380ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1381ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1382ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
13839f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
13849f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
13859f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
13869f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
13879f54ad4381370c6b771424b53d219e661d6d6706John McCall
13889f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
13899f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
13909f54ad4381370c6b771424b53d219e661d6d6706John McCall
1391ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1392ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1393ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1394ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1395ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1396ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1397ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1398ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1399ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1400ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1401ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
14029f54ad4381370c6b771424b53d219e661d6d6706John McCall
14039f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
14049f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
14059f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
14069f54ad4381370c6b771424b53d219e661d6d6706John McCall
14079f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
14089f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
14099f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
14109f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
14119f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
14129f54ad4381370c6b771424b53d219e661d6d6706John McCall
14139f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
14149f54ad4381370c6b771424b53d219e661d6d6706John McCall
14159f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
14169f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1417ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1418ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
14199f54ad4381370c6b771424b53d219e661d6d6706John McCall
1420ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1421ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1422ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1423ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14249f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
14259f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
14269f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
14279f54ad4381370c6b771424b53d219e661d6d6706John McCall
1428323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1429323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
14309f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
14319f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
14329f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
14339f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
14349f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
14357c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
14367c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
14379f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
14389f54ad4381370c6b771424b53d219e661d6d6706John McCall
14399f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
14409f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
14419f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
14429f54ad4381370c6b771424b53d219e661d6d6706John McCall
14439f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
14449f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
14459f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1446323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1447323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1448323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
14499f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1450ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1451ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1452ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1453ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1454ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
14559f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
14569f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1457ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1458ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
14607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
14617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
14627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
14637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
14647ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
14657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
14667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
14677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
14687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
14697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
14707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
14717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
14727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
14737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
14747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
14757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
14767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
14777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
14787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1479ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1480ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
14827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
14837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
14847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
14857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
14890dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
14900dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
14910dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14930dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
14940dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
14950dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
14989488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
14997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
15007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
15017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
15027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
15030d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1504ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1505ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
15070dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
15080dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1509ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1510d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
15117e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
15122fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
15132fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
15142fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
15158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
15168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
15178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1518e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1519e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1520e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1521e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1522e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1523e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1524e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1525ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1526e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1527e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1528e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1529e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1530bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1531e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1532e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1533e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1534e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1535bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1536e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
15379148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1538e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1539e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1540e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1541e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1542e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1543e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1544e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1545e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1546e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1547e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1548e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1549e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1550e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1551e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1552e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1553e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1555e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1557ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1558ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1559ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1560ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1561ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1562ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1563ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1564ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1565ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1566ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1567ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1568ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1569ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1570550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1571550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1572550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1573550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1574550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1575ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1576ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1577ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1578ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1579ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1580ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1581ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1582ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1583ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1584833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1585833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1586833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1587833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1588d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1589833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1590d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1591d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1592ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1593d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1594ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1595ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1596ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1597ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1598ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1599ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1600ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1601ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1602ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1603d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1604ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1605ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1606ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1607ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1609ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1610ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1611ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1612ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1613ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1614ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1615ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1616ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1617ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1618ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1619ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1620ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1621ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1622ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1623ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1624ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1625ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1626ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1627ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1628ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1631ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
16343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
16353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
16363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
16373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1638d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1643ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1644ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1645ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1646ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1647ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1648ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1649ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1650ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1652ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1653ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1654ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1657ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1658ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1660ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1661ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1662ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1663ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1664ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1665ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1666ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1667ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1668ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1669ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1670ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1671ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1672d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
16733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1674ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     0);
1675b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1676b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1677b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1678b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1679ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1680ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1681ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1684ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1685ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1688ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
168921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallbool
169021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallSema::CheckInstantiatedParams(llvm::SmallVectorImpl<ParmVarDecl*> &Params) {
169121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  bool Invalid = false;
169221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  for (unsigned i = 0, i_end = Params.size(); i != i_end; ++i)
169321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    if (ParmVarDecl *PInst = Params[i]) {
169421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      if (PInst->isInvalidDecl())
169521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall        Invalid = true;
169621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      else if (PInst->getType()->isVoidType()) {
169721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall        Diag(PInst->getLocation(), diag::err_param_with_void_type);
16985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
169921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall        Invalid = true;
170021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      }
170121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      else if (RequireNonAbstractType(PInst->getLocation(),
170221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                      PInst->getType(),
170321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                      diag::err_abstract_type_in_decl,
170421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                      Sema::AbstractParamType)) {
17055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor        PInst->setInvalidDecl();
170621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall        Invalid = true;
170721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall      }
170821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    }
170921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return Invalid;
171021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall}
17115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
171221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
171321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
171421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
171521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
171621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
171721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
171821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *NewTInfo = SemaRef.SubstType(OldTInfo, TemplateArgs,
171921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                               D->getTypeSpecStartLoc(),
172021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                               D->getDeclName());
172121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
172221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
17235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
172421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  // Get parameters from the new type info.
172521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeLoc NewTL = NewTInfo->getTypeLoc();
172621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
172721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(NewProtoLoc && "Missing prototype?");
172821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i)
172921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    Params.push_back(NewProtoLoc->getArg(i));
17305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
173121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
17325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
17335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
17341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1735e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1736e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1737e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
17391eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1740e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1741e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1742e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
17431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1744cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1745cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1746cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
17471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1749cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1750cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1751cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1752cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1753cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1754cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1755cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
17561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1757cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
17581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1759cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1760bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1761cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1762cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1763f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1764cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1765cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
17680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
17690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
17700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
17710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
17720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
17730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
17740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
17750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
17760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
17770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
17780ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
17790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
17800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
17810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
17820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
17830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
17840ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
17850ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
17860ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
17870ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
17880ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
17890ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
17900ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
17910ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
17920ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
17930ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
17940ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
17950ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
17960ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
17970ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
17980ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
17990ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
18000ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1801ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getNoReturnAttr(),
1802ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                                 Proto->getCallConv()));
18030ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
18040ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1805e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1806e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1807e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
18085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
18095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
18105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
18115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
18125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
18141eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
18155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1816e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1817e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
18205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1821e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1822e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
18235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
18245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
18255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
18265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
18275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1828a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1829a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1830a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1831a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1832b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1833b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1834b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1835b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1836a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1837b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1838b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1839b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1840b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1841b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1842e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1843e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1844e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1845e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1846f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1847b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1848e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1849e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
185054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
185154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
185254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
18536fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1855251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1856251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1857251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1858251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
18591eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
18603b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
18611eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
18621eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
18636fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
18641eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1865e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1866e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1867e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1868e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1869e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1870e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1871e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1872e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1873e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1874e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1875e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1876e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1877e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1878e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1879e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1880e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
18811eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1882e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
18831eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1884d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1885d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
18861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1887d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1889d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
18907ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1891d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1893f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1894f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1895f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor    return;
1896b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
1897b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1898b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1899b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1900b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1901b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1902b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
19031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1904e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1905e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
190654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
190760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
190860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
190960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
191060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
191160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
191260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
191360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
191460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
19151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
191754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
191854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
191954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
192054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
192154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
1922b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
1923b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
1924b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
1925b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
1926b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
1928090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    getTemplateInstantiationArgs(Function);
1929090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
1930090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
1932090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1933090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1934090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
19361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
193754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
1938090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
1939e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
194052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
194152604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
194252604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
19431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
1944e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
1945b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
19460c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
19470c01d18094100db92d38daa923c95661512db203John McCall
1948b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
1949aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
1950aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
1951aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
19521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
195360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
195460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
195560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
195660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
195760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
1958b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
1959b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
19601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
1961b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
19621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1963b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
1964b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1965b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
1966a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
1967a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1968a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
1969a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1970a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
19717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
19727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
19737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
19747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
19757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
19767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
19777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
19787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
19797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
1980e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1981e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1982e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
1983e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
19847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
19857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
19867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
1987e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
1988e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
19897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
19907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
19911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
19937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
19947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
19950d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
19960d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
19971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
19997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
20007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
20011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2003e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
20040d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2005e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2006e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2007e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2008e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2009e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2010e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
20137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2014251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
20151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2016251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2017251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2018251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2019251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2020251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2021251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
20221028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2023251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2024251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
20251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
20277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
20287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
20317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
20327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
20337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
20347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
20357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
20387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
20397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
20407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20421028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2043ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
20447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
20457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
20467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
20477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2048583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2049583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2050583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2051583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
20527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
20537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
20547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
20551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
20577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
20581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
20597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
20601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
20627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
20631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2064a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2065815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2066090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2067090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2068090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2069090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2071090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
20729db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
20739db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2074090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2075090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
207672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
207772f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2078090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2079090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
20806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2081090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
20829db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
20831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
20856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
20866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
20876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
20886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2089090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
20909db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2091090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2092090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2093a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2094802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2095802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2096802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2097a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
20989db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2099802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2100802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2101802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2102802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2103a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2105090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2106802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2107090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2108090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2109090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
21109988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
21111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21129988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
21139988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
21147c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21157c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
21169988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
21177c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2119e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
21201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2122090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2123090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2124802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2125090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2126090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2127090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
21289db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
21299db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2130090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
21319db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2132090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2135090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2136090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
21381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2139090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
21401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
2141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
21439db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
21449db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2145090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
214752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
214852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
214952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
215052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
215152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
215252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
215352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
215452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
215552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
215652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
215752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
215852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
215952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
216052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
216152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
21620d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
21630d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
21640d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
21650d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
21660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
21670d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
21680d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
21690d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
21700d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
21710d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
21720d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
21730d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
21740d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2175ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2176ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2177ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2178ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2179ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2180ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2181ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2182ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2183ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2184ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2185ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2186ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2187ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2188ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2189ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2190ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
219152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
219252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
219352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
219452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
219552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
219652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
219752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
219852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
219952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
220052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
220152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
220252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
220352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
220452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
220552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
220652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
220752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
220852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
220952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
221052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
221152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
221252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
221352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
221452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
221552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
221652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
221752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
221852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
221952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
222052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
222152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
222252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
222352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
222452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
222552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
222652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
222752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
222852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
222952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2230ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2231ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2232ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2233ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2234ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2235ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2236ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2237ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2238ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2239ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2240ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2241ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
22437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
22447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2245ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
22467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
22477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
22490d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
22500d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2251ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
22520d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
22530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
225452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
225552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
225652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
225752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
225852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
225952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
226052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
226152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
226252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
226352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
226452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
226552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
226652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
226752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
226852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2269ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2270ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2271815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
22720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
22737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
22747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
22757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
22767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
22777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
22787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
22797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
22817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
22820d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
22830d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
22840d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
22850d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2286815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
22870d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
22880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
22891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
229052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
229152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
22921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
229352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
229452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2295815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
229652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
229752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2298815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
22997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
230052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
230152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
230252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
230352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
230452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2305a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
23060d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
23070d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
23080d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2309ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2310ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2311ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2312ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2314d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2315d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2316d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
23171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2318d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2319d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2320d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
23211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2322ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2323ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2324ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2325ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2326ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2327ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2328815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2329815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2330815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2331815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2332815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
23331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2334815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2335815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2336815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2337815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2338815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2339815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2340815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2341815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2342815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2343815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
234402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
234502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
234602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
234702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
23487c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2349e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
235002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
23517c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
235202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
235302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
235402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
235502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2356ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2357ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2358815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2359815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2360815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2361815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2362815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2363815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2364815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2365815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2366815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2367815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2368815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2369815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2370815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2371815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2372815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2374815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2375815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2376815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2377815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2378815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2379815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2380ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2381ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
23827c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2383e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2384815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2385550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
23866d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2387550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
23882bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
23892bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
23902bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
23912bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2392815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2393e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2394e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2395e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2396e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
23978b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
23988b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
23998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
24008b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
24018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2402e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2403e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2404e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2405e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
24063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = ClassTemplate->getInjectedClassNameSpecialization(Context);
2407e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2408e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2409e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
24103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
24123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
24133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
24143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
24153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
24163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = cast<InjectedClassNameType>(T)->getUnderlyingType();
24173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2418e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2419e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
24208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
24218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
24228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
24238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
24248b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
24258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
24268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
24278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
24288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2429e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2430e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2431e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2432e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2433e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2434e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2435e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2436e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
24378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
24388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
24398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
24408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
24418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
24428b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
24438b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
24448b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
24451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
244652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
24471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
24488b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2449e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2450e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
245152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
24528b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
24538b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
24548b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
245552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
245652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
24578b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
24588b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
24598b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
24608b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
24618b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
24628b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
24638b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
246452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
24658b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
246652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
24678b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2468e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2469e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2470e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
247152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2472e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2473e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2474e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
24757c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
24761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
247744c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
24781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2479815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2480815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2481815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2482815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
24837c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
24843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
24853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
24863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
24877c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
24887c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
24893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
24903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
24913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
24923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
24933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
24947c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
24957c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
24967c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2497815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2498815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
249917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2500815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2501815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2502815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2503815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2504815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2505815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2506815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2507815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2508815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2509815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
25101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
251117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
251217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2513815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
25141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25159f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
251600225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
251700225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
25189f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
25199f54ad4381370c6b771424b53d219e661d6d6706John McCall
2520815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2521815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2522815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2523815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2524815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2525d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
25261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2527d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
252860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
252960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
253060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
253160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
253260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
253360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
253460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
253560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
253660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
253760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
253860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
253960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
25401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
25427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
25431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2544c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2545c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2546c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
25471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25486fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2549b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
25507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
25517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
25521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
25547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
25557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2556c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2557291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2558291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2559291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2560291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2561291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2562291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2563291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2564291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2565291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2566291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2567291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2568291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2569291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2570291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2571291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2572291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2573291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2574291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
25751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2576c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2577c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2578c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2579c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
25801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2582d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2583d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
25840c01d18094100db92d38daa923c95661512db203John McCall
25850c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
25860c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
25870c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
25880c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
25890c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
25900c01d18094100db92d38daa923c95661512db203John McCall
25910c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
25920c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
25930c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
25940c01d18094100db92d38daa923c95661512db203John McCall      break;
25950c01d18094100db92d38daa923c95661512db203John McCall    }
25960c01d18094100db92d38daa923c95661512db203John McCall  }
25970c01d18094100db92d38daa923c95661512db203John McCall}
2598