SemaTemplateInstantiateDecl.cpp revision d8e54990ade0dd5566f8e3aa2e62def08753d1e9
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
146cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
147cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
148cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
149cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
150cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    if (oldTag->getTypedefForAnonDecl() == D) {
151cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
152cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      assert(!newTag->getIdentifier() && !newTag->getTypedefForAnonDecl());
153cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      newTag->setTypedefForAnonDecl(Typedef);
154cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
155d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
156d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
1575126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1587c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1597c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
1605126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1615126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1625126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1631d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
164d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
16546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
16617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
1726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
1736b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
1756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
1776b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
1796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
1806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1816b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
1826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
1846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
1856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
1866b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
1877663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                   ASTOwningVector<Expr*> &NewArgs,
1886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
1896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
1906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
1916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
1926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
1946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
1956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1964765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
1976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
1986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
1996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
20891fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor    return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
20991fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                        true, TemplateArgs, NewArgs);
2106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
21328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
21491fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor      if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
21591fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                       TemplateArgs, NewArgs))
21628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
21728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
21828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
21928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
220a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
22128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
22228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
2236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
22560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
2266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
2276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
2286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
2306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
2316b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
2326b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2333d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2359901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
2369901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2379901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2389901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2399901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
2409901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
241ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
242a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2430a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2440a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2450a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2460a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2483d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
249c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
250c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
251c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
252c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
253c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
254c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor
255b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
2573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2580a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
25916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
26016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
2613d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2623d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
265b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
266b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
267b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27446460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
275c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
276c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
277c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
2784469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
279390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
280390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
2813d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
2826826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
2836826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
284449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
28560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
28660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
2876826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
290ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
291ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
2927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
2937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
2947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
295f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
296f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
2977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
2981d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
2998dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
300251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
301251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
302251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
303251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
304cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
305251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
30660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
30760c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
30860c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3091f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3101f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3111f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3121f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3131f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
316ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
3176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
318a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                InitArgs, RParenLoc)) {
31907a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      // Attach the initializer to the declaration, if we have one.
32007a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      if (InitArgs.size() == 0)
32107a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        SemaRef.ActOnUninitializedDecl(Var, false);
32207a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      else if (D->hasCXXDirectInitializer()) {
3236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
324d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
3256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3266eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
32807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      } else {
32907a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        assert(InitArgs.size() == 1);
3309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
3319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        SemaRef.AddInitializerToDecl(Var, Init, false);
33283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
3336eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3366b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
3376eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3386eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3391f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
34065b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
341d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
3423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3435764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
3445764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
3455764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
346bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
3473d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3483d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3493d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3506206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3516206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3526206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3536206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3546206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3556206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3566206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3576206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
360a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
361836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
362836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
36307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
36407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
36507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
366a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
36707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
36807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
37607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
379b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
380b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
3818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
3848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
3858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
3868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
387ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
388f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
391ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
396e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
3978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
3988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
39907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
40007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
405ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
408663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
409663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
410f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
411663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4131d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
414d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
415f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
416f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
418f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
419f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
420f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4219901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
4229901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4239901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4247a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4259901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
428f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
42946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
430f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
43587c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
43687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
43787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
43887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
43987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
44087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
44187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
44287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PI != PE; ++PI)
44387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
44487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                            *PI, TemplateArgs));
44587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
44640e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
44787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
44887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
44940e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
45087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
45187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
45487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
45587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
45687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
45787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
45887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
45902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
46002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
46106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
46232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
46332f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
46432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
46532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
46606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
46706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
468fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
46906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
47006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
47106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
47206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
47306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
4749a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
47506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
47606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
47706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
47806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
47906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
48006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
48132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
482af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
483af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
484af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
485af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
486af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
48706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
49006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
49106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
4925fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
4939a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
49402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
496fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
497fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
4988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
4998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
501ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
502f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
505ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
50960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
5103fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
511d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5129ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
5139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              Message.get());
5148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
519741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
520a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
521a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
5221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
5231274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
5241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
5251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
5261274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
5271274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
5281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
5291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
5301274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
5311274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
5321274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
5341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
5351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
5371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
5381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
5391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
5401274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5411274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
5421274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5435b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
5445b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5458dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
54606c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
547b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
54817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
55196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
55296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
55396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
554d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
5558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
55717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
55817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
56160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
562ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
563ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
565f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
567ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
568ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
5788dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
5798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
5809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
5818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
5895b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
5905b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5913b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
59217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
593d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
59596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
59696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
59796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
59896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
59996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
60096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
604c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
605fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
606c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
607d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
608de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
609fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6146477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6156477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6166477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6176477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6186477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
619e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
62093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
62193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
622550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
623550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
6242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
625e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
626ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
628d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
629e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
630e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
63193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
63293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
63393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
63493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
63593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
63693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
63793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
63893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
63993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
64093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
64193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
64293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
64393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
64493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
64593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
64637574f55cd637340f651330f5cfda69742880d36Nick Lewycky  if (!isFriend && Pattern->getPreviousDeclaration()) {
64737574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
64837574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
64937574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
65037574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
65137574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
65237574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
65337574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
65437574f55cd637340f651330f5cfda69742880d36Nick Lewycky
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
67093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
67193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
67293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
67893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
6871eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
6881eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
692c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
694c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
695c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
696c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
697c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
698c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
699c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
700c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
701c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
702c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
703c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
704c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
705c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
706c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
707c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
708c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
709c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
710c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
711c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
712c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
713c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
714c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
715c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
716c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
717c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
718c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
719c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
725c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
727c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
728c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
729c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
730c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
731c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
732c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
736c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
737c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
73893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
73993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
74093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
74693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
747f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
74993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
75093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
751b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
756e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
757ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
759ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
760ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
761ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
762ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
763ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
768e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
76937574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
77037574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
772f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
773f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
77524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
776ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
777259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
780e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
781259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
782e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
783e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
784d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
785d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
786d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
787d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
788d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
789d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
790d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
791d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
792d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
793d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
794d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
795d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
796e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
797e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
799d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8007974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8017974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
802ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
803ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
809ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
812ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
815ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
816d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
817d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
818d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
819d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
820d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
8217974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8227974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8237974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
824d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
825550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
826550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
827550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
828550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
8292a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
830895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
831d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
832d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
834d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
836a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
837a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
838a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
839a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
840a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
841a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
842a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
843a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
844a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
845a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
846d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
847d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
84846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
84946460a68f6508775e98c19b4bb8454bb471aac24John McCall
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
851d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
85237d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
853a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
85437d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
855a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
856a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
857e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
858b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
859b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
860e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
861e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
862e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
863b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
864a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
865a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
866b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
867b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
868a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
869b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
871d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
872d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
873d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
874d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
875d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
876d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
8776c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
8787c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
8797c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
8806c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
8816c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
8826c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
8836c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
884d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
885d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
8861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
887741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
888741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
889b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
890b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
891b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
892b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
893b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
894d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
895eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
896eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
897eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
898eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
899eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
900d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
901f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
902d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
90302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
90402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
90502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
90602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
90702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9089901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9099901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9109901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9117a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9129901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9139901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
914d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
91517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
916d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
917d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
918d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
91902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
92002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
92102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
92202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
92302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9247557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
925a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
926127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
927127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
928127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
929127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
930b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
93124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
93224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9342c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9352c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9362c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
938127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9392c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9402c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
941127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
943b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
944b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
945b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
946b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
947b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
948b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
94979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
950b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
95179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
95279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
9532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
955e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
95621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
95721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
95821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9592dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
96021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
961fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
962d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
963d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
964d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
965d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
966d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
967d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
968d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
969d325daa506338ab86f9dd468b48fd010673f49a6John McCall
97068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
97168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
97268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
97368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
97468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
975d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
976d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
977d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
978d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
979d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
980d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
981d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
9827c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
9837c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
984d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
98568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
98602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
9871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
98821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
98916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
9900130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
991b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
992d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
993d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
994b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
995b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
996b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
997b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
998b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
999b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1000b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1001b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1003e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1004e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
10053019c444c672938c57f5573840071ecd73425ee7John McCall    if (Params[P])
10063019c444c672938c57f5573840071ecd73425ee7John McCall      Params[P]->setOwningFunction(Function);
1007838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
100802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1009ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1010a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1011a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1012a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1013a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1014a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1015a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1016a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1017a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1018a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1019a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1020a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1021a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1022a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1023a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1024a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1026a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1027a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1028a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1029a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1030b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1031b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1032d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1033d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1034d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1035d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1036d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1037d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1038d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
103966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
104066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
104124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
104224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1043838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1044910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
104524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
104624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
104766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1048d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1049d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1050d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1051d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
105202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1054e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1055e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1057e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1058af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10606826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10626826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1063af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1064af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1065af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1066af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1067af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1068af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1069af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1070af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1071af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1072af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1073e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1074e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1075e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1076af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1077af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1078af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1079af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1080af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1081af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1082af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1083af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1084af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1085af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1086af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1087af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1088af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1089af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1090af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1091af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1092af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1093af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1094af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1095a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1096a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1097a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
10986826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1099a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1100a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1101a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1102a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1103a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11046826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11056826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1106a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1107a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11089f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1109c80e8115278ba1537c8b517a083ecbd0a018b579Peter Collingbourne                                   isExplicitSpecialization, Redeclaration);
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;
1389c80e8115278ba1537c8b517a083ecbd0a018b579Peter Collingbourne  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
139065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
13914ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
13924ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
13934ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
139446460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
139546460a68f6508775e98c19b4bb8454bb471aac24John McCall
13969eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
13979eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
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) {
14286a24bfda084f06a0b252b7befe8cbb17fce7f94eDouglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs, llvm::Optional<unsigned>());
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.
14586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
14596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
14606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
14616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
14626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeSourceInfo *DI;
146333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
146433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
14656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
14666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
14676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack is an already-expanded pack
14686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
14696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
14706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
14716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
14726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
14736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
14746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getLocation(),
14756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
14766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
14776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
14786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
14796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
14806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
14816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
14826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
14836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
14846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
14856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
14866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
14876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
14886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
14896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
14906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else if (isa<PackExpansionTypeLoc>(TL)) {
14916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
14926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
14936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
14946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
14956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
14966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
14976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
14986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
14996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
15006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
15016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
15026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
15036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> OrigNumExpansions
15046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
15056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
15066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
15076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
15086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Unexpanded.data(),
15096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Unexpanded.size(),
15106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
15116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Expand, RetainExpansion,
15126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
15136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
15146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
15166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
15176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
15186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
15196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation(),
15206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
15216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
15226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
15236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
15256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
15266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
15276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
15286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
15296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
15306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
15316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
15326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
15346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
15356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
15366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
15376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
15386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
15396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
15406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
15416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
15426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
15436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
15446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getLocation(),
15456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
15466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
15476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
15506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
15516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
15526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
15556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
15566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
15576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
15586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
15596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
15606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
15616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
15626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
15646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Invalid = false;
15656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
15666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
15676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
15686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
15696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
15706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
157133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
157233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
15736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
15746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
15756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
15766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
157771b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
15786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getPosition(),
15796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
15806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
15816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
15826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
15836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
15846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
15856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
15866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
15876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
15886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getPosition(),
15896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
15906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
15916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
159233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
159333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
159433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1595d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1596550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1597550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1598550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1599550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
160033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
160133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
160233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
16030dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
16049106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
16059106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
16069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
16079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
16089106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
16099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
16109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
16119106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
16122a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
16139106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
16149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
16159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
16169106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
16179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
16189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
16199106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
16209106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
162171b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                   D->getDepth() - TemplateArgs.getNumLevels(),
162261c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getPosition(), D->isParameterPack(),
162361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1624d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
16254469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
16269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
16279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
16289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
16299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
16309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
16319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
16329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
163348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
163448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
163548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
163648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
163748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
163848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
163948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
164048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
164148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
164248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
164348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
164448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
164548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
164648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1647ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
16481b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
16491b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
16501b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
16511b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
16521b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
16531b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
16541b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
16551b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
16561b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
16571b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  NestedNameSpecifier *NNS =
16581b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
16591b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      D->getNestedNameRange(),
16601b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      TemplateArgs);
16611b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  if (!NNS)
16621b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      return 0;
16631b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
16641b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
16651b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1666ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1667ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16689f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
16699f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
16709f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
16719f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
16729f54ad4381370c6b771424b53d219e661d6d6706John McCall
1673ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1674ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
16759f54ad4381370c6b771424b53d219e661d6d6706John McCall
1676ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1677ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1678ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
16791b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor                                       NNS,
1680ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1681ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1682ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1683ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
16841b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  SS.setScopeRep(NNS);
1685ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
16869f54ad4381370c6b771424b53d219e661d6d6706John McCall
16879f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
16889f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
16899f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
16909f54ad4381370c6b771424b53d219e661d6d6706John McCall
16919f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
16929f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
16939f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
16949f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
16959f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
16969f54ad4381370c6b771424b53d219e661d6d6706John McCall
16979f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
16989f54ad4381370c6b771424b53d219e661d6d6706John McCall
16999f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
17009f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1701ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1702ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
17039f54ad4381370c6b771424b53d219e661d6d6706John McCall
1704ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1705ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1706ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1707ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17089f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
17099f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
17109f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
17119f54ad4381370c6b771424b53d219e661d6d6706John McCall
1712323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1713323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
17149f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
17159f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
17169f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
17179f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
17189f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
17197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
17207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
17219f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
17229f54ad4381370c6b771424b53d219e661d6d6706John McCall
17239f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
17249f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
17259f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
17269f54ad4381370c6b771424b53d219e661d6d6706John McCall
17279f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
17289f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
17299f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1730323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1731323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1732323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
17339f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1734ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1735ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1736ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1737ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1738ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
17399f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
17409f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1741ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1742ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
17447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
17457ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
17467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
17477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
17487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
17497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
17507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
17517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
17527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
17537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
17547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
17557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1756ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1757ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1758ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
17597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
17607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1761ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
17627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
17637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
17644469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1765ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1766ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
17687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
17697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
17707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
17717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
17721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
17731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
17741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
17750dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
17760dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
17770dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
17781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17790dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
17800dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
17810dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
17821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1783ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1784ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1785ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
17861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
17879488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1788ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
17897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
17907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
17914469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1792ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1793ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17940d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
17950dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
17960dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1797ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1798d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
17997e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
18002fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
18012fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
18022fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
18038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
18048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
18058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1806e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1808e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1812e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1813ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1814e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1815e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1816e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1817e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1818bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1819e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1820e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1821e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1822e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1823bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1824e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
18259148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1826e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1827e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1828e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1829ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1830e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1831e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1832e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1833e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1834e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1835e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1836e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
18371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1838e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1840ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1841ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1842ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1848d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
1849d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
1850d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
1851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1854550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1855550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1856550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
18572a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1858550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1861ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1862ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1863ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1864d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1865ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1866ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1867ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1868d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1869e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1870e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    PartialSpec->getNumTemplateArgsAsWritten(),
1871e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
1872e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
1873ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1874ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1875ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1876910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Converted;
1877ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1878ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1879d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1880ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1881ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1882d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1883ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1884ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1885ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1886ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1887ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1888910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
1889910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
1890ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1891ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1892ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1893ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1895910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
1896910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
1897ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1898ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1899ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1900ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1901ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1902ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
19053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
19063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
19073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
19083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1909d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1910ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1911ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1912ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1913ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1914ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1915ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1916ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1917ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1918ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1919ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1920ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1921ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1922ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1923ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1924ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1925ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1926ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1927ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1928ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1929d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
1930ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1931ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1932d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1933ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1934ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1935ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1936ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1937ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
193813c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
193913c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
194013c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1941ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1944910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
1945910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
1946d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
19473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1948dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1949cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1950b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1951b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1952b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1953b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1954ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
19554469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
19564469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1958ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1959cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1960d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
1961ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1962ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
196321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
196421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
196521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
196621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
196721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
196821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
19696cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
19706cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
19716cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
19726cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
197321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
197421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
19755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1976cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1977cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1978140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
19796920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19806920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1981140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
19826920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
19836920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
198412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
198512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
198612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
198712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
198812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        if (!OldParam->isParameterPack() ||
198912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor            (NewIdx < NumNewParams &&
199012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
199112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // Simple case: normal parameter, or a parameter pack that's
199212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
199312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
199412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
199512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
199612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                               NewParam);
199712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          continue;
199812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
199912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
200012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        // Parameter pack: make the instantiation an argument pack.
200112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
200212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      OldParam);
200321371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        unsigned NumArgumentsInExpansion
200421371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
200521371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor                                               TemplateArgs);
200621371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        while (NumArgumentsInExpansion--) {
200712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
200812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
200912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
201012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      NewParam);
201112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
20126920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2013895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2014cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2015cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2016cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2017cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2018140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
20196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
20206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
20216920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
20226920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
20236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
20246920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
20256920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
20266920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2027cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2028cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
202921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
20305545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
20315545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2033e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2034e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2035e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2038e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
2039e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
2040e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2042cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2043cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2044cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
20461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2047cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2048cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2049cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2050cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2051cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2052cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2053cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
2055cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2057cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2058bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2059cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2060cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
2061f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
2062cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2063cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
20641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
20660ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
20670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
20690ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
20700ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
20710ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
20720ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
20730ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
20740ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
2075b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
2076b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2077b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
2078b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2079b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2080b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
2081b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        assert(!Unexpanded.empty() &&
2082b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
2083b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2084b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
2085d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        bool RetainExpansion = false;
2086cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        llvm::Optional<unsigned> NumExpansions
2087cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                          = PackExpansion->getNumExpansions();
2088b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2089b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
2090b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.data(),
2091b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.size(),
2092b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
2093d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    Expand,
2094d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    RetainExpansion,
2095d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    NumExpansions))
2096b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
2097b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2098b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
2099b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
2100cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // just substitute into the pattern and create a new pack expansion
2101cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // type.
2102b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2103b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2104b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2105b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2106b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2107b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2108b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2109cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2110b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2111b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2112b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2113b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2114b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2115b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2116cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2117b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2118b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2119b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2120b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2121b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2122b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2123b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2124b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2125b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
2126b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2127b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2128b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2129b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2130b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2131b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
2132b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2133b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2134b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2135b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
21360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
21370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
21380ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
21390ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
21400ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
21410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
21420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
21430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
21440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
21450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
21460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
21470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2148e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2149e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2150e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2151e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2152e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
2153e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2154e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
21550ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
21560ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
21570ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
21580ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
21590ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
21600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2161e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
21620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
21630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
21641d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
21657cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2166e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2167e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2168e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
21695545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
21705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
21715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
21725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
21735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
21765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2177e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2178e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
21791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2181e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
218285606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
21835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
21845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
21855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
21865545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
21875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2188a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2189a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2190a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2191a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2192b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2193b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2194b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2195b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2196a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2197b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2198b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2199b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2200b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2201b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2202e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2203e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2204e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2205e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2206f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2207b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2208e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2209e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
221006a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
221154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
221254dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2213251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2214251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2215251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
22166cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
22171eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
22183b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
22191eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
22201eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
22216fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
22221eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2223e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2224e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2225e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2226e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2227e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2228e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2229e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2230e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2231e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2232e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2233e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2234e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2235e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2236e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2237cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
223858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
223958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
224062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
224158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2242e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
224358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
22441eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2245e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
22461eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2247d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2248d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
22491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2250d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
22511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2252d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
22537ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2254d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
22551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2256f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2257f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2258e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2259e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2260b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2261b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2262b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
22632a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  llvm::SmallVector<VTableUse, 16> SavedVTableUses;
226462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
22652a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
22662a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
226762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
22682a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
22691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22709679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
2271f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2272d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2273e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
227454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
227560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
227660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
227760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
227860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
227960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
228060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
228160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
228260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
22831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
228454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
22858a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
22868a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
228712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  unsigned FParamIdx = 0;
22888a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
22898a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
229012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (!PatternParam->isParameterPack()) {
229112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      // Simple case: not a parameter pack.
229212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      assert(FParamIdx < Function->getNumParams());
229312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
229412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
229512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocal(PatternParam, FunctionParam);
229612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ++FParamIdx;
229712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      continue;
229812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
229912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
230012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    // Expand the parameter pack.
230112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    Scope.MakeInstantiatedLocalArgPack(PatternParam);
230212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    for (unsigned NumFParams = Function->getNumParams();
230312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         FParamIdx < NumFParams;
230412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         ++FParamIdx) {
230512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
230612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
230712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
230812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
23098a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
231054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2311b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2312b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2313eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  Sema::ContextRAII savedContext(*this, Function);
2314b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
23151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2316e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2317090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2318090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2320090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2321090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2322090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
23231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
23241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
232554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
232660d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2327e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
232852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
232952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
233052604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
23319ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ActOnFinishFunctionBody(Function, Body.get(),
2332e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2333b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
23340c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
23350c01d18094100db92d38daa923c95661512db203John McCall
2336eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  savedContext.pop();
2337aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2338aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2339aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
23401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
234260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
234362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
234460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
234560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2346b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
23472a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
23482a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
23492a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2350b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
235262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
23531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23542a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
23552a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
23562a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2357b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
235862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2359b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2360a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2361a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2362a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2363a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2364a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
23657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
23667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
23677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
23687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
23697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
23707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
23717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
23727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
23737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2374e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2375e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2376e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2377e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
23787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
23797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
23807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2381e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2382e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
23837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
23847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
23851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
23877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
23887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
23890d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
23900d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
23911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23920d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
23937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
23947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
23951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2397e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
23980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2399e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2400e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2401e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2402e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
240358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
240458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
240562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
240658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
240758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
240858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
24097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
24107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
24117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2412251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
24131028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2414251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2415251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2416251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2417251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2418251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2419251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
24201028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2421251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2422251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
24231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
24257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
24267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
24271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
24297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
24307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
243162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
24327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
243362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
24341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
24367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
2437f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
24381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24391028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2440ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
24416bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
2442f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
2443f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
24447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
24457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2446583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2447583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2448583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2449583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
24507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
24517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
24527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
24531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
24557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
24561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
245762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
24581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
246062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
24611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2462a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2463815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2464090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2465090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2466090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2467090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
24681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2469090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
24709db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
24719db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2472090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2473090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
247472f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
247572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2476cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2477090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2478030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2479030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2480030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2481030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2482030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
24836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2484ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
24851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
24873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
24883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
24893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
24903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
24913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
24923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
24933fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2494d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2495cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
24963fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
24973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
24983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.data(),
24993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.size(),
25003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          TemplateArgs, ShouldExpand,
2501d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
25023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
25033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
25043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
25053fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
25063fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
25073fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
25083fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25093fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // Loop over all of the arguments in the argument pack(s),
2510cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
25113fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
25123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
25143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
25153fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                   LParenLoc, NewArgs, RParenLoc)) {
25163fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
25173fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
25183fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
25193fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25203fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
25213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
25223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              TemplateArgs,
25233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              Init->getSourceLocation(),
25243fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
25253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
25263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
25273fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
25283fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
25293fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25303fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
25313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
25323fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     BaseTInfo,
25333fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     (Expr **)NewArgs.data(),
25343fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     NewArgs.size(),
25353fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getLParenLoc(),
25363fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getRParenLoc(),
25373fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
25383fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
25393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
25403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
25413fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
25423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
25433fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25443fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
25453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewArgs.clear();
25463fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
25473fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25483fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
25493fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
25503fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25516b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
25526b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2553a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
25546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
25556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2556090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
25579db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2558090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2559090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2560a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2561802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2562802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2563802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2564a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
25659db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2566802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2567802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2568802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2569802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2570a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
25711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2572090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2573802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2574090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
25753fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     New->getParent(),
25763fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     EllipsisLoc);
2577090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
257800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
257900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
258000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
258100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
25821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2584090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2585090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2586802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2587090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
258800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
258900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
259000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet         cast<IndirectFieldDecl>(FindInstantiatedDecl(
259100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
259200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
259300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
259400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
259500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       NewArgs.size(),
259600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getSourceLocation(),
259700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getLParenLoc(),
259800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getRParenLoc());
2599090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2600090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
26019db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
26029db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2603090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
26049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2605090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2606090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
26071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2608090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2609090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2610090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
26111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2612090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2613d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2614090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2615090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
26169db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
26179db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2618090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2619090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
262052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
262152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
262252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
262352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
262452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
262552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
262652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
262752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
262852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
262952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
263052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
263152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
263252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
263352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
263452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
26350d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
26360d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
26370d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
26380d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
26390d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
26400d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
26410d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
26420d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
26430d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
26440d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
26450d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
26460d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
26470d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2648ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2649ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2650ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2652ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2653ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2654ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2657ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2658ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2660ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2661ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2662ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2663ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
266452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
266552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
266652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
266752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
266852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
266952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
267052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
267152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
267252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
267352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
267452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
267552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
267652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
267752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
267852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
267952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
268052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
268152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
268252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
268352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
268452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
268552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
268652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
268752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
268852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
268952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
269052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
269152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
269252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
269352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
269452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
269552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
269652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
269752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
269852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
269952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
270052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
270152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
270252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2703ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2704ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2705ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2706ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2707ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2708ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2709ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2710ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2711ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2712ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2713ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2714ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
27157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
27167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
27177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2718ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
27197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
27207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
27217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
27220d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
27230d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2724ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
27250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
27260d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
272752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
272852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
272952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
273052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
273152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
273252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
273352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
273452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
273552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
273652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
273752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
273852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
273952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
274052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
274152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2742ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2743ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2744815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
27450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
27467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
27477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
27487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
27497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
27507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
27517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
27527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
27537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
27547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
27550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
27560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
27570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
27580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2759815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
27600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
27610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
27621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
276452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
27651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
276752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2768815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
276952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
277052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2771815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
27727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
277352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
277452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
277552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
277652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
277752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2778a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
27790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
27800d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
27810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2783ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2787d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2788d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2789d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
27901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2791d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2792d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2793d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
27941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2795ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2796ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2797ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2798ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2799ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2800ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2801815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2802815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2803815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2804815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2805815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
28061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2807815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2808815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2809815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2810815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2811815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2812815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2813815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2814815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2815815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2816815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
281702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
281802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
281902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
282002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
28217c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2822e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
282302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
28247c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
282502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
282602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
282702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
282802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2829ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2830ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2831815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2832815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2833815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2834815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2835815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2836815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2837815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2838815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2839815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2840815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2841815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2842815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2843815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2844815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2845815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2846815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2847815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2848815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2849815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2850815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2851815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2852815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2853ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2854ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
28557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2856e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2857815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2858550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
28596d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2860766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
28612bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
28622bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
2863d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2864d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2865d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
2866d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    assert(Found);
2867d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner
2868d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    if (Decl *FD = Found->dyn_cast<Decl *>())
2869d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      return cast<NamedDecl>(FD);
2870d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner
2871d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    unsigned PackIdx = ArgumentPackSubstitutionIndex;
2872d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
28732bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2874815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2875e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2876e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2877e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2878e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
28798b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
28808b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
28818b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
28828b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
28838b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2884e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2885e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2886e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2887e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
288824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2889e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2890e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2891e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
28923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
28933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
28943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
28953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
28963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
28973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
289831f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
28993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2900e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2901e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
29028b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
29038b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
29048b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
29058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
29068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
29078b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
29088b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
29098b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
29108b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2911e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2912e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2913e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2914e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2915e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2916e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2917e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2918e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
29198b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
29208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
29218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
29228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
29238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
29248b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
29258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
29268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
29271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
292852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
29291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
29308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2931e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2932e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
293352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
29348b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
29358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
29368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
293752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
293852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
29398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
29408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
29418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
29428b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
29438b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
29448b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
29458b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
294652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
29478b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
294852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
29498b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2950e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2951e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2952e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
295352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2954e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2955e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2956e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
29577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
29581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
295944c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
29601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2961815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2962815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2963815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2964815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
29657c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
29663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
29673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
29683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
29697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
29707c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
29713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
29723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
29733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
29743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
29753cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
2976a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
2977a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
29787c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
29797c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
29807c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2981815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2982815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
298317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2984815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2985815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2986815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2987815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2988815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2989815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2990815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2991815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2992815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2993815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
29941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
299517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
299617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2997815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
29981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29999f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
300000225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
300100225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
30029f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
30039f54ad4381370c6b771424b53d219e661d6d6706John McCall
3004815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3005815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3006815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3007815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3008815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3009d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
30101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3011d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
301262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruthvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
301360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
301462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
301560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
301660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
301760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
301862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
301962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
302060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
302160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
302260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
302360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
30241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
30267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3027f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3028f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
302958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
303058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
303158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
303258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
30337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
30347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
30351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
30377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
30387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3039c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3040291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3041291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3042291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
3043291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3044291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3045291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3046291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3047291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3048291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3049291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
3050291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3051291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
305258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
305358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
305458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
305558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
305658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
3057291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3058291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3059291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3060291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3061f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3062f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3063f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
30641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
306658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
306758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
306858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3069d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3070d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
30710c01d18094100db92d38daa923c95661512db203John McCall
30720c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
30730c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
30740c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
30750c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
30760c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
30770c01d18094100db92d38daa923c95661512db203John McCall
30780c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
30790c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
30800c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
30810c01d18094100db92d38daa923c95661512db203John McCall      break;
30820c01d18094100db92d38daa923c95661512db203John McCall    }
30830c01d18094100db92d38daa923c95661512db203John McCall  }
30840c01d18094100db92d38daa923c95661512db203John McCall}
3085