SemaTemplateInstantiateDecl.cpp revision c070cc602d6eefea881f71a60de09e05b54c3fdd
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
344469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor    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
459a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    // clang/AST/DeclNodes.inc.
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);
516206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
556477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
57a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
58a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
59d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
60d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
61d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
62615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6303b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
64bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
656477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
66e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
677974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
687974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
69d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
70e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
7133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
729106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
74ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
75ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
90fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    const LangOptions &getLangOptions() {
91fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall      return SemaRef.getLangOptions();
92fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall    }
93fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
9521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
97e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
99e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
100e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
101ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
102b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
103b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const DeclaratorDecl *OldDecl,
104b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        DeclaratorDecl *NewDecl);
105b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const TagDecl *OldDecl,
106b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        TagDecl *NewDecl);
107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
140b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
141b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
142b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
143b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
144b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
145b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
146d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson// FIXME: Is this too simple?
1474469e8a97cdca3725b4f8f366916143113c029acDouglas Gregorvoid TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
148d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
149d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson       TmplAttr = TmplAttr->getNext()) {
150d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
151d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
152d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
153d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
154d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
155d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
156d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
157d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1584f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1594f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1604f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1614f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1624f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1634f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1644f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1654f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1664f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1674f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1684f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1694f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1703dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1713dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1723dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1733dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1743dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1753dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1763dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1773dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1783dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1843dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
187a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
188836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
189836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
190ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
191ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
192ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
194a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
196b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
197b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
2028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
203ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
207d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
208d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
209d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
210d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
211d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
212d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
213d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
214d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
215d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
2165126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2177c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2195126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2205126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2215126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
222a6b0907a71c66e6bf65e2477a6f9783d9fec4d47Douglas Gregor  InstantiateAttrs(D, Typedef);
223d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
22446460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
22517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2346eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
2366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
2376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
2386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2396eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2426eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2436eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2466eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2476eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
2546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2686b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               llvm::SmallVector<SourceLocation, 4> &CommaLocs,
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                             ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
2996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              TemplateArgs, CommaLocs,
3006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              NewArgs);
3016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
30428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
30528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
30628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
30728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
30828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             TemplateArgs,
30928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             CommaLocs, NewArgs))
31028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
31128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
31228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
31328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
31428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
31528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
31628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
3206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
3289901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3299901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3309901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3319901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
335ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
336a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3370a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3380a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3390a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3400a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3413d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
343b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3443d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3460a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
34716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
34816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3493d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setDeclaredInCondition(D->isDeclaredInCondition());
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
353b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
354b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
355b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
356b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
364c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
365c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
366c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3674469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
368390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
369390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3703d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3716826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3726826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
373449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
37460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
37560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3766826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
379ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
380ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
384f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor
385f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
386f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
389251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
390251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
391251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
392251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
393cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
394251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
39560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
39660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
39760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3981f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3991f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
4001f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
4011f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
4021f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
4056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
4066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
4076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
4086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                CommaLocs, InitArgs, RParenLoc)) {
4096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
4106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
4116eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
4126eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
4136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
4146eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
4156eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
4166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
4176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
4186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Expr *Init = (Expr*)(InitArgs.take()[0]);
4196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
4206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     SemaRef.Owned(Init),
4216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     false);
4226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
4236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
4246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
42583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4306eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4321f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
43365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
43465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor    SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
4353d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4365764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
4375764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
4385764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
4395764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor
4403d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4413d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4436206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4446206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4456206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4466206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4476206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4486206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4496206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4506206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
453a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
454836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
455836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
45607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
45707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
45807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
459a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
46007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
46107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
46907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
472b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
473b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
480ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
481ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
484ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
489e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
49207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
49307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
498ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
501663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
502663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
503f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
504663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
506d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  InstantiateAttrs(D, Field);
507d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
508f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
509f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
511f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
512f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
513f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
5149901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
5159901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5179901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        Parent->getLookupContext()->isFunctionOrMethod())
5189901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
52246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
523f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
52802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
52902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
53006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
53132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
53232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
53332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
53432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
53506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
53606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
537fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
53806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
53906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
54006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
54106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
54206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
54306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
54406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
54506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
54606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
54706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
54806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
54932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
550af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
551af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
552af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
553af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
554af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
55506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
55806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5605fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
56102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
56202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
563fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
564fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
568ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
569ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
572ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
57643d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
57743d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Decl *StaticAssert
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
580b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(InstantiatedAssertExpr),
581b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                           move(Message)).getAs<Decl>();
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return StaticAssert;
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
588741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
5908dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
59106c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
592b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
59317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
59696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
59796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
59896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
5990ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
60217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
60317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
607ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
608ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
610ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
613ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
6268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6343b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
63517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
636b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
63896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
63996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
64096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
64196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
64296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
64396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
648fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
649c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
650c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump                        Sema::DeclPtrTy::make(Enum),
651fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        &Enumerators[0], Enumerators.size(),
652fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6576477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6586477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6596477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6606477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6616477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
662e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
665550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
666550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
667550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
668e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
669ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
671d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
672e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
673e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
69293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
69493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
69793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7211eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
7221eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
728c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
729c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
730c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
731c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
732c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
733c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
734c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
735c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
736c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
737c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
738c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
739c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
740c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
741c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
742c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
743c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
744c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
745c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
746c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
747c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
748c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
749c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
750c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
751c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
752c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
753c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
759c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
762c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
763c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
764c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
765c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
766c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
770c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
771c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
779e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
781f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
782e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
78493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
785b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
786e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
791ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
793ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
794ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
795ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
796ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
797ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
79893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
802e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
805f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
806f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
8083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                  Inst->getInjectedClassNameSpecialization(SemaRef.Context));
809ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
810259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
813e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
814259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
815e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
816e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
820dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
821dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  D->getPartialSpecializations(PartialSpecs);
822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
823ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
825e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
826e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
827e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
828d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8297974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8307974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
840ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
841ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
842ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Decl *DCanon = D->getCanonicalDecl();
846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor            P = InstClassTemplate->getPartialSpecializations().begin(),
848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor         PEnd = InstClassTemplate->getPartialSpecializations().end();
849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor       P != PEnd; ++P) {
850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return &*P;
852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
8547974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor  return 0;
8557974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8567974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8577974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
858d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
859550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
860550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
861550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
862550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
863550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
864895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
865d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
866d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
868d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
869ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
870a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
871a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
872a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
873a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
874a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
875a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
876a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
877a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
878a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
879a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
880d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
881d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
88246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
88346460a68f6508775e98c19b4bb8454bb471aac24John McCall
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
885d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
88637d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
887a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
88837d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
889a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
890a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
891e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
892b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
893b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
894e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
895e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
896e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
897b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
898a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
899a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
900b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
901b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
902a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
903b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
904d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
905d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
906d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
907d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
908d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
909d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
910d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9116c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9127c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9137c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9146c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9156c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9166c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9176c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
918d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
919d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
921741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
922741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
923b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
924b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
925b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
927b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
928d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
929eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
930eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
931eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
932eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
933eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
934d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
935f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
936d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
93702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
93802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
93902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
94002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
94102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9429901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9439901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9449901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9459901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
9469901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9479901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
948d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
94917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
950d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
951d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
952d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
95302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
95402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
95502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
95602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
95702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9587557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
959a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
960127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
961127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
962127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
963127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
964b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
965127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    llvm::FoldingSetNodeID ID;
9661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
967d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             TemplateArgs.getInnermost().getFlatArgumentList(),
968d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                       TemplateArgs.getInnermost().flat_size(),
969828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                                SemaRef.Context);
9701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
973127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor                                                                   InsertPos);
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
975127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
976127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    if (Info)
977127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor      return Info->Function;
978127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
980b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
981b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
982b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
983b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
984b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
985b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
98679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
987b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
98879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
98979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
99079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
992e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
99321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
99421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
99521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
99721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
998fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
999d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1000d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
1001d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1002d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
1003d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
1004d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
1005d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1006d325daa506338ab86f9dd468b48fd010673f49a6John McCall
100768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
100868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
100968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
101068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
101168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1012d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
1013d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1014d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
1015d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
1016d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1017d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
10197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1021d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
102268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
102302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
102521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
102616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
10270130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
1028b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1029d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
1030d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
1031b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1032b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1033b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1034b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1035b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1036b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1037b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1038b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1040e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1041e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
1042e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
1043838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
104402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1045ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1046a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1047a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1048a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1049a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1055a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1061d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1062a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1063a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1065a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1066b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1067b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1068d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1069d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1070d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1071d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1072d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1073d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1074d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
107566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
107666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1077838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
107866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                &TemplateArgs.getInnermost(),
107966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1080d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1081d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1082d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1083d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
108402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1085a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1086e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1087e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1089e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1090e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1091af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1092a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10936826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10946826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1096af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1097af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1098af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1099af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1100af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1101af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1102af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1103af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1104af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1105af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1106af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1107af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1109af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1110af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1111af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1112af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1113af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1114af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1115af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1116af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1117af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1118af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1119af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1120af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1124af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1125af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1126af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1127af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1128af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1129af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1130af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1131af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1132a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1133a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1134a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
11356826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1136a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1137a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1138a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1139a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1140a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11416826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11426826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1143a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1144a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11459f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1146af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1147e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1148e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
114976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
115076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
115176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
115276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1153a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1154a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1155d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11566826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
115776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1158a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
115976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1160a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
116176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
116276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
116376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1164238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1165238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1166238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1167238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1168238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
1169238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      if (Function->getBody(Definition) &&
1170238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1171238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1174238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1175238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1176238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1177238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1178238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1179238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1180238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
1181238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        if (*R != Function &&
1182238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1183238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
1184238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              = (*R)->getTemplateInstantiationPattern())
1185238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            if (RPattern->getBody(RPattern)) {
1186238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1187238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
1188238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1189238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1190238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1191238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1192238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1193238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1194238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1195238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1196a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1197a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
119876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
119976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
120076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
120176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1202e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1203e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1205d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1206d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1207d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
12086b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12096b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1210d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1213d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
12146b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    llvm::FoldingSetNodeID ID;
12151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo::Profile(ID,
1216d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                            TemplateArgs.getInnermost().getFlatArgumentList(),
1217d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                                      TemplateArgs.getInnermost().flat_size(),
12186b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                SemaRef.Context);
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FunctionTemplateSpecializationInfo *Info
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
12226b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor                                                                   InsertPos);
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12246b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12256b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    if (Info)
12266b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor      return Info->Function;
12276b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12286b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1229b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1230b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1231b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1232b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1233b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1234b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
123579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
123679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
123779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
123879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
123948dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12400ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
124121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
124221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
124321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
124521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12475f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // \brief If the type of this function is not *directly* a function
12485f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // type, then we're instantiating the a function that was declared
12495f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // via a typedef, e.g.,
12505f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12515f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12525f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12535f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12545f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12555f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
12565f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  if (!isa<FunctionProtoType>(T)) {
12575f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
12585f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
12595f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
12605f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                                TemplateArgs);
12615f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      if (!P)
12625f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor        return 0;
12635f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12645f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      Params.push_back(P);
12655f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    }
12665f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
12675f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1268b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1269b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1270b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1271b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1272b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1273b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1274b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1275b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1276b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1278b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1279b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1280b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1281b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1282b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1283b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1284b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1285b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1286b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1287b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1288b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1289b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1290b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
12912dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1292b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1293dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
12941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1295dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  DeclarationName Name = D->getDeclName();
129617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1297dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1298dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1299dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor                                    SemaRef.Context.getCanonicalType(ClassTy));
13001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->getLocation(),
130221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                        Name, T, TInfo,
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
130416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
130516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
130617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
130717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
130817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
130917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                   SemaRef.Context.getCanonicalType(ClassTy));
131017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
131117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor                                       Destructor->getLocation(), Name,
131216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       T, Destructor->isInlineSpecified(),
131316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
131465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CanQualType ConvTy
131665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      = SemaRef.Context.getCanonicalType(
1317183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                      T->getAs<FunctionType>()->getResultType());
131865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
131965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                                                      ConvTy);
132065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
132165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->getLocation(), Name,
132221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                       T, TInfo,
13230130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
132465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1325dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
132721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                                   D->getDeclName(), T, TInfo,
132816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
132916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
133016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1331dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13326b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1333b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1334b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1335b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1336d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1337d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1338d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1340d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1341d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1342d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1343d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1344d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1345d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1346d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1347d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1348d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1349d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1350d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1351d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1353d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1354b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1355b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1356b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1357b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1359d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
136066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
136166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1362838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
136366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              &TemplateArgs.getInnermost(),
136466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1365b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
136666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13672db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
136866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
136966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
13717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
13727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1373b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1374b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1375b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1376b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
13777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
13781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
13805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
13815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1382838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
13835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
13845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
13855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
13862dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
13876826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Name, SourceLocation(),
13886826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
13891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1390b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1391b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
13921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1393dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1394dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1395dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1396dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13976826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1399dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
140165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
140265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
14039f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
140465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
140565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
14064ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
14074ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
14084ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
140946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
141046460a68f6508775e98c19b4bb8454bb471aac24John McCall
1411b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1412b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1413b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1414b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1415b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1416b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1417b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1418b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1419b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1420b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1421b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1422b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1423b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14252dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14272dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1428615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1429dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1430615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1431615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
143203b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
143317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
143403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
143503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1436bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
143765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1438bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1439bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14406477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1441cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
14422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1444e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1445e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1446e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1447efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const Type* T = D->getTypeForDecl();
1448efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  assert(T->isTemplateTypeParmType());
1449efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1451e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1453efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1454efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor                                 TTPT->getName(),
1455e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1456e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1457e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
14580f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14590f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1460e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1461550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1462550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1463550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1464550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1465e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1466e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1467e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
146833642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
146933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
147033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
147133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1472a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
147333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
147433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
147533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
147633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
147733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
147833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
147933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
148033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
148133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
148233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
148333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
148433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
148533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
148633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
148733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
148833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
148933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
149033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
149133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
149233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
149333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
149433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
149533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
149633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
149733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
149833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
149933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1500d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1501550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1502550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1503550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1504550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
150533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
150633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
150733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
15080dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
15099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
15109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
15119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
15129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
15139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
15149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
15159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
15169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
15179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
15189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
15199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
15209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
15219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
15249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
15259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
15269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
15279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
1528d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
15294469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
15309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
15319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
15329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
15339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
15359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
15369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
153748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
153848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
153948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
154048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
154148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
154248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
154348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
154448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
154548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
154648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
154748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
154848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
154948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
155048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1551ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1552ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1553ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // is required.
1554ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15559f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
15569f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
15579f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
15589f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
15599f54ad4381370c6b771424b53d219e661d6d6706John McCall
15609f54ad4381370c6b771424b53d219e661d6d6706John McCall  LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
15619f54ad4381370c6b771424b53d219e661d6d6706John McCall                    Sema::LookupUsingDeclName, Sema::ForRedeclaration);
15629f54ad4381370c6b771424b53d219e661d6d6706John McCall
1563ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1564ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getLocation(),
1565ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1567ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1568ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getDeclName(),
1569ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1570ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1571ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1572ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1573ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
15749f54ad4381370c6b771424b53d219e661d6d6706John McCall
15759f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
15769f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
15779f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
15789f54ad4381370c6b771424b53d219e661d6d6706John McCall
15799f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
15809f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
15819f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
15829f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
15839f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
15849f54ad4381370c6b771424b53d219e661d6d6706John McCall
15859f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
15869f54ad4381370c6b771424b53d219e661d6d6706John McCall
15879f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
15889f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1589ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1590ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
15919f54ad4381370c6b771424b53d219e661d6d6706John McCall
1592ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1593ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1594ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1595ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15969f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
15979f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
15989f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
15999f54ad4381370c6b771424b53d219e661d6d6706John McCall
1600323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1601323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
16029f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
16039f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
16049f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
16059f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
16069f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
16077c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
16087c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
16099f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
16109f54ad4381370c6b771424b53d219e661d6d6706John McCall
16119f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
16129f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
16139f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
16149f54ad4381370c6b771424b53d219e661d6d6706John McCall
16159f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
16169f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
16179f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1618323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1619323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1620323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
16219f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1622ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1623ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1624ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1625ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1626ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
16279f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
16289f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1629ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1630ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
16337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
16347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
16367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
16377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
16387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
16397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
16417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
16427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
16437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
16457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
16467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
16477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
16487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
16504469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1651ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1652ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
16547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
16557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
16610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
16620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
16630dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
16660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
16670dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
16709488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
16717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getUsingLoc(), SS, D->getLocation(),
16727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  D->getDeclName(), 0,
16737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
16754469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1676ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1677ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16780d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
16790dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
16800dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1681ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1682d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
16837e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
16842fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
16852fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
16862fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
16878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
16888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
16898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1690e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1691e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1692e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1693e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1694e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1695e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1696e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1697ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1698e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1699e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1700e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1701e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1702bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1703e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1704e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1705e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1706e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1707bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1708e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
17099148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1710e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1711e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1712e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  if (Invalid) {
1714e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall         PI != PE; ++PI)
1716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall      if (*PI)
1717e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall        (*PI)->Destroy(SemaRef.Context);
1718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1719e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1721e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1722e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1723e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1724e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1725e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
17261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1739ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1742550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1743550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1744550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1745550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1746550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1748ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1749ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1750ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1751ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1752ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1753ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1754ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1755ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1756833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1757833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1758833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1759833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1760d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1761833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1762d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1763d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1764ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1765d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1766ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1771ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1774ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1775d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1776ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1780ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  llvm::FoldingSetNodeID ID;
1783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl::Profile(ID,
1784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.flatSize(),
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  SemaRef.Context);
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1790ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                                     InsertPos);
1791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
18063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
18073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
18083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
18093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1810d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1821ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1823ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
183913c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
184013c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
184113c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1842ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1846d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
18473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1848dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1849dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                             ClassTemplate->getPartialSpecializations().size());
1850b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1851b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1852b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1853b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
18554469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
18564469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1858ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                        InsertPos);
1861ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1862ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1863ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
186421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
186521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
186621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
186721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
186821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
186921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
18706cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
18716cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
18726cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
18736cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
187421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
187521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
18765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1877cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1878cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1879895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
18806920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18816920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
18826920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      TypeLoc NewTL = NewTInfo->getTypeLoc();
18836920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
18846920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
18856920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
18866920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
18876920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
18886920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1889895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1890895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
18916920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1892895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1893cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1894cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1895cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1896cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1897cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
18986920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18996920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
19006920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
19016920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
19026920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
19036920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
19046920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
19056920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1906cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1907cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
190821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
19095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1912e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1913e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1914e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
19151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19161eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1917e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1918e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1919e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
19201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1921cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1922cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1923cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
19241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
19251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1926cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1927cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1928cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1929cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1930cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1931cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1932cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
19331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1934cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1936cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1937bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1938cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1939cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1940f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1941cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1942cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
19431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
19450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
19460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
19480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
19490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
19500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
19510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
19520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
19530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
19540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
19550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
19560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
19570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
19580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
19590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
19600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
19620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
19630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
19650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
19670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
19680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
19690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
19700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
19710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
19720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
19730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
19740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
19750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
19760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
19770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1978264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 Proto->getExtInfo()));
19790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
19800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19817cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor  InstantiateAttrs(Tmpl, New);
19827cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
1983e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1984e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1985e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
19865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
19875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
19885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
19895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
19905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
19911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19921eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
19935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1994e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1995e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
19961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
19985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1999e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
2000e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
20015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
20035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
20045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
20055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2006a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2007a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2008a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2009a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2010b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2011b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2012b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2013b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2014a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2015b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2016b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2017b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2018b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2019b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2020e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2021e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2022e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2023e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2024f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2025b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2026e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2027e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
2028238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor  if (Function->isInvalidDecl() || Function->getBody())
202954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
203054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2031251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2032251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2033251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
20346cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
20351eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
20363b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
20371eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
20381eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
20396fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
20401eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2041e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2042e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2043e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2044e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2045e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2046e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2047e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2048e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2049e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2050e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2051e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2052e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2053e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2054e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2055cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
2056e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2057e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20581eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2059e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
20601eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2061d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2062d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
20631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2064d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
20651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2066d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
20677ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2068d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
20691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2070f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2071f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2072e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2073e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2074b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2075b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2076b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
2077b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2078b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
2079b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20819679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
20829679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor                                               Action::PotentiallyEvaluated);
2083e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor  ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2084e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
208554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
208660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
208760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
208860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
208960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
209060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
209160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
209260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
209360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
20941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
209654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // instantiation scope.
209754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
209854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
209954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor                            Function->getParamDecl(I));
210054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2101b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2102b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2103b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2104b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2105b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2107e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2108090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2109090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2111090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2112090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2113090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
21141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
21151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
211654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
2117090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
2118e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
211952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
212052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
212152604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
21221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
2123e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2124b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21250c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
21260c01d18094100db92d38daa923c95661512db203John McCall
2127b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2128aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2129aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2130aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
21311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
213360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
213460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
213560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
213660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2137b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
2138b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
21391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
2140b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
21411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2142b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
2143b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2144b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2145a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2146a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2147a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2148a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2149a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
21507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
21517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
21527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
21537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
21547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
21557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
21567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
21577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
21587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2159e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2160e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2161e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2162e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
21637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
21647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
21657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2166e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2167e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
21687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
21697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
21727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
21737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
21740d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
21750d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21770d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
21787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
21797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
21801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
21811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2182e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
21830d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2184e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2185e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2186e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2187e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2188e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2189e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
21907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
21927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2193251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
21941028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2195251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2196251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2197251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2198251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2199251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2200251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
22011028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2202251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2203251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
22067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
22077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
22107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
22117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
22127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
22137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
22147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
22177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
22187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
22197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
22201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22211028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2222ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
22237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
22247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
22257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2227583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2228583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2229583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2230583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
22317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
22327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
22337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
22341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
22367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
22387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
22391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
22417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
22421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2243a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2244815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2245090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2246090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2247090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2248090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
22491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2250090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
22519db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
22529db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2253090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2254090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
225572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
225672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2257090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2258090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
22596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2260090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
22619db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
22621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
22646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
22656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
22666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
22676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2268090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
22699db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2270090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2271090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2272a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2273802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2274802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2275802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2276a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
22779db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2278802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2279802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2280802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2281802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2282a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
22831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2284090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2285802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2286090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2287090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2288090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
22899988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22919988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
22929988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
22937c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
22947c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
22959988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
22967c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
22977c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2298e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
22991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2301090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2302090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2303802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2304090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2305090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2306090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
23079db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
23089db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2309090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
23109db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2311090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2312090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
23131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2314090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2315090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2316090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
23171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2318090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ActOnMemInitializers(DeclPtrTy::make(New),
2320090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2321090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
23229db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
23239db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2324090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2325090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
232652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
232752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
232852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
232952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
233052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
233152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
233352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
233452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
233552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
233652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
233752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
233952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
234052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
23410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
23420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
23430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
23440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
23450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
23460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
23470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
23480d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
23490d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
23500d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
23510d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
23520d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
23530d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2357ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2358ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2360ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2361ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2363ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2364ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2365ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2366ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
237052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
237152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
237252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
237352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
237452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
237552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
237652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
237752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
237852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
237952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
238152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
238252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
238452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
238552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
238652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
238852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
238952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
239052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
239152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
239252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
239352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
239452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
239552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
239652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
239752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
239852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
239952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
240052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
240152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
240252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
240352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
240452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
240552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
240652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
240752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
240852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2409ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2410ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2411ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2412ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2413ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2414ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2415ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2416ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2417ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2418ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2419ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2420ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
24217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
24227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
24237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2424ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
24267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
24277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
24280d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
24290d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2430ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24310d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
24320d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
243352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
243452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
243552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
243652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
243852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
244052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
244152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
244252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
244352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
244452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
244552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
244652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
244752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2448ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2449ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2450815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
24510d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
24527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
24537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
24547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
24557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
24567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
24577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
24587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
24597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
24607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
24610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
24620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
24630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
24640d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2465815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
24660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
24670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
24681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
246952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
247052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
24711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
247252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
247352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2474815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
247552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
247652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2477815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
24787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
247952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
248052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
248152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
248252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
248352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2484a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
24850d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
24860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
24870d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2489ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2490ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2491ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2492ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2493d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2494d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2495d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
24961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2497d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2498d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2499d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
25001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2501ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2502ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2503ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2504ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2505ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2506ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2507815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2508815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2509815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2510815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2511815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
25121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2513815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2514815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2515815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2516815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2517815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2518815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2519815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2520815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2521815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2522815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
252302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
252402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
252502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
252602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
25277c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2528e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
252902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
25307c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
253102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
253202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
253302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
253402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2535ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2536ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2537815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2538815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2539815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2540815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2541815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2542815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2543815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2544815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2545815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2546815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2547815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2548815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2549815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2550815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2551815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2552815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2553815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2554815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2555815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2556815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2557815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2558815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2559ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2560ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
25617c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2562e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2563815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2564550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
25656d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2566550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor      ParentDC->isFunctionOrMethod()) {
25672bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
25682bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
25692bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
25702bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2571815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2572e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2573e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2574e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2575e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25768b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
25778b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
25788b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
25798b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
25808b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2581e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2582e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2583e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2584e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
25853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = ClassTemplate->getInjectedClassNameSpecialization(Context);
2586e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2587e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2588e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
25893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
25903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
25913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
25923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
25933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
25943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
259531f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
25963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2597e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2598e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
25998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
26008b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
26018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
26028b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
26038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
26048b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
26058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
26078b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2608e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2609e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2610e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2611e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2612e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2613e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2614e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2615e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
26178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
26188b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
26198b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
26208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
26218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
26228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
26238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
26241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
262552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
26261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
26278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2628e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2629e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
263052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
26318b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26328b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
26338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
263452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
263552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
26368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
26378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
26388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
26398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
26408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
26418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
26428b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
264352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
26448b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
264552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
26468b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2647e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2648e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2649e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
265052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2651e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2652e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2653e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26547c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
26551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
265644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2658815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2659815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2660815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2661815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
26627c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
26633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
26643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
26653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
26667c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
26677c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
26683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
26693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
26703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
26713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
26723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
26737c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
26747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
26757c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2676815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2677815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
267817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2679815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2680815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2681815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2682815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2683815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2684815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2685815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2686815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2687815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2688815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
26891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
269017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
269117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2692815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
26931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26949f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
269500225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
269600225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
26979f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
26989f54ad4381370c6b771424b53d219e661d6d6706John McCall
2699815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2700815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2701815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2702815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2703815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2704d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2706d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
270760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
270860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
270960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
271060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
271160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
271260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
271360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
271460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
271560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
271660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
271760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
271860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
27191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
27217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
27221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
2723c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2724c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2725c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
27261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27276cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
27287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
27297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
27301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
27327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
27337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2734c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2735291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2736291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2737291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2738291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2739291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2740291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2741291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2742291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2743291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2744291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2745291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2746291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2747291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2748291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2749291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2750291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2751291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2752291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
27531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
2754c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Var->getLocation(), *this,
2755c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2756c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2757c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
27581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2760d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2761d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
27620c01d18094100db92d38daa923c95661512db203John McCall
27630c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
27640c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
27650c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
27660c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
27670c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
27680c01d18094100db92d38daa923c95661512db203John McCall
27690c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
27700c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
27710c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
27720c01d18094100db92d38daa923c95661512db203John McCall      break;
27730c01d18094100db92d38daa923c95661512db203John McCall    }
27740c01d18094100db92d38daa923c95661512db203John McCall  }
27750c01d18094100db92d38daa923c95661512db203John McCall}
2776