SemaTemplateInstantiateDecl.cpp revision 07a77b4b1d1fa95930129541eff8b79558f5d80d
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
28b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
30b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
31b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
32b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
33b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
34b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
35b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
36b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
37b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
40b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
46b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
47b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
48b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
49b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
50b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
51b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
52b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
53b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
56b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
57b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
58b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
59b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
604ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth// FIXME: Is this still too simple?
611d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
621d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall                            Decl *Tmpl, Decl *New) {
63cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
64cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
65cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
664ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
674ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
68cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (Aligned->isAlignmentDependent()) {
694ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        // The alignment expression is not potentially evaluated.
701d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall        EnterExpressionEvaluationContext Unevaluated(*this,
71f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                     Sema::Unevaluated);
724ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
73cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        if (Aligned->isAlignmentExpr()) {
7460d7b3a319d84d688752be3870615ac0f111fb16John McCall          ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
757663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                        TemplateArgs);
76cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (!Result.isInvalid())
77cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
79cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        else {
80cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
817663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             TemplateArgs,
827663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             Aligned->getLocation(),
837663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             DeclarationName());
84cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
85cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result);
86cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
874ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
884ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
894ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
904ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
91d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
921d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Attr *NewAttr = TmplAttr->clone(Context);
93d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
94d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
95d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
96d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
974f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
984f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
994f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1004f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1014f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1024f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1034f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1044f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1054f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1064f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1074f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1084f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1093dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1103dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1113dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1123dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1133dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1143dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1153dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1163dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1173dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1183dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1193dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1203dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1213dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1223dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1233dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
126a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
127836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
128836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
129ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
130ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
131ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
133a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
135b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
136b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
142ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
146d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
147d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
148d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
149d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
150d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
151d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
152d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
153d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
154d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
1555126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
1585126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1595126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1605126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1611d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
162d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
16346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
16417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
1706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
1716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
1736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
1756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
1776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
1786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
1806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
1826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
1836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
1846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
1857663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                   ASTOwningVector<Expr*> &NewArgs,
1866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
1876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
1886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
1896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
1906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
1926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
1936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1944765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
1956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
1966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
1986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
1996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
20691fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor    return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
20791fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                        true, TemplateArgs, NewArgs);
2086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
21128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
21291fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor      if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
21391fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                       TemplateArgs, NewArgs))
21428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
21528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
21628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
21728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
218a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
21928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
22028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
2216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
22360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
2246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
2256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
2266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
2286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
2296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
2306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2313d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
2349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2359901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2369901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2379901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
2389901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
239ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
240a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2410a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2420a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2430a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2440a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
247c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
248c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
249c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
250c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
251c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
252c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor
253b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2543d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
2553d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2560a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
25716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
25816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
2593d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2603d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
262b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
263b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
264b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
265b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
273c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
274c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
275c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
2764469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
277390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
278390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
2793d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
2806826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
2816826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
282449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
28360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
28460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
2856826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
288ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
289ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
2907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
2917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
2927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
293f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
294f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
2957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
2961d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
2978dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
298251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
299251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
300251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
301251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
302cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
303251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
30460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
30560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
30660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3071f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3081f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3091f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3101f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3111f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
314ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
316a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                InitArgs, RParenLoc)) {
31707a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      // Attach the initializer to the declaration, if we have one.
31807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      if (InitArgs.size() == 0)
31907a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        SemaRef.ActOnUninitializedDecl(Var, false);
32007a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      else if (D->hasCXXDirectInitializer()) {
3216eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
322d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
3236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3246eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
32607a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      } else {
32707a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        assert(InitArgs.size() == 1);
3289ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
3299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        SemaRef.AddInitializerToDecl(Var, Init, false);
33083ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
3316eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3326b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
3356eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3366eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3371f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
33865b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
339d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
3403d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3415764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
3425764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
3435764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
344bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
3453d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3463d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3486206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3496206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3506206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3516206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3526206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3536206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3546206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3556206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
358a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
359836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
360836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
36107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
36207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
36307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
364a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
36507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
36607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
37407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
377b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
378b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
3798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
3848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
385ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
386f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
389ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
394e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
39707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
39807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
403ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
406663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
407663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
408f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
409663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4111d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
412d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
413f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
414f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
417f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
418f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4199901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
4209901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4219901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4227a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4239901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
426f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
42746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
428f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
43387c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
43487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
43587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
43687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
43787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
43887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
43987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
44087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PI != PE; ++PI)
44187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
44287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                            *PI, TemplateArgs));
44387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
44440e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
44587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
44687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
44740e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
44887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
44987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
45287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
45387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
45487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
45587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
45687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
45802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
45906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
46032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
46132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
46232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
46332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
46406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
46506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
466fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
46706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
46806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
46906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
47006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
47106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
4729a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
47306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
47406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
47506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
47606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
47706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
47806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
47932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
480af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
481af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
482af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
483af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
484af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
48506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
48806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
48906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
4905fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
4919a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
49202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
49302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
494fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
495fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
4968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
500f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
503ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
50760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
5083fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
509d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
5119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              Message.get());
5128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
517741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
518a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
519a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
5201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
5211274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
5221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
5231274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
5241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
5251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
5261274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
5271274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
5281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
5291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
5301274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5311274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
5321274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
5331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
5351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
5361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
5371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
5381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
5401274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5415b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
5425b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5438dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
54406c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
545b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
54617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
54996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
55096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
55196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
552d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
5538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
55517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
55617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
55960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
560ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
561ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
563f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
566ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
5798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
5875b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
5885b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5893b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
59017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
591d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
59396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
59496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
59596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
59696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
59796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
59896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
602c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
603fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
604c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
605d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
606de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
607fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6126477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6136477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6146477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6156477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6166477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
61893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
61993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
620550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
621550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
6222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
623e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
624ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
626d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
627e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
628e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
62993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
63093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
63193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
63293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
63393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
63493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
63593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
63693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
63793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
63893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
63993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
64093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
64193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
64293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
64393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
64437574f55cd637340f651330f5cfda69742880d36Nick Lewycky  if (!isFriend && Pattern->getPreviousDeclaration()) {
64537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
64637574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
64737574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
64837574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
64937574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
65037574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
65137574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
65237574f55cd637340f651330f5cfda69742880d36Nick Lewycky
65393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
65493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
67093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
67293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
67393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
6851eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
6861eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
690c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
692c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
693c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
694c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
695c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
696c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
697c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
698c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
699c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
700c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
701c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
702c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
703c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
704c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
705c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
706c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
707c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
708c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
709c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
710c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
711c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
712c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
713c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
714c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
715c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
716c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
717c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
723c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
724c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
725c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
727c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
728c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
729c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
730c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
734c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
735c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
73693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
73793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
745f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
74793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
749b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
75193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
755ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
757ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
758ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
759ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
760ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
761ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
766e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
76737574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
76837574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
770f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
771f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
77324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
774ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
775259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
778e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
779259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
780e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
781e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
782d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
783d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
784d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
785d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
786d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
787d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
788d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
789d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
790d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
791d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
792d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
793d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
795e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
797d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
7987974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
7997974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
800ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
801ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
814d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
815d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
816d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
817d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
818d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
8197974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8207974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
822d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
823550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
824550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
825550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
826550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
8272a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
828895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
829d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
830d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
832d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
834a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
835a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
836a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
837a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
839a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
840a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
841a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
842a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
843a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
844d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
845d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
84646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
84746460a68f6508775e98c19b4bb8454bb471aac24John McCall
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
849d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
85037d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
851a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
85237d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
853a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
854a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
855e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
856b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
857b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
858e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
859e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
860e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
861b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
862a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
863a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
864b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
865b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
866a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
867b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
868d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
869d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
871d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
872d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
873d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
874d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
8756c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
8767c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
8777c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
8786c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
8796c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
8806c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
8816c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
882d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
883d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
885741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
886741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
887b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
888b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
889b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
890b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
891b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
892d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
893eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
894eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
895eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
896eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
897eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
898d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
899f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
900d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
90102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
90202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
90302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
90402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
90502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9069901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9079901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9089901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9097a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9109901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9119901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
912d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
91317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
914d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
915d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
916d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
91702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
91802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
91902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
92002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
92102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9227557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
923a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
924127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
925127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
926127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
927127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
928b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
92924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
93024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9322c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9332c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9342c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
936127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9372c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9382c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
939127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
941b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
942b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
943b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
944b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
945b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
946b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
94779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
948b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
94979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
95079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
9512a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
953e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
95421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
95521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
95621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
95821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
959fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
960d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
961d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
962d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
963d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
964d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
965d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
966d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
967d325daa506338ab86f9dd468b48fd010673f49a6John McCall
96868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
96968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
97068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
97168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
97268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
973d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
974d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
975d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
976d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
977d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
978d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
979d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
9807c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
9817c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
982d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
98368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
98402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
98621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
98716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
9880130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
989b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
990d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
991d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
992b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
993b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
994b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
995b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
996b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
997b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
998b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
999b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1001e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1002e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
10033019c444c672938c57f5573840071ecd73425ee7John McCall    if (Params[P])
10043019c444c672938c57f5573840071ecd73425ee7John McCall      Params[P]->setOwningFunction(Function);
1005838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
100602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1007ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1008a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1009a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1010a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1011a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1012a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1013a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1014a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1015a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1016a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1017a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1018a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1019a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1020a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1021a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1022a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1023d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1024a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1025a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1026a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1027a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1028b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1029b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1030d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1031d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1032d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1033d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1034d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1035d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1036d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
103766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
103866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
103924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
104024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1041838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1042910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
104324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
104424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
104566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1046d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1047d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1048d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1049d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
105002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1052e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1053e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1056e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1057af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10596826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10606826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1062af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1063af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1064af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1065af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1066af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1067af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1068af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1069af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1070af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1071af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1072e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1073e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1074e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1075af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1076af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1077af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1078af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1079af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1080af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1081af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1082af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1083af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1084af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1085af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1086af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1087af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1088af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1089af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1090af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1091af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1092af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1093af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1094a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1095a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1096a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
10976826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1098a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1099a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1100a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1101a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1102a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11036826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11046826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1105a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1106a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11079f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1109e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1110e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
111176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
111276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
111376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
111476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1115a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1116a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1117d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11186826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
111976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1120a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
112176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1122a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
112376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
112476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
112576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1126ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
112777535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1128ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1129238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1130238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1131238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1132238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
113306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (Function->hasBody(Definition) &&
1134238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1135238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1136238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1137238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1138238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1139238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1140238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1141238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1142238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1143238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1144238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
114513a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
114613a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1147ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1148ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
1149ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          if (!queuedInstantiation && R->isUsed(false)) {
1150ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1151ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1152ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1153ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1154ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1155ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1156ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1157ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1158ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1159ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1160ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1161ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1162ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1163238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
11646a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
116506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis            if (RPattern->hasBody(RPattern)) {
1166238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1167238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
11686a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1169238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1170238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1171238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1174238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1175a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1176a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
117776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
117876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
117976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
118076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1181e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1182e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
11832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1184d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1185d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1186d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
11876b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
11886b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1189d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1192d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
119324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
119424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11962c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
11972c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
11982c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12006b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12012c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12022c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
12036b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12046b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1205b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1206b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1207b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1208b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1209b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1210b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
121179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
121279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
121379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12142a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
121548dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12164eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
12174eab39f0745fb1949dbb40c4145771b927888242John McCall  llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
12184eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
12194eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
12204eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
12214eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
12224eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
12234eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
12244eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
12254eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
12264eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
12274eab39f0745fb1949dbb40c4145771b927888242John McCall    }
12284eab39f0745fb1949dbb40c4145771b927888242John McCall  }
12294eab39f0745fb1949dbb40c4145771b927888242John McCall
12300ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
123121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
123221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
123321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12342dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
123521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12362dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1237723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1238723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1239723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
12405f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12415f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12425f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12435f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12445f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12455f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1246723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
12475f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
124812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    llvm::SmallVector<QualType, 4> ParamTypes;
124912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
125012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               D->getNumParams(), TemplateArgs, ParamTypes,
125112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
125212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      return 0;
12535f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
12545f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1255b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1256b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1257b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1258b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1259b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1260b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1261b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1262b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1263b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1264b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1265b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1266b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1267b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1268b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1269b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1270c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1271c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1272c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1273b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1274b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1275b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1276b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1277b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1278b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1279b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1280b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
12812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1282b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1283dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
12841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
12862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
128717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
12892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                        NameInfo, T, TInfo,
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
129116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
129216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
129317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
129417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1295b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                       NameInfo, T, TInfo,
12962577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
129716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
129865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
129965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
13002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T, TInfo,
13010130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
130265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1303dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
13052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   NameInfo, T, TInfo,
130616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
130716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
130816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1309dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13106b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1311b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1312b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1313b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1314d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1315d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1316d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1318d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1319d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1320d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1321d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1322d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1323d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1324d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1325d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1326d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1327d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1328d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1329d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1331d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1332b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1333b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1334b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1335b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1337d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
133866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
133966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
134024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
134124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1342838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1343910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1344910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1345910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
134666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1347b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
134866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13492db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
135066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
135166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
13537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
13547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1355b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
13564eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
13574eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
13584eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
13594eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
13604eab39f0745fb1949dbb40c4145771b927888242John McCall
1361b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1362b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1363b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
13647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
13651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
13675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
13685545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1369838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
13705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
13715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
13725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
13732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
13742577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
13752577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1377b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1378b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1380dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1381dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1382dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1383dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13846826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13856826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1386dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13872dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
138865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
138965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
13909f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
139165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
139265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
13934ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
13944ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
13954ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
139646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
139746460a68f6508775e98c19b4bb8454bb471aac24John McCall
1398b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1399b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1400b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1401b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1402b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1403b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1404b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1405b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1406b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1407b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1408b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1409b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1410b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1411bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
14122dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1415615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1416dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1417615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1418615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
141903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
142017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
142103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
142203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1423bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
142465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1425bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1426bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14276477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1428cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
14292dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1431e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1432e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1433e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1434efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const Type* T = D->getTypeForDecl();
1435efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  assert(T->isTemplateTypeParmType());
1436efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1438e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1439e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
144071b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                 TTPT->getDepth() - TemplateArgs.getNumLevels(),
144161139c561f1a2c872209e32ff9143487cebf4324Nick Lewycky                                 TTPT->getIndex(), D->getIdentifier(),
1442e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1443e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1444e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
14450f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14460f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1447e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1448550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1449550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1450550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1451550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1453e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
145533642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
145633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
145733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
145833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1459a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
146033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
146133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
146233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
146333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
146433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
146533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
146633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
146733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
146833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
146933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
147033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
147133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
147233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
147333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
147433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
147533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
147633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
147733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
147833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
147933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
148010738d36b150aa65206890c1c845cdba076e4200Douglas Gregor  // FIXME: Variadic templates.
148133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
148233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
148371b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
148471b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                      D->getPosition(), D->getIdentifier(), T,
148510738d36b150aa65206890c1c845cdba076e4200Douglas Gregor                                      D->isParameterPack(), DI);
148633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
148733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
148833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1489d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1490550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1491550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1492550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1493550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
149433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
149533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
149633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
14970dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
14989106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
14999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
15009106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
15019106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
15029106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
15039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
15049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
15059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
15062a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
15079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
15089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
15099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
15109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15129106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
15139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
15149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
151571b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                   D->getDepth() - TemplateArgs.getNumLevels(),
151661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getPosition(), D->isParameterPack(),
151761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1518d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
15194469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
15209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
15219106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
15229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
15239106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15249106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
15259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
15269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
152748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
152848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
152948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
153048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
153148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
153248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
153348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
153448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
153548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
153648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
153748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
153848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
153948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
154048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1541ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
15421b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
15431b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
15441b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
15451b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
15461b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
15471b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
15481b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
15491b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
15501b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
15511b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  NestedNameSpecifier *NNS =
15521b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
15531b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      D->getNestedNameRange(),
15541b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      TemplateArgs);
15551b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  if (!NNS)
15561b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      return 0;
15571b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
15581b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
15591b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1560ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1561ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15629f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
15639f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
15649f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
15659f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
15669f54ad4381370c6b771424b53d219e661d6d6706John McCall
1567ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1568ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
15699f54ad4381370c6b771424b53d219e661d6d6706John McCall
1570ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1571ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1572ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
15731b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor                                       NNS,
1574ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1575ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1576ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1577ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
15781b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  SS.setScopeRep(NNS);
1579ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
15809f54ad4381370c6b771424b53d219e661d6d6706John McCall
15819f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
15829f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
15839f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
15849f54ad4381370c6b771424b53d219e661d6d6706John McCall
15859f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
15869f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
15879f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
15889f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
15899f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
15909f54ad4381370c6b771424b53d219e661d6d6706John McCall
15919f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
15929f54ad4381370c6b771424b53d219e661d6d6706John McCall
15939f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
15949f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1595ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1596ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
15979f54ad4381370c6b771424b53d219e661d6d6706John McCall
1598ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1599ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1600ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1601ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16029f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
16039f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
16049f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
16059f54ad4381370c6b771424b53d219e661d6d6706John McCall
1606323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1607323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
16089f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
16099f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
16109f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
16119f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
16129f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
16137c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
16147c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
16159f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
16169f54ad4381370c6b771424b53d219e661d6d6706John McCall
16179f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
16189f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
16199f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
16209f54ad4381370c6b771424b53d219e661d6d6706John McCall
16219f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
16229f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
16239f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1624323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1625323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1626323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
16279f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1628ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1629ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1630ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1631ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1632ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
16339f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
16349f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1635ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1636ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
16397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
16407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
16427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
16437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
16447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
16457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
16477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
16487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
16497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1650ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1651ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1652ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
16537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
16547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1655ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
16584469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1659ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1660ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
16627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
16637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16647ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
16661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
16690dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
16700dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
16710dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16730dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
16740dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
16750dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1677ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1678ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1679ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
16819488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1682ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
16854469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1686ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1687ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
16890dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
16900dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1691ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1692d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
16937e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
16942fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
16952fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
16962fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
16978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
16988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
16998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1700e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1701e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1702e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1703e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1704e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1705e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1706e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1707ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1708e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1710e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1711e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1712bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1713e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1714e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1717bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1718e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
17199148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1720e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1721e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1722e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1723ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1724e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1725e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1726e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1727e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1728e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1729e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1730e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
17311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1739ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1742d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
1743d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
1744d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
1745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1748550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1749550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1750550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
17512a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1752550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1753ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1754ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1755ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1756ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1757ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1758d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1759ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1760ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1762d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1763e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1764e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    PartialSpec->getNumTemplateArgsAsWritten(),
1765e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
1766e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
1767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1770910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Converted;
1771ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1773d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1774ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1775ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1776d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1780ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1782910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
1783910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
1784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1789910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
1790910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
1791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1795ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1798ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
17993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
18003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
18013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
18023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1803d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1821ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1822ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1823d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1826d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
183213c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
183313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
183413c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1838910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
1839910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
1840d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
18413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1842dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1843cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1844b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1845b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1846b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1847b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
18494469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
18504469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1853cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1854d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
1855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
185721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
185821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
185921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
186021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
186121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
186221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
18636cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
18646cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
18656cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
18666cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
186721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
186821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
18695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1870cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1871cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1872140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
18736920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
18746920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1875140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
18766920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
18776920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
187812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
187912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
188012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
188112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
188212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        if (!OldParam->isParameterPack() ||
188312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor            (NewIdx < NumNewParams &&
188412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
188512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // Simple case: normal parameter, or a parameter pack that's
188612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
188712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
188812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
188912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
189012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                               NewParam);
189112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          continue;
189212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
189312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
189412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        // Parameter pack: make the instantiation an argument pack.
189512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
189612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      OldParam);
189721371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        unsigned NumArgumentsInExpansion
189821371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
189921371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor                                               TemplateArgs);
190021371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        while (NumArgumentsInExpansion--) {
190112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
190212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
190312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
190412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      NewParam);
190512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
19066920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1907895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1908cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1909cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1910cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1911cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1912140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
19136920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19146920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
19156920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
19166920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
19176920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
19186920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
19196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
19206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1921cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1922cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
192321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
19245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1927e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1928e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1929e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
19301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1932e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1933e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1934e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1936cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1937cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1938cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
19391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
19401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1941cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1942cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1943cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1944cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1945cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1946cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1947cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
19481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1949cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
19501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1951cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1952bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1953cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1954cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1955f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1956cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1957cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
19581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
19600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
19610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
19630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
19640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
19650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
19660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
19670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
19680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
1969b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
1970b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
1971b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
1972b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1973b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
1974b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
1975b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        assert(!Unexpanded.empty() &&
1976b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
1977b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
1978b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
1979d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        bool RetainExpansion = false;
1980cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        llvm::Optional<unsigned> NumExpansions
1981cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                          = PackExpansion->getNumExpansions();
1982b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
1983b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
1984b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.data(),
1985b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.size(),
1986b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
1987d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    Expand,
1988d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    RetainExpansion,
1989d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    NumExpansions))
1990b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
1991b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
1992b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
1993b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
1994cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // just substitute into the pattern and create a new pack expansion
1995cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // type.
1996b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1997b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
1998b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
1999b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2000b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2001b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2002b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2003cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2004b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2005b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2006b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2007b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2008b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2009b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2010cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2011b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2012b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2013b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2014b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2015b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2016b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2017b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2018b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2019b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
2020b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2021b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2022b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2023b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2024b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2025b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
2026b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2027b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2028b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2029b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
20300ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
20310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
20320ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
20330ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
20340ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
20350ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
20360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
20380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
20390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
20410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2042e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2043e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2044e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2045e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2046e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
2047e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2048e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
20490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
20500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
20510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
20520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
20530ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
20540ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2055e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
20560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
20570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20581d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
20597cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2060e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2061e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2062e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
20635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
20645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
20655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
20665545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
20675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
20681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
20691eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
20705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2071e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2072e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
20731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2075e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
207685606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
20775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
20795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
20805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
20815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2082a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2083a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2084a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2085a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2086b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2087b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2088b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2089b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2090a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2091b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2092b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2093b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2094b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2095b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2096e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2097e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2098e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2099e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2100f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2101b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2102e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2103e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
210406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
210554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
210654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2107251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2108251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2109251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
21106cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
21111eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
21123b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
21131eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
21141eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
21156fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
21161eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2117e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2118e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2119e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2120e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2121e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2122e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2123e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2124e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2125e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2126e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2127e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2128e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2129e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2130e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2131cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
213258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
213358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
213462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
213558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2136e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
213758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
21381eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2139e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
21401eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2141d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2142d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
21431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2144d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
21451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2146d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
21477ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2148d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2150f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2151f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2152e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2153e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2154b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2155b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2156b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
21572a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  llvm::SmallVector<VTableUse, 16> SavedVTableUses;
215862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
21592a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
21602a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
216162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
21622a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21649679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
2165f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2166d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2167e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
216854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
216960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
217060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
217160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
217260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
217360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
217460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
217560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
217660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
21771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
217854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
21798a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
21808a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
218112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  unsigned FParamIdx = 0;
21828a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
21838a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
218412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (!PatternParam->isParameterPack()) {
218512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      // Simple case: not a parameter pack.
218612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      assert(FParamIdx < Function->getNumParams());
218712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
218812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
218912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocal(PatternParam, FunctionParam);
219012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ++FParamIdx;
219112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      continue;
219212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
219312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
219412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    // Expand the parameter pack.
219512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    Scope.MakeInstantiatedLocalArgPack(PatternParam);
219612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    for (unsigned NumFParams = Function->getNumParams();
219712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         FParamIdx < NumFParams;
219812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         ++FParamIdx) {
219912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
220012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
220112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
220212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
22038a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
220454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2205b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2206b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2207b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2208b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2209b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
22101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2211e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2212090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2213090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2215090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2216090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2217090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
22181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
222054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
222160d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2222e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
222352604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
222452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
222552604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
22269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ActOnFinishFunctionBody(Function, Body.get(),
2227e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2228b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
22290c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
22300c01d18094100db92d38daa923c95661512db203John McCall
2231b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2232aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2233aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2234aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
22351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
223660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
223760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
223862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
223960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
224060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2241b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
22422a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
22432a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
22442a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2245b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
22461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
224762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22492a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
22502a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
22512a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2252b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
225362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2254b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2255a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2256a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2257a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2258a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2259a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
22607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
22617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
22627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
22637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
22657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
22667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
22687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2269e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2270e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2271e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2272e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
22737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
22747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
22757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2276e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2277e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
22787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
22797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
22827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
22837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
22840d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
22850d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
22861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22870d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
22887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
22897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2292e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
22930d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2294e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2295e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2296e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2297e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
229858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
229958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
230062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
230158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
230258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
230358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
23047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
23057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
23067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2307251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
23081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2309251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2310251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2311251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2312251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2313251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2314251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
23151028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2316251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2317251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
23181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
23207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
23217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
23221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
23247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
23257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
232662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
23277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
232862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
23291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
23317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
23327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
23337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23351028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2336ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
23376bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
23387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
23397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
23407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2341583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2342583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2343583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2344583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
23457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
23467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
23477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
23481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
23507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
235262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
23531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
235562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
23561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2357a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2358815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2359090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2360090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2361090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2362090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
23631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2364090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
23659db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
23669db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2367090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2368090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
236972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
237072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2371cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2372090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2373030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2374030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2375030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2376030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2377030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
23786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2379ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
23801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
23823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
23833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
23843fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
23853fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
23863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
23873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
23883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2389d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2390cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
23913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
23923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
23933fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.data(),
23943fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.size(),
23953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          TemplateArgs, ShouldExpand,
2396d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
23973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
23983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
23993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
24003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
24013fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
24023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
24033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // Loop over all of the arguments in the argument pack(s),
2405cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
24063fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
24073fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24083fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
24093fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
24103fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                   LParenLoc, NewArgs, RParenLoc)) {
24113fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
24123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
24133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
24143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24153fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
24163fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
24173fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              TemplateArgs,
24183fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              Init->getSourceLocation(),
24193fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
24203fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
24213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
24223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
24233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
24243fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
24263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
24273fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     BaseTInfo,
24283fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     (Expr **)NewArgs.data(),
24293fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     NewArgs.size(),
24303fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getLParenLoc(),
24313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getRParenLoc(),
24323fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
24333fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
24343fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
24353fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
24363fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
24373fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
24383fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
24403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewArgs.clear();
24413fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
24423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24433fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
24443fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
24453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24466b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
24476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2448a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
24496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
24506b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2451090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
24529db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2453090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2454090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2456802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2457802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2458802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2459a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
24609db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2461802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2462802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2463802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2464802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2465a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
24661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2467090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2468802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2469090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
24703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     New->getParent(),
24713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     EllipsisLoc);
2472090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
247300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
247400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
247500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
247600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
24771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2479090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2480090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2481802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2482090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
248300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
248400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
248500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet         cast<IndirectFieldDecl>(FindInstantiatedDecl(
248600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
248700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
248800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
248900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
249000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       NewArgs.size(),
249100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getSourceLocation(),
249200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getLParenLoc(),
249300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getRParenLoc());
2494090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2495090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
24969db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
24979db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2498090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
24999db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2500090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2501090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2503090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2504090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2505090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
25061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2507090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2508d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2509090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2510090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
25119db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
25129db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2513090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2514090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
251552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
251652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
251752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
251852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
251952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
252052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
252152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
252252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
252352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
252452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
252552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
252652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
252752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
252852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
252952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
25300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
25310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
25320d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
25330d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
25340d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
25350d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
25360d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
25370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
25380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
25390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
25400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
25410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
25420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2543ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2544ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2545ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2546ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2547ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2548ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2550ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2551ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2552ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2553ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2554ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2555ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2557ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2558ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
255952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
256052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
256152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
256252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
256352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
256452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
256552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
256652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
256752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
256852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
256952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
257052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
257152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
257252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
257352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
257452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
257552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
257652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
257752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
257852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
257952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
258052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
258152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
258252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
258352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
258452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
258552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
258652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
258752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
258852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
258952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
259052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
259152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
259252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
259352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
259452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
259552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
259652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
259752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2598ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2599ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2600ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2601ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2602ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2603ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2604ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2605ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2606ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2607ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2608ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2609ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
26107ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
26117ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
26127ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2613ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
26147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
26157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
26167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
26170d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
26180d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2619ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
26200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
26210d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
262252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
262352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
262452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
262552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
262652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
262752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
262852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
262952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
263052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
263152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
263252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
263352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
263452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
263552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
263652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2637ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2638ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2639815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
26400d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
26417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
26427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
26437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
26447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
26457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
26467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
26477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
26487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
26497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
26500d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
26510d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
26520d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
26530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2654815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
26550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
26560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
265952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
26601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
266152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
266252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2663815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
266452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
266552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2666815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
26677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
266852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
266952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
267052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
267152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
267252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2673a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
26740d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
26750d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
26760d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2677ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2678ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2679ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2680ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2681ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2682d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2683d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2684d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
26851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2686d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2687d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2688d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
26891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2690ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2691ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2692ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2693ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2694ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2695ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2696815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2697815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2698815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2699815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2700815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
27011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2702815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2703815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2704815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2705815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2706815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2707815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2708815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2709815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2710815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2711815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
271202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
271302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
271402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
271502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
27167c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2717e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
271802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
27197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
272002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
272102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
272202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
272302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2724ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2725ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2726815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2727815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2728815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2729815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2730815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2731815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2732815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2733815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2734815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2735815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2736815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2737815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2738815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2739815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2740815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2741815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2742815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2743815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2744815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2745815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2746815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2747815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2748ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2749ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
27507c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2751e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2752815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2753550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
27546d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2755766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
27562bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
27572bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
27588dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
27592bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2760815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2761e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2762e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2763e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2764e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
27658b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
27668b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
27678b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
27688b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
27698b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2770e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2771e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2772e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2773e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
277424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2775e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2776e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2777e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
27783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
27793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
27803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
27813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
27823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
27833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
278431f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
27853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2786e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2787e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
27888b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
27898b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
27908b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
27918b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
27928b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
27938b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
27948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
27958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
27968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2797e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2798e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2799e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2800e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2801e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2802e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2803e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2804e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
28058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
28068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
28078b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
28088b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
28098b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
28108b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
28118b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
28128b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
28131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
281452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
28151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
28168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2817e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2818e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
281952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
28208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
28218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
28228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
282352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
282452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
28258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
28268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
28278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
28288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
28298b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
28308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
28318b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
283252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
28338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
283452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
28358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2836e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2837e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2838e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
283952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2840e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2841e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2842e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
28437c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
28441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
284544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
28461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2847815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2848815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2849815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2850815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
28517c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
28523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
28533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
28543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
28557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
28567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
28573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
28583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
28593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
28603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
28613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
2862a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
2863a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
28647c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
28657c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
28667c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2867815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2868815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
286917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2870815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2871815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2872815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2873815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2874815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2875815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2876815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2877815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2878815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2879815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
28801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
288117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
288217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2883815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
28841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28859f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
288600225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
288700225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
28889f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
28899f54ad4381370c6b771424b53d219e661d6d6706John McCall
2890815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2891815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2892815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2893815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2894815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2895d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
28961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2897d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
289862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruthvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
289960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
290062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
290160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
290260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
290360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
290462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
290562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
290660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
290760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
290860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
290960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
29101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
29127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
2913f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2914f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
291558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
291658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
291758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
291858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
29197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
29207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
29211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
29237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
29247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2925c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2926291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2927291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2928291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2929291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2930291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2931291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2932291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2933291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2934291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2935291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2936291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2937291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
293858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
293958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
294058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
294158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
294258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
2943291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2944291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2945291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2946291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2947f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2948f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
2949f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
29501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
295158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
295258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
295358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
295458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
2955d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2956d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
29570c01d18094100db92d38daa923c95661512db203John McCall
29580c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
29590c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
29600c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
29610c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
29620c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
29630c01d18094100db92d38daa923c95661512db203John McCall
29640c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
29650c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
29660c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
29670c01d18094100db92d38daa923c95661512db203John McCall      break;
29680c01d18094100db92d38daa923c95661512db203John McCall    }
29690c01d18094100db92d38daa923c95661512db203John McCall  }
29700c01d18094100db92d38daa923c95661512db203John McCall}
2971