SemaTemplateInstantiateDecl.cpp revision d226f65006733ed7f709c3174f22ce33391cb58f
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
12e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/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
348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    typedef Sema::OwningExprResult OwningExprResult;
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
38d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
397e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
42390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
439a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    // clang/AST/DeclNodes.inc.
444f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
454f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
463dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
483d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
496206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
536477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
55a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
56a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
57d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
58d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
59d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
60615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
62bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
636477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
64e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
657974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
667974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
67d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
68e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
709106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
72ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
73ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
8921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
91e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
93e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
94e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
95ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
96b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
97b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const DeclaratorDecl *OldDecl,
98b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        DeclaratorDecl *NewDecl);
99b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const TagDecl *OldDecl,
100b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        TagDecl *NewDecl);
101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
104ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
108b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
109b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
110b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
111b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
112b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
139b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1404ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth// FIXME: Is this still too simple?
1411d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
1421d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall                            Decl *Tmpl, Decl *New) {
143cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
144cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
145cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
1464ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
1474ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
148cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (Aligned->isAlignmentDependent()) {
1494ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        // The alignment expression is not potentially evaluated.
1501d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall        EnterExpressionEvaluationContext Unevaluated(*this,
1514ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth                                                     Action::Unevaluated);
1524ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
153cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        if (Aligned->isAlignmentExpr()) {
154cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          OwningExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
155cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              TemplateArgs);
156cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (!Result.isInvalid())
157cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
158cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
159cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        else {
160cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
161cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              TemplateArgs,
162cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              Aligned->getLocation(),
163cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              DeclarationName());
164cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
165cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result);
166cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
1674ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
1684ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
1694ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1704ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
171d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
1721d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Attr *NewAttr = TmplAttr->clone(Context);
173d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
174d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
175d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
176d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1774f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1784f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1794f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1804f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1814f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1824f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1834f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1844f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1854f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1864f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1884f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1923dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1933dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1943dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1953dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1963dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1973dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1983dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1993dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
2003dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
2013dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
2023dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
2033dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
206a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
207836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
208836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
209ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
210ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
211ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
213a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
215b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
216b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
2218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
222ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
226d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
227d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
228d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
229d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
230d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
231d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
232d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
233d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
234d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
2355126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2367c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2377c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2385126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2395126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2405126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2411d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
242d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
24346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
24417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
2556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                         llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    Expr *ArgExpr = (Expr *)Arg.get();
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // FIXME: We're faking all of the comma locations. Do we need them?
2716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    FakeCommaLocs.push_back(
2726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                          SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
2736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2756eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               llvm::SmallVector<SourceLocation, 4> &CommaLocs,
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                             ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
3006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
3026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
3036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
3056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
3066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
3086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
3096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
3116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
3166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
3186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              TemplateArgs, CommaLocs,
3196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              NewArgs);
3206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
32328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
32428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
32528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
32628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
32728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             TemplateArgs,
32828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             CommaLocs, NewArgs))
32928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
33028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
33128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
33228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
33328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
33428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
33528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3366b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3376b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
3396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3406b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3426b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3436b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3456b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
3479901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3489901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3499901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3529901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3539901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
354ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
355a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3560a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3570a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3580a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3590a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3613d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
362b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3633d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3643d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3650a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
36616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
36716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3683d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3693d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
372b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
373b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
374b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38146460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
382c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
383c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
384c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3854469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
386390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
387390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3883d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3896826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3906826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
391449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
39260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
39360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3946826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
397ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
398ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
4007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
4017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
402f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
403f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
4047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
4051d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
4068dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
407251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
408251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
409251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
410251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
411cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
412251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
41360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
41460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
41560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
4161f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
4171f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
4181f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
4191f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
4201f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
4236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
4246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
4256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
4266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                CommaLocs, InitArgs, RParenLoc)) {
4276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
4286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
4296eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
430d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
4316b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
4326eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
4336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              CommaLocs.data(),
4346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
4356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
4366b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        Expr *Init = (Expr*)(InitArgs.take()[0]);
437d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddInitializerToDecl(Var,
4386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     SemaRef.Owned(Init),
4396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                     false);
4406b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
4416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
442d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.ActOnUninitializedDecl(Var, false);
44383ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4446eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4456b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4466b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4501f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
45165b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
452d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4533d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4545764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
4555764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
4565764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
457bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4583d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4616206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4626206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4636206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4646206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4656206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4666206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4676206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4686206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
471a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
472836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
473836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
47407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
47507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
47607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
477a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
47807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
47907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
48707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
490b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
491b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
498ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
499ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult InstantiatedBitWidth
502ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
5038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
5048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
5058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
507e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
5088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
51007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
51107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
5138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
5148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
5158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
516ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
5178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
5188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
519663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
520663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
521f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
522663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5241d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
525d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
526f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
527f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
530f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
531f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
5329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
5339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5359901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        Parent->getLookupContext()->isFunctionOrMethod())
5369901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
539f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
54046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
541f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
54602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
54702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
54806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
54932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
55032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
55132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
55232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
55306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
55406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
555fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
55606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
55706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
55806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
56106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
56206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
56306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
56406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
56506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
56606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
56732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
568af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
569af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
570af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
571af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
572af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
57306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
57606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
57706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5785fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
57902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
58002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
581fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
582fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
586ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
587ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  OwningExprResult InstantiatedAssertExpr
590ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
59443d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  OwningExprResult Message(SemaRef, D->getMessage());
59543d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
596d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
597d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                              move(InstantiatedAssertExpr),
598d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                              move(Message));
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
6038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
604741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
6058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    /*PrevDecl=*/0);
6068dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
60706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
608b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
60917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
6118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
61296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
61396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
61496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
615d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
61817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
61917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
6228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    OwningExprResult Value = SemaRef.Owned((Expr *)0);
623ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
624ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
626ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor                                                   Action::Unevaluated);
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
629ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  move(Value));
6428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6503b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
65117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
652d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
65496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
65596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
65696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
65796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
65896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
65996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
663c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
664fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
665c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
666d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
667de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
668fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6736477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6746477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6756477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6766477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6776477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
678e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
681550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
682550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
683550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
684e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
685ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
687d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
688e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
689e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
69293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
69493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
69793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
72693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7371eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
7381eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
742c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
744c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
745c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
746c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
747c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
748c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
749c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
750c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
751c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
752c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
753c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
754c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
755c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
756c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
757c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
758c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
759c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
762c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
763c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
764c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
765c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
766c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
767c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
768c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
769c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
775c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
776c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
777c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
778c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
780c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
781c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
782c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
78493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
786c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
787c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
79493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
797f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
801b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
802e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
807ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
809ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
810ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
811ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
812ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
813ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
81593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
818e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
821f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
822f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
82424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
825ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
826259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
82793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
82893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
829e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
830259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
831e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
832e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
836dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
837dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  D->getPartialSpecializations(PartialSpecs);
838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
840ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
841e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
842e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
843e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
844d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8457974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8467974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
858ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
861cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
8627974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8637974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
865d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
866550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
867550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
868550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
869550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
870550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
871895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
872d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
873d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
875d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
876ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
877a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
878a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
879a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
880a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
881a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
882a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
883a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
884a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
885a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
886a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
887d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
888d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
88946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
89046460a68f6508775e98c19b4bb8454bb471aac24John McCall
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
892d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
89337d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
894a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
89537d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
896a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
897a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
898e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
899b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
900b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
901e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
902e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
903e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
904b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
905a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
906a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
907b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
908b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
909a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
910b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
911d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
912d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
913d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
914d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
915d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
916d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
917d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9186c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9216c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9226c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9236c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9246c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
925d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
926d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
928741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
929741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
930b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
931b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
932b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
933b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
934b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
935d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
936eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
937eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
938eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
939eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
940eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
941d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
942f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
943d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
94402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
94502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
94602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
94702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
94802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9499901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9529901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
9539901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9549901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
955d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
95617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
957d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
958d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
959d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
96002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
96102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
96202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
96302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
96402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9657557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
966a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
967127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
968127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
969127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
970127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
971b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
97224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
97324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9752c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9762c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9772c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
979127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9802c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9812c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
982127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
984b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
985b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
986b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
987b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
988b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
989b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
99079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
991b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
99279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
99379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
99479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
996e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
99721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
99821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
99921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
100121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1002fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1003d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1004d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
1005d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1006d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
1007d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
1008d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
1009d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1010d325daa506338ab86f9dd468b48fd010673f49a6John McCall
101168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
101268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
101368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
101468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
101568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1016d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
1017d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
1019d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
1020d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1021d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1022d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
10237c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10247c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
102668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
102702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
10281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
102921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
103016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
10310130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
1032b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1033d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
1034d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
1035b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1036b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1037b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1038b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1039b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1040b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1041b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1042b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1044e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1045e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
1046e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Params[P]->setOwningFunction(Function);
1047838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
104802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1049ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1055a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1062a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1063a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1065d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1066a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1067a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1068a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1069a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1070b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1071b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1072d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1073d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1074d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1075d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1076d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1077d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1078d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
107966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
108066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
108124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
108224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1083838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
108424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                  new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
108524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
108624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
108766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1088d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1089d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1090d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1091d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
109202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1093a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1094e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1095e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1097e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1098e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1099af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1100a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11016826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11026826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11036826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1104af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1105af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1106af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1107af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1109af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1110af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1111af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1112af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1113af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1114af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1115af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1116af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1117af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1118af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1119af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1120af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1124af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1125af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1126af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1127af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1128af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1129af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1130af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1131af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1132af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1133af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1134af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1135af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1136af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1137af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1138af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1139af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1140a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1141a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1142a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
11436826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1144a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1145a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1146a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1147a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1148a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11496826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1151a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1152a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11539f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1154af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1155e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1156e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
115776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
115876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
115976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
116076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1161a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1162a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1163d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11646826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
116576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1166a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
116776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1168a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
116976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
117076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
117176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1174238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1175238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1176238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
117706a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (Function->hasBody(Definition) &&
1178238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1179238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1180238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1181238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1182238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1183238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1184238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1185238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1186238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1187238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1188238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
1189238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        if (*R != Function &&
1190238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1191238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
1192238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              = (*R)->getTemplateInstantiationPattern())
119306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis            if (RPattern->hasBody(RPattern)) {
1194238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1195238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
1196238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1197238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1198238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1199238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1200238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1201238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1202238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1203238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor
1204a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1205a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
120676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
120776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
120876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
120976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1210e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1211e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1213d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1214d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1215d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
12166b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12176b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1218d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1221d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
122224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
122324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12252c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
12262c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
12272c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12302c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12312c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
12326b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1234b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1235b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1236b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1237b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1238b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1239b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
124079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
124179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
124279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
124379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
124448dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12450ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
124621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
124721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
124821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
125021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12512dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12525f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // \brief If the type of this function is not *directly* a function
12535f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // type, then we're instantiating the a function that was declared
12545f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // via a typedef, e.g.,
12555f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12565f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12575f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12585f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12595f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12605f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
12615f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  if (!isa<FunctionProtoType>(T)) {
12625f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
12635f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
12645f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
12655f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                                TemplateArgs);
12665f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      if (!P)
12675f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor        return 0;
12685f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12695f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      Params.push_back(P);
12705f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    }
12715f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
12725f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1273b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1274b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1275b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1276b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1278b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1279b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1280b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1281b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1282b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1283b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1284b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1285b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1286b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1287b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1288b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1289b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1290b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1291b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1292b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1293b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1294b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1295b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
12962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1297b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1298dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
13012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
130217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
13042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                        NameInfo, T, TInfo,
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
130616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
130716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
130817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
130917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
13102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T,
13112577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
131216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
131365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
131465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
13152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T, TInfo,
13160130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
131765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1318dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
13202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   NameInfo, T, TInfo,
132116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
132216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
132316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1324dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13256b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1326b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1327b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1328b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1329d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1330d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1331d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1333d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1334d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1335d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1336d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1337d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1338d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1339d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1340d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1341d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1342d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1343d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1344d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1346d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1347b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1348b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1349b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1350b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1352d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
135366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
135466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
135524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
135624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1357838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
135824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                    new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
135924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                              Innermost.first,
136024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                              Innermost.second),
136166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1362b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
136366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13642db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
136566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
136666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
13687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
13697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1370b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1371b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1372b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1373b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
13747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
13751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
13775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
13785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1379838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
13805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
13815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
13825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
13832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
13842577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
13852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1387b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1388b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
13891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1390dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1391dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1392dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1393dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13946826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1396dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13972dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
139865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
139965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
14009f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
140165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
140265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
14034ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
14044ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
14054ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
140646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
140746460a68f6508775e98c19b4bb8454bb471aac24John McCall
1408b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1409b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1410b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1411b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1412b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1413b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1414b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1415b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1416b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1417b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1418b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1419b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1420b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1421bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
14222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1425615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1426dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1427615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1428615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
142903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
143017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
143103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
143203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1433bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
143465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1435bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1436bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14376477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1438cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
14392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1441e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1442e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1443e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1444efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const Type* T = D->getTypeForDecl();
1445efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  assert(T->isTemplateTypeParmType());
1446efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1448e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1449e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1450efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor                                 TTPT->getDepth() - 1, TTPT->getIndex(),
1451efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor                                 TTPT->getName(),
1452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1453e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
14550f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14560f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1457e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1458550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1459550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1460550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1461550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1462e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1463e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1464e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
146533642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
146633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
146733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
146833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1469a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
147033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
147133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
147233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
147333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
147433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
147533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
147633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
147733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
147833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
147933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
148033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
148133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
148233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
148333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
148433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
148533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
148633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
148733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
148833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
148933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
149033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
149133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
149233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getDepth() - 1, D->getPosition(),
149333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                      D->getIdentifier(), T, DI);
149433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
149533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
149633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1497d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1498550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1499550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1500550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1501550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
150233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
150333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
150433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
15050dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
15069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
15079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
15089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
15099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
15109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
15119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
15129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
15139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
15149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Sema::LocalInstantiationScope Scope(SemaRef);
15159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
15169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
15179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
15189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
15219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
15229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
15239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getDepth() - 1, D->getPosition(),
15249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                       D->getIdentifier(), InstParams);
1525d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
15264469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
15279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
15289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
15299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
15309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
15329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
15339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
153448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
153548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
153648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
153748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
153848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
153948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
154048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
154148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
154248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
154348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
154448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
154548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
154648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
154748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1548ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1549ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  // The nested name specifier is non-dependent, so no transformation
1550ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // is required. The same holds for the name info.
1551ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1552ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15539f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
15549f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
15559f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
15569f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
15579f54ad4381370c6b771424b53d219e661d6d6706John McCall
1558ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1559ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
15609f54ad4381370c6b771424b53d219e661d6d6706John McCall
1561ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1562ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1563ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
1564ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getTargetNestedNameDecl(),
1565ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1567ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1568ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
1569ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setScopeRep(D->getTargetNestedNameDecl());
1570ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
15719f54ad4381370c6b771424b53d219e661d6d6706John McCall
15729f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
15739f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
15749f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
15759f54ad4381370c6b771424b53d219e661d6d6706John McCall
15769f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
15779f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
15789f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
15799f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
15809f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
15819f54ad4381370c6b771424b53d219e661d6d6706John McCall
15829f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
15839f54ad4381370c6b771424b53d219e661d6d6706John McCall
15849f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
15859f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1586ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1587ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
15889f54ad4381370c6b771424b53d219e661d6d6706John McCall
1589ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1590ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1591ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1592ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15939f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
15949f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
15959f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
15969f54ad4381370c6b771424b53d219e661d6d6706John McCall
1597323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1598323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
15999f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
16009f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
16019f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
16029f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
16039f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
16047c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
16057c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
16069f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
16079f54ad4381370c6b771424b53d219e661d6d6706John McCall
16089f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
16099f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
16109f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
16119f54ad4381370c6b771424b53d219e661d6d6706John McCall
16129f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
16139f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
16149f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1615323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1616323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1617323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
16189f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1619ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1620ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1621ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1622ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1623ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
16249f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
16259f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1626ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1627ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
16307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
16317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
16337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
16347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
16357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
16367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
16387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
16397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
16407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1641ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1642ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1643ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
16447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
16457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1646ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
16494469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1650ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1651ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
16537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
16547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
16600dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
16610dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
16620dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16640dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
16650dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
16660dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1668ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1669ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1670ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
16729488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1673ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
16764469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1677ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1678ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
16800dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
16810dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1682ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1683d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
16847e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
16852fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
16862fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
16872fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
16888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
16898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
16908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1691e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1692e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1693e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1694e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1695e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1696e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1697e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1698ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1699e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1700e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1701e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1702e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1703bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1704e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1705e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1706e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1707e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1708bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
17109148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1711e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1712e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1714ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1717e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1719e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1721e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1723e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1724ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1725ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1726ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1738550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1739550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1740550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
1741550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  Sema::LocalInstantiationScope Scope(SemaRef);
1742550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1743ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1744ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1748ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1749ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1750ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1751ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1752833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1753833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1754833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1755833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1756d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1757833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1758d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1759d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1761d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1762ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1763ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1764ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1765ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1766ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1771d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1774ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1775ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1776ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1780cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis    = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1781cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                                                Converted.flatSize(), InsertPos);
1782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1790ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
17973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
17983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
17993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
18003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1801d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1821ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1823ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
183013c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
183113c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
183213c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1837d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
18383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1839dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1840cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1841b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1842b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1843b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1844b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
18464469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
18474469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1850cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
185421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
185521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
185621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
185721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
185821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
185921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
18606cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
18616cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
18626cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
18636cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
186421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
186521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
18665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1867cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1868cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1869895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
18706920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18716920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
18726920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      TypeLoc NewTL = NewTInfo->getTypeLoc();
18736920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
18746920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
18756920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
18766920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
18776920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
18786920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1879895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1880895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
18816920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1882895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1883cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1884cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1885cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1886cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1887cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
18886920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18896920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
18906920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
18916920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
18926920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
18936920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
18946920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
18956920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1896cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1897cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
189821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
18995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1902e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1903e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1904e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
19051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19061eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1907e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1908e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1909e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
19101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1911cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1912cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1913cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
19141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
19151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1916cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1917cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1918cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1919cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1920cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1921cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1922cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
19231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1924cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
19251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1926cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1927bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1928cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1929cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1930f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1931cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1932cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
19331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19340ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
19350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
19360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
19380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
19390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
19400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
19410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
19420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
19430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
19440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
19450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
19460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
19470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
19480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
19490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
19500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
19520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
19530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
19550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
19570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
19580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
19590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
19600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
19610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
19620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
19630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
19640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
19650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
19660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
19670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
1968264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 Proto->getExtInfo()));
19690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
19700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19711d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
19727cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
1973e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
1974e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1975e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
19765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
19775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
19785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
19795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
19805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
19811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19821eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
19835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
1984e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
1985e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
19861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
19885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
1989e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
1990e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian    Record->setMethodAsVirtual(New);
19915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
19935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
19945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
19955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
1996a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
1997a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
1998a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
1999a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2000b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2001b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2002b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2003b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2004a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2005b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2006b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2007b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2008b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2009b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2010e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2011e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2012e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2013e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2014f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2015b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2016e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2017e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
201806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
201954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
202054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2021251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2022251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2023251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
20246cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
20251eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
20263b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
20271eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
20281eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
20296fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
20301eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2031e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2032e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2033e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2034e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2035e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2036e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2037e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2038e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2039e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2040e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2041e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2042e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2043e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2044e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2045cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
2046e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2047e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
20481eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2049e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
20501eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2051d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2052d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
20531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2054d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
20551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2056d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
20577ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2058d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
20591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2060f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2061f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2062e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2063e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2064b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2065b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2066b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
2067b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2068b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
2069b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20719679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
20729679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor                                               Action::PotentiallyEvaluated);
2073d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2074e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
207554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
207660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
207760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
207860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
207960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
208060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
208160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
208260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
208360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
20841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
20868a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
20878a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
20888a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
20898a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
20908a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    ParmVarDecl *FunctionParam = Function->getParamDecl(I);
20918a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    FunctionParam->setDeclName(PatternParam->getDeclName());
20928a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    Scope.InstantiatedLocal(PatternParam, FunctionParam);
20938a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
209454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2095b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2096b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2097b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2098b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2099b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2101e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2102090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2103090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2105090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2106090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2107090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
211054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
2111090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
2112e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
211352604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
211452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
211552604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
2116d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnFinishFunctionBody(Function, move(Body),
2117e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2118b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21190c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
21200c01d18094100db92d38daa923c95661512db203John McCall
2121b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2122aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2123aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2124aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
21251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
212660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
212760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
212860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
212960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
213060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2131b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
2132b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
21331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
2134b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PerformPendingImplicitInstantiations();
21351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2136b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
2137b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2138b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2139a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2140a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2141a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2142a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2143a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
21447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
21457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
21467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
21477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
21487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
21497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
21507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
21517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
21527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2153e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2154e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2155e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2156e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
21577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
21587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
21597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2160e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2161e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
21627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
21637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
21667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
21677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
21680d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
21690d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
21701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21710d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
21727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
21737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2176e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
21770d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2178e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2179e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2180e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2181e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2182e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
2183e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
21847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
21857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
21867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2187251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
21881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2189251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2190251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2191251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2192251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2193251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2194251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
21951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2196251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2197251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
22007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
22017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
22047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
22057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
22067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
22077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
22087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
22091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
22117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
22127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
22137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2216ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
22177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
22187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
22197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2221583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2222583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2223583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2224583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
22257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
22267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
22277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
22307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
22311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
22327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PerformPendingImplicitInstantiations();
22331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
22357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
22361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2237a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2238815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2239090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2240090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2241090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2242090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
22431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2244090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
22459db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
22469db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2247090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2248090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
224972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
225072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2251090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2252090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
22536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2254090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
22559db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    llvm::SmallVector<SourceLocation, 4> CommaLocs;
22561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
22586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
22596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                               LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
22606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
22616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2262090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
22639db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2264090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2265090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2266a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2267802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2268802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2269802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2270a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
22719db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2272802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2273802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2274802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2275802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2276a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
22771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2278090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2279802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2280090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2281090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2282090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
22839988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22859988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
22869988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
22877c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
22887c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
22899988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
22907c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
22917c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2292e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
22931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2295090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2296090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2297802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2298090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2299090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2300090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
23019db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
23029db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2303090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
23049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2305090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2306090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
23071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2308090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2309090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2310090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2312090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2313d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2314090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2315090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
23169db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
23179db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2318090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2319090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
232052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
232152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
232252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
232352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
232452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
232552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
232652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
232752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
232852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
232952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
233052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
233152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
233252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
233352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
233452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
23350d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
23360d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
23370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
23380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
23390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
23400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
23410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
23420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
23430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
23440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
23450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
23460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
23470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2348ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2349ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2350ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2351ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2354ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2356ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2357ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2358ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2360ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2361ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2363ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
236452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
236552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
236652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
236752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
236852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
236952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
237052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
237152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
237252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
237352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
237452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
237552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
237652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
237752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
237852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
237952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
238052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
238252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
238352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
238452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
238552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
238652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
238852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
238952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
239052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
239152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
239252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
239352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
239452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
239552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
239652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
239752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
239852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
239952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
240052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
240152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
240252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2403ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2404ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2405ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2406ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2407ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2408ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2409ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2410ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2411ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2412ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2413ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2414ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
24157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
24167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
24177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2418ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
24207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
24217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
24220d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
24230d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2424ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
24260d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
242752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
242852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
242952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
243052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
243252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
243452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
243552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
243652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
243752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
243852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
244052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
244152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2442ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2443ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2444815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
24450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
24467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
24477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
24487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
24497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
24507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
24517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
24527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
24537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
24547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
24550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
24560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
24570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
24580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2459815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
24600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
24610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
24621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
246352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
246452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
246652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
246752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2468815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
246952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
247052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2471815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
24727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
247352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
247452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
247552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
247652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
247752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2478a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
24790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
24800d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
24810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2482ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2483ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2485ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2487d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2488d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2489d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
24901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2491d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2492d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2493d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
24941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2495ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2496ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2497ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2498ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2499ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2500ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2501815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2502815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2503815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2504815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2505815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
25061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2507815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2508815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2509815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2510815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2511815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2512815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2513815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2514815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2515815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2516815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
251702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
251802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
251902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
252002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
25217c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2522e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
252302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
25247c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
252502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
252602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
252702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
252802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2529ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2530ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2531815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2532815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2533815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2534815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2535815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2536815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2537815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2538815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2539815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2540815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2541815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2542815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2543815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2544815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2545815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2546815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2547815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2548815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2549815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2550815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2551815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2552815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2553ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2554ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
25557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2556e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2557815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2558550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
25596d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2560766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
25612bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
25622bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
25638dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
25642bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2565815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2566e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2567e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2568e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2569e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
25708b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
25718b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
25728b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
25738b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
25748b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2575e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2576e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2577e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2578e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
257924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2580e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2581e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2582e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
25833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
25843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
25853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
25863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
25873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
25883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
258931f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
25903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2591e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2592e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
25938b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
25948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
25958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
25968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
25978b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
25988b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
25998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26008b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
26018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2602e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2603e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2604e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2605e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2606e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2607e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2608e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2609e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26108b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
26118b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
26128b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
26138b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
26148b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
26158b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
26168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
26178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
26181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
261952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
26201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
26218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2622e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2623e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
262452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
26258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
26278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
262852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
262952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
26308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
26318b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
26328b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
26338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
26348b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
26358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
26368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
263752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
26388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
263952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
26408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2641e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2642e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2643e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
264452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2645e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2646e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2647e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26487c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
26491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
265044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
26511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2652815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2653815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2654815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2655815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
26567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
26573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
26583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
26593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
26607c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
26617c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
26623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
26633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
26643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
26653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
26663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
26677c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
26687c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
26697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2670815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2671815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
267217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2673815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2674815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2675815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2676815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2677815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2678815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2679815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2680815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2681815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2682815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
26831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
268417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
268517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2686815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
26871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26889f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
268900225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
269000225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
26919f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
26929f54ad4381370c6b771424b53d219e661d6d6706John McCall
2693815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2694815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2695815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2696815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2697815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2698d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
26991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2700d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
270160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregorvoid Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
270260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
270360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor         (!LocalOnly && !PendingImplicitInstantiations.empty())) {
270460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
270560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
270660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
270760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingImplicitInstantiations.front();
270860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingImplicitInstantiations.pop_front();
270960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
271060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
271160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
271260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
27131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
27157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
2716d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      PrettyStackTraceActionsDecl CrashInfo(Function,
2717c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Function->getLocation(), *this,
2718c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                            Context.getSourceManager(),
2719c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                           "instantiating function definition");
27201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27216cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
27227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
27237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
27241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
27267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
27277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2728c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2729291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2730291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2731291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2732291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2733291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2734291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2735291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2736291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2737291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2738291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2739291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2740291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
2741291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
2742291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;  // No longer need implicit instantiation.
2743291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2744291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2745291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2746291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2747d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    PrettyStackTraceActionsDecl CrashInfo(Var, Var->getLocation(), *this,
2748c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          Context.getSourceManager(),
2749c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "instantiating static data member "
2750c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson                                          "definition");
27511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
2753d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2754d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
27550c01d18094100db92d38daa923c95661512db203John McCall
27560c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
27570c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
27580c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
27590c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
27600c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
27610c01d18094100db92d38daa923c95661512db203John McCall
27620c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
27630c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
27640c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
27650c01d18094100db92d38daa923c95661512db203John McCall      break;
27660c01d18094100db92d38daa923c95661512db203John McCall    }
27670c01d18094100db92d38daa923c95661512db203John McCall  }
27680c01d18094100db92d38daa923c95661512db203John McCall}
2769