SemaTemplateInstantiateDecl.cpp revision e7089b0c6ffe8a8854150b60df00fb544099f77d
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "Sema.h"
137d384dd5ace9ae9a22a69e700d2cacb256bc6c69John McCall#include "Lookup.h"
14aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
180c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
20a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
22c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson#include "clang/Basic/PrettyStackTrace.h"
2383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
2885b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramer  class TemplateDeclInstantiator
29b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
32d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    void InstantiateAttrs(Decl *Tmpl, Decl *New);
35d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
40d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
417e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
44390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
45390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // clang/AST/DeclNodes.def.
464f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
474f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
483dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
546477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
56a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
57a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
58d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
59d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
60d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
61615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
63bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
646477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
65e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
667974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
677974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
68d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
69e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
7033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
719106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
73ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
74ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
89fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
90fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
91fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
92fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
9421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
96e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
98e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
99e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
100ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
101b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
102b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const DeclaratorDecl *OldDecl,
103b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        DeclaratorDecl *NewDecl);
104b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const TagDecl *OldDecl,
105b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        TagDecl *NewDecl);
106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
145d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
146d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlssonvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
147d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
148d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
149d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
150d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
151d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
152d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
153d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
154d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
155d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
156d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1574f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1584f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1594f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1604f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1614f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1624f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1634f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1644f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1654f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1664f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1674f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1684f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1693dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1703dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1713dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1723dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1733dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1743dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1753dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1763dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1773dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1783dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
186a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
187ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall  if (DI->getType()->isDependentType()) {
188ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
189ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
190ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
192a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
199ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
203d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
204d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
205d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
206d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
207d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
208d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
209d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
210d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
211d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
2125126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2137c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2147c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2155126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2165126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2175126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
218d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
21946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
22017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2256eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2286eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
2316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
2326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
2336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
2446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
2476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2686b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               llvm::SmallVector<SourceLocation, 4> &CommaLocs,
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                             ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              TemplateArgs, CommaLocs,
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              NewArgs);
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
29928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
30028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
30128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
30228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
30328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             TemplateArgs,
30428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             CommaLocs, NewArgs))
30528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
30628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
30728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
30828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
30928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
31028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
31128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
323ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
324a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3250a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3260a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3270a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3280a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
331b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3333d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3340a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
33516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
33616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3393d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
342b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
343b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
344b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35146460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
35246460a68f6508775e98c19b4bb8454bb471aac24John McCall
353390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
354390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3566826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3576826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
358449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
35960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
36060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    D->getLexicalDeclContext()->addDecl(Var);
3657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
368f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor
369f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
370f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
373251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
374251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
375251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
376251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
377cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
378251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
37960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
38060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
38160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3821f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3831f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3841f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3851f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3861f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
3896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
3906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
3916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
3926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                CommaLocs, InitArgs, RParenLoc)) {
3936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
3946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
3956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
3966eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
3976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3986eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3996eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
4006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
4016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
4026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Expr *Init = (Expr*)(InitArgs.take()[0]);
4036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
4046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     SemaRef.Owned(Init),
4056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     false);
4066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
4076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
4086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
40983ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4106eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4146eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4161f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
41765b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
41865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
4193d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4213d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
425a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
42607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  if (DI->getType()->isDependentType())  {
42707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
42807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
42907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
430a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
43107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
43207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
44007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
4438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
449ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
450ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
453ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
458e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
46107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
46207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
467ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
470663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
471663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
472f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
473663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
476d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
477f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
478f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
481f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
482f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
485f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
48646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
487f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
49202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
49302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
49406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
49532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
49632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
49732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
49832f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
49906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
50006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
501fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
50206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
50306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
50406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
50506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
50606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
50706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
50806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
50906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
51006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
51106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
51206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
51332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
514af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
515af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
516af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
517af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
518af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
51906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
52206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
52306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5245fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
52502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
52602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
527fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
528fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
533ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
536ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
54043d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
54143d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
544b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
545b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
5468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
5478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
552741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
5538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
5548dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
55506c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
55717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
56096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
56196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
56296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
5630ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
5648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
56617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
56717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
5708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
571ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
572ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
574ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
576ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
577ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
5908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
5983b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
59917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
600b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
6018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
60296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
60396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
60496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
60596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
60696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
60796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
612fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
613c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
614c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
615fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
616fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6216477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6226477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6236477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6246477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6256477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
626e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
62793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
62893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
629550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
630550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
631550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
632e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
633ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
635d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
636e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
637e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
63893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
63993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
64093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
64193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
64293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
64393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
64493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
64593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
64693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
64793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
64893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
64993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
65093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
65193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
65293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
65393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
65493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
67093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
67293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
67393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
6851eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
6861eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
690c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
692c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
693c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
694c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
695c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
696c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
697c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
698c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
699c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
700c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
701c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
702c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
703c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
704c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
705c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
706c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
707c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
708c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
709c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
710c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
711c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
712c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
713c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
714c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
715c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
716c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
717c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
723c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
724c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
725c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
727c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
728c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
729c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
730c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
734c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
735c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
73793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
745f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
74793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
75193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
755ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
757ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
758ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
759ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
760ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
761ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
766e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
769f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
770f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
7723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                  Inst->getInjectedClassNameSpecialization(SemaRef.Context));
773ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
774259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
777e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
778259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
779e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
780e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
784dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
785dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  D->getPartialSpecializations(PartialSpecs);
786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
791e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
792d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
7937974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
7947974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
8187974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
8197974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8207974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
822d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
823550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
824550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
825550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
826550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
827550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
828895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
829d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
830d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
832d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
834a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
835a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
836a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
837a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
839a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
840a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
841a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
842a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
843a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
844d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
845d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
84646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
84746460a68f6508775e98c19b4bb8454bb471aac24John McCall
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
849d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
85037d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
851a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
85237d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
853a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
854a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
855e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
856b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
857b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
858e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
859e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
860e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
861b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
862a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
863a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
864b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
865b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
866a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
867b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
868d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
869d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
871d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
872d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
873d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
874d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
8756c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
8767c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
8777c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
8786c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
8796c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
8806c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
8816c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
882d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
883d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
885741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
886741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
887b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
888b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
889b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
890b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
891b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
892d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
893eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
894eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
895eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
896eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
897eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
898d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
899f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
900d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
90102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
90202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
90302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
90402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
90502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
906d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
907d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
90817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
909d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
910d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
911d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
91202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
91302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
91402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
91502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
91602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9177557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
918a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
919127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
920127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
921127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
922127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
923b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
924127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
926d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
927d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
928828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
932127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
934127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
935127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
936127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
937127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
939b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
940b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
941b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
942b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
943b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
944b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
94579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
94679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
94779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
94879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
950e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
95121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
95221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
95321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9542dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
95521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
956fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
957d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
958d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
959d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
960d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
961d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
962d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
963d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
964d325daa506338ab86f9dd468b48fd010673f49a6John McCall
96568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
96668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
96768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
96868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
96968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
970d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
971d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
972d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
973d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
974d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
975d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
976d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
9777c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
9787c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
979d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
98068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
98102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
98321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
98416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
9850130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
986b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
987d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
988d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
989b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
990b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
991b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
992b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
993b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
994b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
995b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
996b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
998e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
999e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
1000e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
1001838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
100202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1003a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1004a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1005a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1006a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1007a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1008a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1009a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1010a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1011a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1012a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1013a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1014a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1015a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1016a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1017a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1019a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1020a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1021a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1022a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1023b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1024b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1026d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1027d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1028d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1029d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1030d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1031d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
103266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
103366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1034838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
103566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
103666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1037d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1038d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1039d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1040d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
104102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1042a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1043e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1044e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1046e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1047e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1048af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1049a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10516826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10526826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1053af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1054af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1055af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1056af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1057af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1058af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1059af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1060af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1061af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1062af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1063af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1064af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1065af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1066af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1067af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1068af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1069af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1070af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1071af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1072af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1073af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1074af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1075af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1076af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1077af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1078af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1079af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1080af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1081af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1082af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1083af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1084af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1085af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1086af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1087af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1088af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1089a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1090a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1091a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
10926826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1093a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1094a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1095a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1096a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1097a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
10986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
10996826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1100a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1101a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11029f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1103af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1104e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1105e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
110676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
110776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
110876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
110976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1110a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1111a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1112d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11136826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
111476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1115a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
111676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1117a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
111876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
111976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
112076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1121a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1122a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
112376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
112476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
112576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
112676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1127e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1128e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
11292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1130d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1131d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1132d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
11336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
11346b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1135d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1138d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
11396b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
1141d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
1142d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
11436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
11476b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11496b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
11506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
11516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
11526b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
11536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1154b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1155b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1156b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1157b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1158b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1159b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
116079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
116179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
116279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
116379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
116448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
11650ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
116621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
116721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
116821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11692dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
117021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
11712dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1172b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1173b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1174b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1175b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1176b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1177b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1178b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1179b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1180b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1181b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1182b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1183b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1184b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1185b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1186b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1187b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1188b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1189b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1190b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1191b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1192b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1193b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1194b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
11952dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1196b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1197dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1199dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
120017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1201dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1202dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1203dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
120621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                        Name, T, TInfo,
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
120816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
120916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
121017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
121117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
121217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
121317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
121417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
121517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
121616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       T, Destructor->isInlineSpecified(),
121716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
121865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
122065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
1221183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
122265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
122365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
122465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
122565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
122621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                       T, TInfo,
12270130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
122865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1229dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
123121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                   D->getDeclName(), T, TInfo,
123216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
123316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
123416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1235dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
12366b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1237b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1238b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1239b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1240d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1241d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1242d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1244d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1245d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1246d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1247d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1248d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1249d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1250d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1251d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1252d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1253d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1254d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1255d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
12561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1257d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1258b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1259b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1260b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1261b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1263d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
126466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
126566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1266838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
126766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
126866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1269b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
127066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
12712db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
127266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
127366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
12757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
12767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1278b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1279b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1280b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
12817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
12821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
12845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
12855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1286838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
12875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
12885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
12895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
12902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12916826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
12926826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1294b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1295b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1297dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1298dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1299dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1300dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13016826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13026826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1303dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
130565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
130665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
13079f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
130865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
130965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
13104ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
13114ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
13124ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
131346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
131446460a68f6508775e98c19b4bb8454bb471aac24John McCall
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1316b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1317b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1318b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1319b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1320b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1321b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1322b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1323b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1324b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1325b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1326b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1327b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
13281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
13302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
13312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1332615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1333dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1334615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1335615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
133603b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
133717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
133803b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
133903b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1340bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
134165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1342bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1343bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
13446477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1345cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
13462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
13472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1348e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1349e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1350e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1351e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const Type* T = D->getTypeForDecl();
1352e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  assert(T->isTemplateTypeParmType());
1353e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1355e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1356e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1357550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1358e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 TTPT->getName(),
1359e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1360e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1361e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
13620f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
13630f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1364e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1365550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1366550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1367550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1368550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1369e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1370e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1371e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
137233642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
137333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
137433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
137533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1376a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
137733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
137833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
137933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
138033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
138133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
138233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
138333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
138433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
138533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
138633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
138733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
138833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
138933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
139033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
139133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
139233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
139333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
139433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
139533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
139633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
139733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
139833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
139933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
140033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
140133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
140233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
140333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
140433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
1405550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1406550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1407550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1408550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
140933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
141033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
141133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
14120dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
14139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
14149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
14159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
14169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
14179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
14189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
14199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
14209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
14219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
14229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
14239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
14249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
14259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
14269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
14289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
14299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
14309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
14319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
14329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  Param->setDefaultArgument(D->getDefaultArgument());
14339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
14359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
14369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
14379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
14389106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
14399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
14409106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
144148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
144248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
144348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
144448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
144548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
144648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
144748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
144848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
144948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
145048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
145148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
145248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
145348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
145448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1455ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1456ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1457ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1458ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
14599f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
14609f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
14619f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
14629f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
14639f54ad4381370c6b771424b53d219e661d6d6706John McCall
14649f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
14659f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
14669f54ad4381370c6b771424b53d219e661d6d6706John McCall
1467ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1468ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1469ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1470ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1471ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1472ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1473ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1474ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1475ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1476ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1477ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
14789f54ad4381370c6b771424b53d219e661d6d6706John McCall
14799f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
14809f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
14819f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
14829f54ad4381370c6b771424b53d219e661d6d6706John McCall
14839f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
14849f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
14859f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
14869f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
14879f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
14889f54ad4381370c6b771424b53d219e661d6d6706John McCall
14899f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
14909f54ad4381370c6b771424b53d219e661d6d6706John McCall
14919f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
14929f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1493ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1494ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
14959f54ad4381370c6b771424b53d219e661d6d6706John McCall
1496ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1497ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1498ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1499ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15009f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
15019f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
15029f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
15039f54ad4381370c6b771424b53d219e661d6d6706John McCall
1504323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1505323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
15069f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
15079f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
15089f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
15099f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
15109f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
15117c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
15127c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
15139f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
15149f54ad4381370c6b771424b53d219e661d6d6706John McCall
15159f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
15169f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
15179f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
15189f54ad4381370c6b771424b53d219e661d6d6706John McCall
15199f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
15209f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
15219f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1522323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1523323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1524323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
15259f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1526ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1527ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1528ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1529ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1530ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
15319f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
15329f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1533ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1534ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
15367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
15377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
15387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
15397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
15407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
15417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
15427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
15437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
15457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
15467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
15477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
15497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
15507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
15517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
15527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
15537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
15547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (UD)
1555ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1556ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
15587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
15597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
15607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
15617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
15650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
15660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
15670dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15690dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
15700dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
15710dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
15721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
15749488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
15757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
15767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
15777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
15787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
15790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (UD)
1580ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1581ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15820d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
15830dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
15840dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1585ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1586d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
15877e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
15882fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
15892fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
15902fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
15918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
15928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
15938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1594e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1595e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1596e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1597e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1598e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1599e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1600e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1601ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1602e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1603e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1604e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1605e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1606bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1607e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1608e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1609e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1610e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1611bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1612e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
16139148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1614e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1615e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1616e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1618e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1619e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1620e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1621e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1622e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1623e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1624e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1625e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1626e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1627e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1628e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1629e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1631e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1632ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1636ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1637ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1638ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1643ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1644ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1645ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1646550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1647550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1648550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1649550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1650550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1652ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1653ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1654ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1657ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1658ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1660833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1661833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1662833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1663833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1664d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1665833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1666d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1667d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1668ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1669d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1670ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1671ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1672ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1673ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1674ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1675ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1676ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1677ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1678ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1679d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1680ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1681ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1684ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1685ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1688ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1689ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1690ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1691ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1692ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1693ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1694ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1695ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1696ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1697ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1698ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1699ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1700ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1701ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1702ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1703ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1704ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1705ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1706ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1707ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1708ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1709ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
17103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
17113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
17123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
17133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1714d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1715ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1716ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1717ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1718ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1719ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1720ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1722ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1723ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1724ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1725ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1726ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1739ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1742ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
1743ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1744ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1748d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
17493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1750dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1751dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                             ClassTemplate->getPartialSpecializations().size());
1752b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1753b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1754b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1755b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1756ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1757ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
1758ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1762ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1763ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1764ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1765ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
176621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
176721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
176821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
176921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
177021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
177121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
17726cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
17736cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
17746cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
17756cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
177621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
177721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
17785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1779cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1780cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1781895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
17826920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
17836920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
17846920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      TypeLoc NewTL = NewTInfo->getTypeLoc();
17856920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
17866920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
17876920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
17886920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
17896920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
17906920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1791895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1792895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
17936920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1794895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1795cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1796cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1797cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1798cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1799cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
18006920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18016920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
18026920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
18036920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
18046920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
18056920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
18066920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
18076920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1808cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1809cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
181021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
18115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
18125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1814e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1815e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1816e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
18171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1819e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1820e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1821e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
18221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1823cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1824cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1825cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
18261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
18271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1828cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1829cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1830cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1831cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1832cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1833cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1834cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1836cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
18371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1838cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1839bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1840cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1841cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1842f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1843cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1844cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
18451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
18470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
18480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
18500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
18510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
18520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
18530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
18540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
18550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
18560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
18570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
18580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
18590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
18600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
18610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
18620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
18640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
18650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
18670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
18680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
18690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
18700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
18710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
18720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
18730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
18740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
18750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
18760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
18770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
18780ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
18790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1880264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 Proto->getExtInfo()));
18810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
18820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
1883e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1884e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1885e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
18865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
18875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
18885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
18895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
18905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
18911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
18935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1894e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1895e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
18961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
18985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1899e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1900e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
19015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
19035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
19045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
19055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1906a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1907a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1908a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1909a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
1910b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
1911b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
1912b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
1913b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1914a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
1915b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
1916b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
1917b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
1918b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
1919b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
1920e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
1921e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
1922e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
1923e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
1924f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
1925b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
1926e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
1927e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
192854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  if (Function->isInvalidDecl())
192954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
193054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
19316fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis  assert(!Function->getBody() && "Already instantiated!");
19321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1933251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
1934251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1935251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
1936251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
19371eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
19383b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
19391eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
19401eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
19416fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
19421eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1943e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
1944e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
1945e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
1946e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1947e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
1948e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
1949e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
1950e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
1951e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
1952e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
1953e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
1954e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
1955e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
1956e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
1957e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
1958e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
19591eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
1960e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
19611eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
1962d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
1963d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
19641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
1965d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
1967d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
19687ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
1969d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
19701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1971f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1972f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
1973e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
1974e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
1975b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
1976b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
1977b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
1978b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1979b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
1980b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
19811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1982e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1983e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
198454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
198560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
198660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
198760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
198860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
198960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
199060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
199160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
199260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
19931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
199454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
199554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
199654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
199754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
199854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
199954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2000b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2001b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2002b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2003b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2004b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
20051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2006e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2007090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2008090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
20091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2010090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2011090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2012090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
20131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
20141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
2016090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
2017e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
201852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
201952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
202052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
20211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
2022e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2023b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
20240c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
20250c01d18094100db92d38daa923c95661512db203John McCall
2026b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2027aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2028aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2029aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
20301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
203260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
203360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
203460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
203560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2036b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
2037b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
2039b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
20401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2041b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
2042b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2043b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2044a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2045a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2046a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2047a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2048a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
20497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
20507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
20517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
20527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
20537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
20547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
20557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
20567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
20577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2058e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2059e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2060e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2061e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
20627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
20637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
20647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2065e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2066e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
20677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
20687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
20717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
20727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
20730d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
20740d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
20751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20760d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
20777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
20787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2081e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
20820d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2083e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2084e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2085e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2086e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2087e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2088e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
20907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
20917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2092251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
20931028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2094251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2095251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2096251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2097251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2098251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2099251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
21001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2101251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2102251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
21031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
21057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
21067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
21097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
21107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
21117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
21127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
21137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
21141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
21167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
21177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
21187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
21191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2121ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
21227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
21237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
21247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
21257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2126583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2127583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2128583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2129583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
21307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
21317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
21327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
21331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
21357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
21361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
21377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
21381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
21407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
21411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2142a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2143815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2144090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2145090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2147090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
21481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
21509db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
21519db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2152090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2153090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
215472f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
215572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2156090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2157090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
21586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2159090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
21609db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
21611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
21636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
21646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
21656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
21666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2167090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
21689db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2169090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2170090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2171a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2172802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2173802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2174802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2175a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
21769db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2177802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2178802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2179802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2180802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2181a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
21821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2183090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2184802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2185090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2186090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2187090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
21889988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
21891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21909988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
21919988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
21927c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21937c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
21949988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
21957c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
21967c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2197e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2200090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2201090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2202802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2203090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2204090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2205090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
22069db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
22079db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2208090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
22099db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2210090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2211090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2213090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2214090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2215090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
22161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2217090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
22181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
2219090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2220090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
22219db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
22229db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2223090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2224090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
222552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
222652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
222752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
222852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
222952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
223052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
223152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
223252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
223352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
223452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
223552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
223652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
223752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
223852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
223952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
22400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
22410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
22420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
22430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
22440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
22450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
22460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
22470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
22480d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
22490d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
22500d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
22510d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
22520d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2253ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2254ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2255ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2256ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2257ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2258ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2259ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2260ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2261ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2262ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2264ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2265ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2266ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2267ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2268ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
226952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
227052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
227152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
227252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
227352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
227452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
227552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
227652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
227752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
227852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
227952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
228052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
228152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
228252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
228352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
228452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
228552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
228652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
228752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
228852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
228952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
229052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
229152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
229252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
229352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
229452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
229552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
229652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
229752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
229852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
229952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
230052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
230152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
230252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
230352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
230452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
230552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
230652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
230752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2308ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2309ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2310ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2311ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2312ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2313ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2314ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2315ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2316ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2317ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2318ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2319ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
23207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
23217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
23227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2323ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
23247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
23257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
23267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
23270d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
23280d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2329ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
23300d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
23310d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
233252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
233352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
233452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
233552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
233752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
233952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
234052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
234152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
234252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
234352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
234452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
234552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
234652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2347ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2348ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2349815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
23500d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
23517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
23527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
23537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
23547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
23557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
23567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
23577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
23587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
23597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
23600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
23610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
23620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
23630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2364815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
23650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
23660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
236952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
23701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
237152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
237252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
237452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
237552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2376815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
23777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
237852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
237952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
238052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
238252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2383a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
23840d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
23850d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
23860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2387ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2388ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2391ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2392d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2393d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2394d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
23951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2396d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2397d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2398d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
23991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2400ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2401ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2402ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2403ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2404ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2405ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2406815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2407815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2408815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2409815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2410815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
24111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2412815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2413815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2414815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2415815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2416815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2417815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2418815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2419815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2420815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2421815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
242202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
242302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
242402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
242502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
24267c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2427e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
242802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
24297c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
243002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
243102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
243202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
243302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2434ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2435ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2436815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2437815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2438815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2439815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2440815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2441815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2442815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2443815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2444815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2445815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2446815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2447815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2448815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2449815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2450815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2451815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2452815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2453815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2454815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2455815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2456815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2457815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2458ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2459ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
24607c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2461e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2462815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2463550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
24646d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2465550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
24662bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
24672bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
24682bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
24692bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2470815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2471e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2472e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2473e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2474e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
24758b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
24768b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
24778b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
24788b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
24798b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2480e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2481e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2482e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2483e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
24843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = ClassTemplate->getInjectedClassNameSpecialization(Context);
2485e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2486e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2487e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
24883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
24903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
24913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
24923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
24933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
249431f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
24953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2496e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2497e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
24988b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
24998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
25008b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
25018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
25028b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
25038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
25048b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
25058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
25068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2507e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2508e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2509e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2510e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2511e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2512e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2513e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2514e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25158b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
25168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
25178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
25188b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
25198b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
25208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
25218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
25228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
25231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
252452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
25251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
25268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2527e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2528e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
252952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
25308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
25318b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
25328b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
253352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
253452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
25358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
25368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
25378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
25388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
25398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
25408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
25418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
254252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
25438b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
254452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
25458b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2546e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2547e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2548e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
254952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2550e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2551e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2552e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25537c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
25541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
255544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
25561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2557815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2558815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2559815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2560815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
25617c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
25623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
25633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
25643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
25657c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
25667c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
25673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
25683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
25693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
25703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
25713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
25727c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
25737c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
25747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2575815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2576815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
257717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2578815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2579815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2580815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2581815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2582815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2583815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2584815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2585815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2586815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2587815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
25881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
258917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
259017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2591815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
25921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25939f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
259400225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
259500225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
25969f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
25979f54ad4381370c6b771424b53d219e661d6d6706John McCall
2598815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2599815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2600815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2601815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2602815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2603d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
26041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2605d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
260660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
260760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
260860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
260960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
261060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
261160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
261260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
261360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
261460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
261560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
261660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
261760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
26181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
26207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
26211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2622c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2623c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2624c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
26251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26266fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis      if (!Function->getBody())
2627b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor        InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
26287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
26297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
26301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
26327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
26337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2634c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2635291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2636291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2637291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2638291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2639291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2640291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2641291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2642291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2643291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2644291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2645291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2646291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2647291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2648291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2649291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2650291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2651291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2652291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
26531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2654c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2655c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2656c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2657c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
26581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2660d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2661d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
26620c01d18094100db92d38daa923c95661512db203John McCall
26630c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
26640c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
26650c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
26660c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
26670c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
26680c01d18094100db92d38daa923c95661512db203John McCall
26690c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
26700c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
26710c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
26720c01d18094100db92d38daa923c95661512db203John McCall      break;
26730c01d18094100db92d38daa923c95661512db203John McCall    }
26740c01d18094100db92d38daa923c95661512db203John McCall  }
26750c01d18094100db92d38daa923c95661512db203John McCall}
2676