SemaTemplateInstantiateDecl.cpp revision 9a34edb710917798aa30263374f624f13b594605
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//===----------------------------------------------------------------------===/
122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
14f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
16aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
200c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
22a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregornamespace {
2985b4521e34dcd4a0a4a1f0819e1123128e5a3125Benjamin Kramer  class TemplateDeclInstantiator
30b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Sema &SemaRef;
328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    DeclContext *Owner;
33d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor    const MultiLevelTemplateArgumentList &TemplateArgs;
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  public:
368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
37d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                             const MultiLevelTemplateArgumentList &TemplateArgs)
387e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor      : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: Once we get closer to completion, replace these manually-written
41390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // declarations with automatically-generated ones from
429a55591af3e5506b95a9718e15380129fbfc5ebcSean Hunt    // clang/AST/DeclNodes.inc.
434f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
444f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor    Decl *VisitNamespaceDecl(NamespaceDecl *D);
453dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitTypedefDecl(TypedefDecl *D);
473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    Decl *VisitVarDecl(VarDecl *D);
486206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitFieldDecl(FieldDecl *D);
508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Decl *VisitEnumDecl(EnumDecl *D);
526477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
5302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Decl *VisitFriendDecl(FriendDecl *D);
54a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Decl *VisitFunctionDecl(FunctionDecl *D,
55a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                            TemplateParameterList *TemplateParams = 0);
56d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
57d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
58d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                             TemplateParameterList *TemplateParams = 0);
59615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor    Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
6003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor    Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
61bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor    Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
626477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor    ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
63e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor    Decl *VisitClassTemplatePartialSpecializationDecl(
657974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                    ClassTemplatePartialSpecializationDecl *D);
66d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
67e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
6833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
699106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
7048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
71ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingDecl(UsingDecl *D);
72ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Base case. FIXME: Remove once we can instantiate everything.
7748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    Decl *VisitDecl(Decl *D) {
7848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
7948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                            Diagnostic::Error,
8048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                                   "cannot instantiate %0 yet");
8148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor      SemaRef.Diag(D->getLocation(), DiagID)
8248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor        << D->getDeclKindName();
8348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      return 0;
858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    // Helper functions for instantiating methods.
8821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                             llvm::SmallVectorImpl<ParmVarDecl *> &Params);
90e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
92e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
93e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateParameterList *
94ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      SubstTemplateParams(TemplateParameterList *List);
95b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
96b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const DeclaratorDecl *OldDecl,
97b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        DeclaratorDecl *NewDecl);
98b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    bool SubstQualifier(const TagDecl *OldDecl,
99b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                        TagDecl *NewDecl);
100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    bool InstantiateClassTemplatePartialSpecialization(
102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                              ClassTemplateDecl *ClassTemplate,
103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           ClassTemplatePartialSpecializationDecl *PartialSpec);
1048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  };
1058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
108b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
109b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
110b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
111b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
112b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
121b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
122b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
123b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
124b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
125b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
126b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
127b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
128b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
129b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
130b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
131b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
132b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
133b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
134b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
135b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
136b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
137b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
138b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1394ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth// FIXME: Is this still too simple?
1401d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
1411d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall                            Decl *Tmpl, Decl *New) {
142cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
143cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
144cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
1454ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
1464ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
147cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (Aligned->isAlignmentDependent()) {
1484ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        // The alignment expression is not potentially evaluated.
1491d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall        EnterExpressionEvaluationContext Unevaluated(*this,
150f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                     Sema::Unevaluated);
1514ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
152cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        if (Aligned->isAlignmentExpr()) {
15360d7b3a319d84d688752be3870615ac0f111fb16John McCall          ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
154cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              TemplateArgs);
155cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (!Result.isInvalid())
156cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
157cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
158cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        else {
159cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
160cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              TemplateArgs,
161cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              Aligned->getLocation(),
162cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt                                              DeclarationName());
163cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
164cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result);
165cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
1664ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
1674ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
1684ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1694ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
170d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
1711d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Attr *NewAttr = TmplAttr->clone(Context);
172d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
173d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
174d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
175d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1764f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1774f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1784f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1794f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1804f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1814f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1824f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1834f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1844f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1854f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1864f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1883dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1923dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1933dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1943dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1953dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1963dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1973dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1983dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1993dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
2003dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
2013dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
2023dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
205a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
206836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
207836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
208ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
209ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
210ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
212a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
214b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
215b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
2198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
2208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
221ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
2228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
225d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
226d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
227d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
228d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
229d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
230d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
231d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
232d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
233d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
2345126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
2357c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2367c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
2375126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
2385126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2395126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2401d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
241d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
24246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
24317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
2496eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
2506eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
2516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
2526eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
2536eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
254ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                           ASTOwningVector<Expr*> &InitArgs) {
2556eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
2566eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
2576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
2586eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
2596eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
2606eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
26160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
2626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
2636eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
2646eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2656eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
2666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
2676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2686eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
2696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
2706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
287ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                             ASTOwningVector<Expr*> &NewArgs,
2886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
2976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
3006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
3016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
3036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
3046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
3066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
3076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
3086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
3096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
310a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                              TemplateArgs, NewArgs);
3116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
31428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
31528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
31628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
31728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
318a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                             TemplateArgs, NewArgs))
31928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
32028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
32128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
32228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
323a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
32428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
32528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
3266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
3276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
32860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
3296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
3306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
3316b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3326b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
3336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
3346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
3356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
3363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
3379901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3389901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3399901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3409901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3419901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3429901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3439901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
344ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
345a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3460a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3470a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3480a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3490a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
352c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
353c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
354c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
355c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
356c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
357c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor
358b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
3603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3610a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
36216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
36316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3643d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3653d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
368b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
369b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
370b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
378c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
379c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
380c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3814469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
382390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
383390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3843d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3856826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3866826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
387449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
38860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
38960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3906826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
393ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
394ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
398f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
399f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
4007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
4011d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
4028dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
403251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
404251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
405251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
406251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
407cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
408251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
40960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
41060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
41160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
4121f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
4131f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
4141f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
4151f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
4161f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
419ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
4206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
421a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                InitArgs, RParenLoc)) {
4226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
4236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
4246eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
425d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
4266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
4276eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
4286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
4296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
4309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
4319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        SemaRef.AddInitializerToDecl(Var, Init, false);
4326b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
4336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
434d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.ActOnUninitializedDecl(Var, false);
43583ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4376b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4406eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
4416eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
4421f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
44365b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
444d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4465764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
4475764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
4485764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
449bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4503d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4523d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4536206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4546206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4556206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4566206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4576206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4586206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4596206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4606206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
463a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
464836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
465836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
46607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
46707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
46807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
469a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
47007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
47107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
47907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
482b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
483b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
490ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
491f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
494ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
499e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
50207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
50307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
5058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
5078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
508ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
5098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
5108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
511663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
512663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
513f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
514663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5161d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
517d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
518f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
519f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
522f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
523f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
5249901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
5259901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5269901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5277a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
5289901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
531f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
53246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
533f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
53802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
53902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
54006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
54132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
54232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
54332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
54432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
54506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
54606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
547fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
54806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
54906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
55006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
55106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
55206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5539a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
55406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
55506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
55606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
55706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
55806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
56032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
561af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
562af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
563af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
564af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
565af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
56606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
56906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
57006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5715fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5729a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
57302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
57402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
575fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
576fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
580ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
581f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
584ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
58860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
58943d9d9243329b1b75d1a6efdad9f16d6fb386b8eDouglas Gregor  D->getMessage()->Retain();
590d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5919ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
5929ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              Message.get());
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
598741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
5991274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                    /*PrevDecl=*/0,
6001274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                    D->isScoped(), D->isFixed());
6011274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
6021274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
6031274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6041274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6051274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6061274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6071274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
6081274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
6091274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
6101274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
6111274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6121274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
6131274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
6141274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
6161274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6171274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6191274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6211274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6228dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
62306c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
624b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
62517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
6268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
62896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
62996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
63096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
631d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
6328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
63417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
63517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
63860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
639ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
640ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
642f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
644ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
645ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6663b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
66717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
668d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
67096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
67196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
67296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
67396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
67496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
67596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
680fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
681c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
682d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
683de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
684fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6896477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6906477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6916477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6926477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6936477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
694e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
697550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
698550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
6992a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
700e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
701ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
703d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
704e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
705e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
72693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
73793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
74693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
74793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
74993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
75093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7531eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
7541eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
758c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
762c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
763c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
764c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
765c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
766c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
767c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
768c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
769c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
770c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
771c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
772c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
773c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
774c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
775c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
776c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
777c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
778c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
780c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
781c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
782c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
783c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
784c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
785c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
78693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
791c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
792c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
793c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
794c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
795c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
796c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
797c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
798c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
802c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
803c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
81093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
813f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
814e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
81593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
817b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
818e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
822e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
823ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
825ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
826ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
827ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
828ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
829ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
83093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
83193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
83293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
83393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
834e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
83593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setInstantiatedFromMemberTemplate(D);
83693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
837f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
838f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
84024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
841ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
842259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
84393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
84493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
845e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
846259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
847e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
848e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Instantiate all of the partial specializations of this member class
851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template.
852dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
853dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor  D->getPartialSpecializations(PartialSpecs);
854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
857e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
858e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
859e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
860d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8617974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8627974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
863ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
864ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
865ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
866ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
867ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
868ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
869ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
870ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
871ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
872ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
873ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
874ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
875ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
876ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
877cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
8787974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8797974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8807974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
881d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
882550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
883550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
884550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
885550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
8862a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
887895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
888d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
889d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
891d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
892ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
893a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
894a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
895a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
896a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
897a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
898a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
899a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
900a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
901a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
902a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
903d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
904d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
90546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
90646460a68f6508775e98c19b4bb8454bb471aac24John McCall
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
908d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
90937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
910a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
91137d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
912a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
913a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
914e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
915b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
916b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
917e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
918e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
919e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
920b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
921a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
922a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
923b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
924b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
925a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
926b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
927d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
928d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
929d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
930d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
931d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
932d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
933d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9346c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9357c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9367c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9376c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9386c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9396c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9406c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
941d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
942d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
944741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
945741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
946b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
947b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
948b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
949b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
951d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
952eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
953eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
954eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
955eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
956eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
957d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
958f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
959d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
96002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
96102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
96202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
96302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
96402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9659901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9669901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9679901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9687a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9699901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9709901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
971d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
97217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
973d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
974d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
975d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
97602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
97702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
97802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
97902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
98002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9817557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
982a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
983127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
984127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
985127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
986127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
987b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
98824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
98924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9912c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9922c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9932c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
995127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9962c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9972c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
998127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1000b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1001b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1002b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1003b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1004b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1005b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
100679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1007b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
100879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
100979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
10102a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
10111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1012e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
101321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
101421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
101521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
101721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1018fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1019d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1020d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
1021d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1022d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
1023d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
1024d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1026d325daa506338ab86f9dd468b48fd010673f49a6John McCall
102768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
102868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
102968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
103068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
103168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1032d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
1033d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1034d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
1035d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
1036d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1037d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1038d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
10397c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10407c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1041d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
104268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
104302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
10441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
104521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
104616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
10470130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
1048b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1049d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
1050d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
1051b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1052b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1053b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1054b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1055b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1056b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1057b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1058b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1060e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1061e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
10623019c444c672938c57f5573840071ecd73425ee7John McCall    if (Params[P])
10633019c444c672938c57f5573840071ecd73425ee7John McCall      Params[P]->setOwningFunction(Function);
1064838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
106502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1066ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1067a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1068a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1069a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1070a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1071a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1072a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1073a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1074a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1075a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1076a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1077a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1078a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1079a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1080a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1081a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1082d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1083a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1084a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1085a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1086a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1087b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1088b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1089d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1090d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1091d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1092d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1093d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1094d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1095d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
109666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
109766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
109824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
109924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1100838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
110124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                  new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
110224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
110324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
110466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1105d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1106d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1107d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1108d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
110902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1110a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1111e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1112e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1114e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1115e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1116af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1117a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11186826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11196826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11206826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1124af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1125af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1126af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1127af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1128af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1129af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1130af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1131af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1132af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1133af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1134af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1135af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1136af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1137af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1138af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1139af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1140af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1141af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1142af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1143af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1144af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1145af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1146af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1147af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1148af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1149af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1150af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1151af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1152af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1153af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1154af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1155af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1156af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1157a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1158a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1159a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
11606826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1161a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1162a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1163a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1164a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1165a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11666826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11676826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1168a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1169a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11709f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1171af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1172e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1173e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
117476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
117576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
117676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
117776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1178a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1179a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1180d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11816826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
118276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1183a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
118476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1185a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
118676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
118776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
118876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1189ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
119077535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1191ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1192238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1193238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1194238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1195238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
119606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (Function->hasBody(Definition) &&
1197238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1198238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1199238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1200238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1201238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1202238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1203238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1204238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1205238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1206238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1207238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
120813a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
120913a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1210ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1211ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
1212ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          if (!queuedInstantiation && R->isUsed(false)) {
1213ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1214ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1215ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1216ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1217ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1218ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1219ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1220ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1221ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1222ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1223ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1224ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1225ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1226238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
12276a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
122806a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis            if (RPattern->hasBody(RPattern)) {
1229238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1230238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
12316a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1232238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1233238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1234238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1235238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1236238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1237238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1238a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1239a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
124076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
124176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
124276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
124376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1244e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1245e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1247d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1248d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1249d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
12506b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12516b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1252d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1255d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
125624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
125724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12592c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
12602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
12612c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12636b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12652c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
12666b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12676b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1268b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1269b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1270b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1271b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1272b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1273b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
127479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
127579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
127679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12772a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
127848dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12790ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
128021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
128121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
128221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
128421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12852dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
12865f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // \brief If the type of this function is not *directly* a function
12875f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // type, then we're instantiating the a function that was declared
12885f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // via a typedef, e.g.,
12895f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12905f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12915f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12925f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12935f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12945f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
12955f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  if (!isa<FunctionProtoType>(T)) {
12965f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
12975f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
12985f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
12995f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                                TemplateArgs);
13005f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      if (!P)
13015f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor        return 0;
13025f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
13035f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      Params.push_back(P);
13045f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    }
13055f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
13065f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1307b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1308b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1309b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1310b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1311b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1312b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1313b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1314b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1316b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1317b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1318b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1319b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1320b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1321b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1322b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1323b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1324b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1325b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1326b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1327b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1328b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1329b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
13302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1331b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1332dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
13331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13342577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
13352577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
133617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
13382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                        NameInfo, T, TInfo,
13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
134016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
134116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
134217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
134317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
13442577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T,
13452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
134616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
134765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
134865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
13492577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T, TInfo,
13500130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
135165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1352dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
13542577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   NameInfo, T, TInfo,
135516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
135616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
135716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1358dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13596b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1360b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1361b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1362b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1363d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1364d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1365d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1367d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1368d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1369d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1370d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1371d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1372d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1373d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1374d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1375d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1376d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1377d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1378d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1380d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1381b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1382b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1383b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1384b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1386d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
138766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
138866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
138924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
139024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1391838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
139224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                    new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
139324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                              Innermost.first,
139424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                              Innermost.second),
139566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1396b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
139766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13982db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
139966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
140066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
14027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
14037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1404b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1405b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1406b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1407b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
14087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
14115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
14125545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1413838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
14145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
14165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
14172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
14182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
14192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1421b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1422b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1424dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1425dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1426dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1427dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
14286826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
14296826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1430dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
143265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
143365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
14349f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
143565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
143665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
14374ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
14384ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
14394ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
144046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
144146460a68f6508775e98c19b4bb8454bb471aac24John McCall
1442b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1443b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1444b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1445b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1446b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1447b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1448b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1449b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1450b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1451b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1452b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1453b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1454b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1455bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
14562dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1459615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1460dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1461615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1462615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
146303b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
146417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
146503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
146603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1467bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
146865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1469bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1470bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14716477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1472cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
14732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14742dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1475e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1476e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1477e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1478efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const Type* T = D->getTypeForDecl();
1479efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  assert(T->isTemplateTypeParmType());
1480efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1482e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1483e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
148471b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                 TTPT->getDepth() - TemplateArgs.getNumLevels(),
148571b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                 TTPT->getIndex(),TTPT->getName(),
1486e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1487e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1488e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
14890f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14900f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1491e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1492550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1493550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1494550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1495550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1496e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1497e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1498e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
149933642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
150033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
150133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
150233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1503a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
150433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
150533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
150633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
150733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
150833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
150933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
151033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
151133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
151233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
151333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
151433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
151533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
151633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
151733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
151833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
151933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
152033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
152133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
152233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
152333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
152433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
152533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
152671b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
152771b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                      D->getPosition(), D->getIdentifier(), T,
152871b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                      DI);
152933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
153033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
153133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1532d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1533550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1534550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1535550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1536550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
153733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
153833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
153933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
15400dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
15419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
15429106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
15439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
15449106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
15459106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
15469106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
15479106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
15489106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
15492a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
15509106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
15519106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
15529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
15539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
15569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
15579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
155871b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                   D->getDepth() - TemplateArgs.getNumLevels(),
155971b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                       D->getPosition(), D->getIdentifier(),
156071b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                       InstParams);
1561d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
15624469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
15639106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
15649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
15659106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
15669106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15679106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
15689106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
15699106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
157048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
157148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
157248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
157348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
157448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
157548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
157648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
157748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
157848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
157948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
158048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
158148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
158248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
158348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1584ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
15851b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
15861b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
15871b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
15881b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
15891b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
15901b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
15911b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
15921b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
15931b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
15941b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  NestedNameSpecifier *NNS =
15951b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
15961b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      D->getNestedNameRange(),
15971b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      TemplateArgs);
15981b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  if (!NNS)
15991b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      return 0;
16001b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
16011b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
16021b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1603ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1604ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16059f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
16069f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
16079f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
16089f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
16099f54ad4381370c6b771424b53d219e661d6d6706John McCall
1610ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1611ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
16129f54ad4381370c6b771424b53d219e661d6d6706John McCall
1613ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1614ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1615ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
16161b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor                                       NNS,
1617ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1618ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1619ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1620ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
16211b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  SS.setScopeRep(NNS);
1622ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
16239f54ad4381370c6b771424b53d219e661d6d6706John McCall
16249f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
16259f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
16269f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
16279f54ad4381370c6b771424b53d219e661d6d6706John McCall
16289f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
16299f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
16309f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
16319f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
16329f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
16339f54ad4381370c6b771424b53d219e661d6d6706John McCall
16349f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
16359f54ad4381370c6b771424b53d219e661d6d6706John McCall
16369f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
16379f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1638ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1639ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
16409f54ad4381370c6b771424b53d219e661d6d6706John McCall
1641ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1642ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1643ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1644ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16459f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
16469f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
16479f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
16489f54ad4381370c6b771424b53d219e661d6d6706John McCall
1649323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1650323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
16519f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
16529f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
16539f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
16549f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
16559f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
16567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
16577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
16589f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
16599f54ad4381370c6b771424b53d219e661d6d6706John McCall
16609f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
16619f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
16629f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
16639f54ad4381370c6b771424b53d219e661d6d6706John McCall
16649f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
16659f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
16669f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1667323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1668323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1669323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
16709f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1671ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1672ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1673ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1674ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1675ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
16769f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
16779f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1678ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1679ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
16827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
16837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
16857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
16867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
16877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
16887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
16907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
16917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
16927ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1693ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1694ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1695ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
16967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
16977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1698ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
17007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
17014469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1702ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1703ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
17057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
17067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
17077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
17087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
17101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
17120dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
17130dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
17140dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17160dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
17170dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
17180dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1720ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1721ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1722ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
17231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
17249488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1725ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
17267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
17277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
17284469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1729ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1730ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17310d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
17320dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
17330dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1734ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1735d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
17367e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
17372fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
17382fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
17392fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
17408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
17418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
17428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1744e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1749e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1750ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1755bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1756e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1757e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1759e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1760bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1761e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
17629148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1763e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1764e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1765e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1766ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1767e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1768e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1769e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1770e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1771e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1773e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
17741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1775e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1776ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1780ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \returns true if there was an error, false otherwise.
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorbool
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1790550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1791550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1792550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
17932a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1794550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1799ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1804833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1805833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1806833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1807833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1808d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1809833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1810d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1811d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
1813d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        InstTemplateArgs.size());
1821ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1823d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1832cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis    = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1833cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                                                Converted.flatSize(), InsertPos);
1834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                  Converted.getFlatArguments(),
1840ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    Converted.flatSize());
1841ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1842ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
18493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
18503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
18513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
18523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1853d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1858ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1861ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1862ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1863ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1864ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1865ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1866ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1867ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1868ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1869ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1870ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1871ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1872ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1873ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << WrittenTy;
1874ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1875ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1876ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return true;
1877ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1878ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1879ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1880ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1881ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
188213c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
188313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
188413c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1885ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1886ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1887ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1888ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     Converted,
1889d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
18903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1891dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1892cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1893b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1894b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1895b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1896b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1897ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
18984469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
18994469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1900ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1901ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1902cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
1904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1905ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
190621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
190721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
190821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
190921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
191021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
191121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
19126cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
19136cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
19146cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
19156cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
191621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
191721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
19185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1919cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1920cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1921895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
19226920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
19246920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      TypeLoc NewTL = NewTInfo->getTypeLoc();
19256920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
19266920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
19276920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
19286920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
19296920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
19306920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1931895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1932895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
19336920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1934895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1935cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1936cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1937cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1938cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1939cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    TypeLoc OldTL = OldTInfo->getTypeLoc();
19406920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19416920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
19426920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
19436920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
19446920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
19456920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
19466920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
19476920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1948cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1949cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
195021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
19515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1954e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1955e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1956e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
19571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19581eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1959e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1960e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1961e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
19621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1963cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1964cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1965cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
19671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1968cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1969cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1970cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1971cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1972cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1973cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1974cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
19751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1976cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1978cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1979bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1980cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1981cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1982f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1983cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1984cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
19851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19860ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
19870ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
19880ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19890ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
19900ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
19910ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
19920ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
19930ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
19940ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
19950ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
19960ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
19970ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
19980ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
19990ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
20000ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
20010ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
20020ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20030ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
20040ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
20050ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20060ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
20070ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20080ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
20090ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
20100ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
20110ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
20120ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
20130ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
20140ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->isVariadic(),
20150ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getTypeQuals(),
20160ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasExceptionSpec(),
20170ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Proto->hasAnyExceptionSpec(),
20180ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.size(),
20190ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 Exceptions.data(),
2020264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                 Proto->getExtInfo()));
20210ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
20220ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20231d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
20247cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2025e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2026e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2027e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
20285545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
20295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
20305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
20315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
20325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
20331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
20341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
20355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2036e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2037e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20395545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2040e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
204185606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
20425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
20445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
20455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
20465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2047a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2048a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2049a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2050a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2051b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2052b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2053b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2054b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2055a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2056b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2057b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2058b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2059b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2060b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2061e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2062e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2063e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2064e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2065f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2066b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2067e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2068e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
206906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
207054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
207154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2072251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2073251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2074251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
20756cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
20761eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
20773b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
20781eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
20791eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
20806fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
20811eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2082e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2083e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2084e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2085e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2086e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2087e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2088e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2089e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2090e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2091e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2092e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2093e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2094e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2095e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2096cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
209758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
209858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
209962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
210058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2101e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
210258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
21031eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2104e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
21051eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2106d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2107d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2109d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2111d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
21127ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2113d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
21141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2115f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2116f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2117e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2118e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2119b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2120b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2121b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
212262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
2123b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive)
212462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
21251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21269679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
2127f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2128d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2129e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
213054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
213160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
213260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
213360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
213460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
213560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
213660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
213760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
213860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
21391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
214054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
21418a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
21428a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
21438a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
21448a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
21458a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    ParmVarDecl *FunctionParam = Function->getParamDecl(I);
21468a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    FunctionParam->setDeclName(PatternParam->getDeclName());
21478a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    Scope.InstantiatedLocal(PatternParam, FunctionParam);
21488a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
214954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2150b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2151b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2152b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2153b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2154b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2156e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2157090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2158090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
21591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2160090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2161090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2162090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
216554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
216660d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2167e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
216852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
216952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
217052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
21719ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ActOnFinishFunctionBody(Function, Body.get(),
2172e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2173b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
21740c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
21750c01d18094100db92d38daa923c95661512db203John McCall
2176b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2177aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2178aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2179aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
21801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
218260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
218362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
218460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
218560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2186b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
2187b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
218962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
21901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2191b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
219262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2193b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2194a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2195a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2196a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2197a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2198a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
21997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
22007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
22017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
22027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
22047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
22057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
22077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2208e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2209e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2210e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2211e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
22127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
22137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
22147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2215e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2216e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
22177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
22187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
22217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
22227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
22230d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
22240d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
22251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22260d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
22277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
22287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
22301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2231e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
22320d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2233e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2234e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2235e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2236e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
223758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
223858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
223962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
224058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
224158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
224258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
22437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
22457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2246251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
22471028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2248251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2249251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2250251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2251251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2252251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2253251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
22541028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2255251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2256251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
22571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
22597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
22607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
22637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
22647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
226562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
22667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
226762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
22681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
22707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
22717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
22727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
22731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22741028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2275ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
22767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          getTemplateInstantiationArgs(Var)));
22777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
22787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
22797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2280583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2281583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2282583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2283583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
22847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
22857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
22867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
22871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
22897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
229162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
22921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
229462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
22951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2296a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2297815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2298090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2299090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2300090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2301090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2303090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
23049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
23059db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2306090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2307090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
230872f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
230972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2310090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2311090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2312030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2313030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2314030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2315030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2316030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
23176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2318ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
23216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2322a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
23236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
23246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2325090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
23269db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2327090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2328090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2329a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2330802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2331802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2332802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2333a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
23349db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2335802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2336802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2337802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2338802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2339a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
23401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2341090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2342802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2343090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2344090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2345090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
23469988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      FieldDecl *Member;
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23489988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      // Is this an anonymous union?
23499988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      if (FieldDecl *UnionInit = Init->getAnonUnionMember())
23507c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
23517c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      UnionInit, TemplateArgs));
23529988d5d9ad0850e455bd413b03ba7dc8ecee5999Anders Carlsson      else
23537c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
23547c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                      Init->getMember(),
2355e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                                      TemplateArgs));
23561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2358090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2359090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2360802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2361090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
2362090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2363090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
23649db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
23659db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2366090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
23679db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2368090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2369090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
23701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2371090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2372090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2373090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
23741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2375090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2376d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2377090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2378090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
23799db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
23809db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2381090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2382090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
238352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
238452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
238552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
238652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
238752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
238852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
238952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
239052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
239152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
239252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
239352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
239452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
239552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
239652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
239752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
23980d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
23990d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
24000d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
24010d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
24020d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
24030d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
24040d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
24050d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
24060d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
24070d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
24080d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
24090d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
24100d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2412ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2414ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2415ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2416ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2417ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2418ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2419ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2420ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2422ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2423ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2424ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2425ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2426ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
242752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
242852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
242952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
243052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
243252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
243352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
243452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
243552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
243652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
243752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
243852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
243952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
244052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
244152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
244252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
244352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
244452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
244552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
244652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
244752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
244852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
244952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
245052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
245152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
245252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
245352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
245452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
245552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
245652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
245752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
245852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
245952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
246052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
246152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
246252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
246352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
246452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
246552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2466ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2467ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2468ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2469ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2470ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2471ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2472ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2473ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2474ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2475ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2476ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2477ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
24787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
24797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
24807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2481ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
24837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
24847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
24850d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
24860d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2487ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
24880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
24890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
249052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
249152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
249252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
249352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
249452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
249552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
249652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
249752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
249852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
249952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
250052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
250152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
250252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
250352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
250452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2505ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2506ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2507815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
25080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
25097ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
25107ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
25117ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
25127ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
25137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
25147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
25157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
25167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
25177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
25180d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
25190d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
25200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
25210d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2522815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
25230d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
25240d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
25251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
252652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
252752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
25281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
252952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
253052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2531815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
253252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
253352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2534815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
25357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
253652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
253752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
253852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
253952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
254052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2541a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
25420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
25430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
25440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2545ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2546ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2547ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2548ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2550d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2551d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2552d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2554d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2555d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2556d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
25571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2558ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2559ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2560ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2561ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2562ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2563ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2564815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2565815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2566815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2567815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2568815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
25691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2570815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2571815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2572815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2573815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2574815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2575815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2576815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2577815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2578815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2579815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
258002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
258102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
258202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
258302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
25847c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2585e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
258602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
25877c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
258802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
258902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
259002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
259102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2592ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2593ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2594815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2595815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2596815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2597815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2598815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2599815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2600815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2601815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2602815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2603815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2604815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2605815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2606815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2607815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2608815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2609815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2610815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2611815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2612815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2613815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2614815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2615815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2616ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2617ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
26187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2619e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2620815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2621550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
26226d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2623766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
26242bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
26252bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
26268dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
26272bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2628815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2629e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2630e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2631e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2632e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
26348b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
26358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
26368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
26378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2638e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2639e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2640e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2641e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
264224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2643e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2644e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2645e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
26463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
26473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
26483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
26493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
26503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
26513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
265231f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
26533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2654e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2655e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
26568b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
26578b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
26588b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
26598b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
26608b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
26618b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
26628b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26638b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
26648b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2665e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2666e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2667e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2668e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2669e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2670e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2671e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2672e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26738b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
26748b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
26758b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
26768b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
26778b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
26788b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
26798b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
26808b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
26811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
268252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
26831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
26848b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2685e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2686e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
268752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
26888b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
26898b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
26908b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
269152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
269252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
26938b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
26948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
26958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
26968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
26978b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
26988b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
26998b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
270052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
27018b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
270252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
27038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2704e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2705e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2706e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
270752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2708e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2709e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2710e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
27117c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
27121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
271344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
27141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2715815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2716815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2717815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2718815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
27197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
27203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
27213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
27223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
27237c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
27247c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
27253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
27263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
27273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
27283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
27293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
27307c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
27317c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
27327c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2733815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2734815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
273517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2736815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2737815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2738815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2739815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2740815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2741815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2742815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2743815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2744815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2745815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
27461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
274717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
274817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2749815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
27501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27519f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
275200225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
275300225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
27549f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
27559f54ad4381370c6b771424b53d219e661d6d6706John McCall
2756815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2757815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2758815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2759815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2760815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2761d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
27621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2763d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
276462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruthvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
276560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
276662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
276760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
276860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
276960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
277062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
277162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
277260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
277360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
277460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
277560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
27787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
2779f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2780f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
278158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
278258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
278358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
278458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
27857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
27867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
27871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
27897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
27907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2791c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2792291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2793291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2794291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2795291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2796291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2797291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2798291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2799291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2800291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2801291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2802291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2803291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
280458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
280558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
280658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
280758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
280858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
2809291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2810291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2811291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2812291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2813f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2814f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
2815f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
281858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
281958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
282058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
2821d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2822d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
28230c01d18094100db92d38daa923c95661512db203John McCall
28240c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
28250c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
28260c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
28270c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
28280c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
28290c01d18094100db92d38daa923c95661512db203John McCall
28300c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
28310c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
28320c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
28330c01d18094100db92d38daa923c95661512db203John McCall      break;
28340c01d18094100db92d38daa923c95661512db203John McCall    }
28350c01d18094100db92d38daa923c95661512db203John McCall  }
28360c01d18094100db92d38daa923c95661512db203John McCall}
2837f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall
2838