SemaTemplateInstantiateDecl.cpp revision b99268b3083c882103bd1bd08bdcc9a76a2b4795
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
14f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
16aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
200c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
22a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2483ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
28b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
30b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
31b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
32b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
33b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
34b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
35b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
36b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
37b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
40b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
46b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *OldQual = OldDecl->getQualifier();
47b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!OldQual) return false;
48b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
49b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  SourceRange QualRange = OldDecl->getQualifierRange();
50b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
51b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NestedNameSpecifier *NewQual
52b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
53b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (!NewQual)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
56b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  NewDecl->setQualifierInfo(NewQual, QualRange);
57b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
58b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
59b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
604ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth// FIXME: Is this still too simple?
611d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
621d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall                            Decl *Tmpl, Decl *New) {
63cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
64cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
65cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
664ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
674ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
68cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt      if (Aligned->isAlignmentDependent()) {
694ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        // The alignment expression is not potentially evaluated.
701d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall        EnterExpressionEvaluationContext Unevaluated(*this,
71f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                     Sema::Unevaluated);
724ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
73cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        if (Aligned->isAlignmentExpr()) {
7460d7b3a319d84d688752be3870615ac0f111fb16John McCall          ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
757663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                        TemplateArgs);
76cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (!Result.isInvalid())
77cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
78cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
79cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        else {
80cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
817663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             TemplateArgs,
827663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             Aligned->getLocation(),
837663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                             DeclarationName());
84cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt          if (Result)
85cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt            AddAlignedAttr(Aligned->getLocation(), New, Result);
86cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt        }
874ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth        continue;
884ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth      }
894ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
904ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
91d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    // FIXME: Is cloning correct for all attributes?
921d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall    Attr *NewAttr = TmplAttr->clone(Context);
93d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson    New->addAttr(NewAttr);
94d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
95d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
96d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
974f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
984f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
994f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Translation units cannot be instantiated");
1004f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1014f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1024f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1034f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1044f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1054f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1064f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1074f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1084f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1093dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1103dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1113dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1123dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1133dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1143dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1153dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace()->getIdentifier(),
1163dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifierRange(),
1173dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getQualifier(),
1183dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1193dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1203dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1213dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1223dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1233dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
126a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
127836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
128836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
129ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
130ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
131ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
133a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
135b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
136b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  TypedefDecl *Typedef
1418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
142ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                          D->getIdentifier(), DI);
1438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
146d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  if (const TagType *TT = DI->getType()->getAs<TagType>()) {
147d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    TagDecl *TD = TT->getDecl();
148d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
149d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // If the TagDecl that the TypedefDecl points to is an anonymous decl
150d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    // keep track of the TypedefDecl.
151d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor    if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
152d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor      TD->setTypedefForAnonDecl(Typedef);
153d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
154d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
1555126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
1567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
1585126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall    Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
1595126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1605126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1611d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
162d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
16346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
16417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1696eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \brief Instantiate the arguments provided as part of initialization.
1706eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor///
1716eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor/// \returns true if an error occurred, false otherwise.
1726eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregorstatic bool InstantiateInitializationArguments(Sema &SemaRef,
1736eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                               Expr **Args, unsigned NumArgs,
1746eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                           const MultiLevelTemplateArgumentList &TemplateArgs,
175ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall                           ASTOwningVector<Expr*> &InitArgs) {
1766eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I) {
1776eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // When we hit the first defaulted argument, break out of the loop:
1786eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    // we don't pass those default arguments on.
1796eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Args[I]->isDefaultArgument())
1806eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      break;
1816eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
18260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
1836eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    if (Arg.isInvalid())
1846eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor      return true;
1856eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1866eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    InitArgs.push_back(Arg.release());
1876eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  }
1886eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1896eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor  return false;
1906eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor}
1916eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
1926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
1936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
1946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
1966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
1986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2087663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                   ASTOwningVector<Expr*> &NewArgs,
2096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2106b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2174765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
2186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return InstantiateInitializationArguments(S, ParenList->getExprs(),
2306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              ParenList->getNumExprs(),
231a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                              TemplateArgs, NewArgs);
2326b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
23528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
23628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      if (InstantiateInitializationArguments(S,
23728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getArgs(),
23828329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor                                             Construct->getNumArgs(),
239a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                             TemplateArgs, NewArgs))
24028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
24128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
24228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
24328329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
244a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
24528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
24628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
2476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2486b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
24960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
2506b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
2516b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
2526b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2573d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2589901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2599901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
2609901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2619901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2629901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2639901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
2649901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
265ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
266a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2670a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2680a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2690a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2700a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2713d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
273c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
274c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
275c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
276c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
277c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
278c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor
279b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2803d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
2813d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
2820a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
28316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
28416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
2853d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
2863d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
288b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
289b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
290b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
291b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
299c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
300c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor  if (!D->isStaticDataMember())
301c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3024469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
303390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
304390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3053d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3066826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3076826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
308449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
30960c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
31060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
314ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
315ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
319f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
320f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3221d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
3238dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
324251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
325251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
326251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
327251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
328cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
329251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
33060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
33160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
33260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3331f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3341f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3351f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3361f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3371f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
340ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
3416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
342a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                InitArgs, RParenLoc)) {
3436b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // Attach the initializer to the declaration.
3446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      if (D->hasCXXDirectInitializer()) {
3456eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
346d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
3476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3486eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
3496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              RParenLoc);
3506b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else if (InitArgs.size() == 1) {
3519ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
3529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        SemaRef.AddInitializerToDecl(Var, Init, false);
3536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      } else {
3546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor        assert(InitArgs.size() == 0);
355d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.ActOnUninitializedDecl(Var, false);
35683ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
3576eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3606b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
3616eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3631f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
36465b90055dd30cfb83d8344ff62ed428257431be4Douglas Gregor  } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
365d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
3663d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3675764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
3685764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
3695764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
370bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
3713d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3733d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3746206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3756206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3766206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3776206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3786206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3796206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3806206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3816206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
384a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
385836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
386836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
38707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
38807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
38907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
390a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
39107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
39207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
40007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
403b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
404b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
411ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
412f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
415ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
420e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
42307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
42407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
429ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
432663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
433663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
434f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
435663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4371d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
438d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
439f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
440f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
442f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
443f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
444f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4459901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
4469901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4479901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4487a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4499901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
452f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
45346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
454f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
45987c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
46087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
46187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
46287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
46387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
46487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
46587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
46687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       PI != PE; ++PI)
46787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    NamedChain[i++] = (SemaRef.FindInstantiatedDecl(D->getLocation(),
46887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                            *PI, TemplateArgs));
46987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
47040e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
47187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
47287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
47340e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
47487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
47587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
47687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
47787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
47887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
47987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
48087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
48187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
48287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
48402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
48506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
48632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
48732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall    TypeSourceInfo *InstTy =
48832f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall      SemaRef.SubstType(Ty, TemplateArgs,
48932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall                        D->getLocation(), DeclarationName());
49006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!InstTy)
49106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
492fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
49306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
49406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
49506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
49606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
49706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
4989a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
49906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
50006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
50106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
50206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
50306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
50406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
50532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
506af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
507af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
508af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
509af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
510af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
51106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
51406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
51506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5165fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5179a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
51802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
51902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
520fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
521fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
525ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
526f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
529ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
53360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
5343fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
535d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
5379ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              Message.get());
5388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
5428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
543741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                                    D->getTagKeywordLoc(),
544a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
545a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
5461274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
5471274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
5481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
5491274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
5501274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
5511274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
5521274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
5531274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
5541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
5551274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
5561274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5571274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
5581274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
5591274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5601274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
5611274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
5621274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
5631274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
5641274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5651274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
5661274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5675b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
5685b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5698dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
57006c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
571b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
57217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
57596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
57696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
57796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
578d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
5798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
58117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
58217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
5848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
58560d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
586ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
587ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
589f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
591ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
592ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
5968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6135b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
6145b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6153b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
61617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
617d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
61996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
62096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
62196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
62296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
62396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
62496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
628c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
629fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
630c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
631d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
632de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
633fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6386477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6396477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6406477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6416477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6426477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
643e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
64493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
64593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
646550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
647550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
6482a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
649e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
650ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
652d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
653e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
654e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
65593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
65693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
65793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
65893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
65993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  NestedNameSpecifier *Qualifier = Pattern->getQualifier();
66093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier) {
66193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
66293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 Pattern->getQualifierRange(),
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                                 TemplateArgs);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!Qualifier) return 0;
66593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
66693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
66793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
66893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
66993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67037574f55cd637340f651330f5cfda69742880d36Nick Lewycky  if (!isFriend && Pattern->getPreviousDeclaration()) {
67137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
67237574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
67337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
67437574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
67537574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
67637574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
67737574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
67837574f55cd637340f651330f5cfda69742880d36Nick Lewycky
67993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
68093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
68193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
68293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
68393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (Qualifier) {
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setScopeRep(Qualifier);
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SS.setRange(Pattern->getQualifierRange());
68993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
69093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
69193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
69293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
69393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
69493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
69593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
69693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
69793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
70493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (!PrevClassTemplate && Qualifier) {
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7111eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
7121eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << Pattern->getQualifierRange();
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
716c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
718c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
719c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
720c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
721c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
722c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
723c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
724c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
725c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
726c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
727c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
728c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
729c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
730c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
731c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
732c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
733c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
734c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
735c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
736c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
737c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
738c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
739c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
740c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
741c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
742c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
743c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
74693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
74793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
749c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
750c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
751c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
752c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
753c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
754c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
755c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
756c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
767e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
769e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                            Pattern->getLocation(), Pattern->getIdentifier(),
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                            Pattern->getTagKeywordLoc(), PrevDecl,
771f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (Qualifier)
77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
775b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
776e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
780e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
781ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
783ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
784ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
785ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
786ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
787ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
792e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
79337574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
79437574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
79593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
796f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
797f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
7983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
79924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
800ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
801259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
804e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
805259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
806e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
807e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
808d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
809d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
810d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
811d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
812d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
813d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
814d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
815d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
816d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
817d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
818d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
819d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
820e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
821e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
822e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
823d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8247974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8257974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
840d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
841d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
842d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
843d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
844d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
8457974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8467974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8477974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
848d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
849550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
850550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
851550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
852550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
8532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
854895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
855d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
856d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
858d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
860a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
861a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
862a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
863a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
864a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
865a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
866a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
867a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
868a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
869a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
870d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
871d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
87246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
87346460a68f6508775e98c19b4bb8454bb471aac24John McCall
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
875d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
87637d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
877a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
87837d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
879a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
880a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
881e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
882b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
883b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
884e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
885e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
886e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
887b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
888a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
889a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
890b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
891b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
892a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
893b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
894d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
895d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
896d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
897d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
898d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
899d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
900d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9016c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9027c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9037c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9046c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9056c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9066c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9076c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
908d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
909d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
911741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getLocation(), D->getIdentifier(),
912741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor                            D->getTagKeywordLoc(), PrevDecl);
913b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
914b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
915b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
916b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
917b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
918d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
919eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
920eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
921eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
922eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
923eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
924d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
925f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
926d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
92702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
92802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
92902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
93002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
93102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9357a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9369901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9379901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
938d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
93917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
940d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
941d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
942d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
94302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
94402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
94502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
94602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
94702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9487557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
949a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
950127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
951127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
952127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
953127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
954b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
95524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
95624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9582c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9592c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9602c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
962127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9632c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9642c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
965127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
967b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
968b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
969b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
970b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
971b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
972b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
97379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
974b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
97579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
97679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
9772a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
979e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
98021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
98121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
98221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
9832dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
98421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
985fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
986d325daa506338ab86f9dd468b48fd010673f49a6John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
987d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier) {
988d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
989d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 D->getQualifierRange(),
990d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                                 TemplateArgs);
991d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!Qualifier) return 0;
992d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
993d325daa506338ab86f9dd468b48fd010673f49a6John McCall
99468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
99568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
99668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
99768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
99868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
999d325daa506338ab86f9dd468b48fd010673f49a6John McCall  else if (isFriend && Qualifier) {
1000d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1001d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setScopeRep(Qualifier);
1002d325daa506338ab86f9dd468b48fd010673f49a6John McCall    SS.setRange(D->getQualifierRange());
1003d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1004d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1005d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
10067c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10077c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1008d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
100968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
101002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
10111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
101221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                           D->getDeclName(), T, TInfo,
101316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
10140130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
1015b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1016d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (Qualifier)
1017d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setQualifierInfo(Qualifier, D->getQualifierRange());
1018b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1019b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1020b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1021b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1022b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1023b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1024b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1025b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1027e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1028e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
10293019c444c672938c57f5573840071ecd73425ee7John McCall    if (Params[P])
10303019c444c672938c57f5573840071ecd73425ee7John McCall      Params[P]->setOwningFunction(Function);
1031838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
103202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1033ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1034a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1035a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1036a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1037a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1038a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1039a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1040a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1041a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1042a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1043a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1044a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1045a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1046a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1047a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1048a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1049d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1054b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1055b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1056d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1057d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1058d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1059d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1060d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1061d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1062d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
106366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
106466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
106524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
106624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1067838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1068910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
106924bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
107024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
107166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1072d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1073d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1074d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1075d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
107602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1077a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1078e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1079e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1081e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1082e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool OverloadableAttrRequired = false;
1083af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1084a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
10856826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
10866826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
10876826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1088af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1089af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1090af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1091af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1092af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1093af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1094af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1095af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1096af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1097af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1098af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1099af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      TemplateArgumentLoc Loc;
1100af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1101af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        return 0;
1102af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1103af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      ExplicitArgs.addArgument(Loc);
1104af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1105af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1106af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1107af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1109af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1110af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1111af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1112af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1113af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1114af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1115af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1116af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1117af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1118af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1119af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1120af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1124a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1125a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1126a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
11276826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1128a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1129a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1130a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1131a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1132a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11336826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11346826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1135a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1136a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11379f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1138af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                   isExplicitSpecialization, Redeclaration,
1139e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
1140e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
114176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
114276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
114376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
114476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1145a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1146a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1147d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11486826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
114976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1150a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
115176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1152a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
115376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
115476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
115576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1156ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
115777535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1158ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1159238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1160238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1161238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1162238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
116306a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (Function->hasBody(Definition) &&
1164238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1165238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1166238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1167238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1168238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1169238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1170238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1171238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1174238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
117513a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
117613a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1177ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1178ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
1179ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          if (!queuedInstantiation && R->isUsed(false)) {
1180ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1181ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1182ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1183ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1184ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1185ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1186ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1187ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1188ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1189ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1190ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1191ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1192ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1193238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
11946a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
119506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis            if (RPattern->hasBody(RPattern)) {
1196238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1197238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
11986a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1199238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1200238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1201238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1202238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1203238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1204238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1205a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1206a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
120776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
120876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
120976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
121076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1211e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1212e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12132dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1214d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1215d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1216d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
12176b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12186b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1219d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1222d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
122324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
122424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12262c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
12272c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
12282c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12306b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12312c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12322c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
12336b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12346b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1235b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1236b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1237b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1238b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1239b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1240b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
124179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
124279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
124379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12442a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
124548dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12464eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
12474eab39f0745fb1949dbb40c4145771b927888242John McCall  llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
12484eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
12494eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
12504eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
12514eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
12524eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
12534eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
12544eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
12554eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
12564eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
12574eab39f0745fb1949dbb40c4145771b927888242John McCall    }
12584eab39f0745fb1949dbb40c4145771b927888242John McCall  }
12594eab39f0745fb1949dbb40c4145771b927888242John McCall
12600ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
126121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
126221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
126321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
126521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1267723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1268723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1269723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
12705f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12715f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12725f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12735f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12745f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12755f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1276723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
12775f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
12785f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
12795f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
12805f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor                                                TemplateArgs);
12815f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      if (!P)
12825f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor        return 0;
12835f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
12845f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor      Params.push_back(P);
12855f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    }
12865f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
12875f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1288b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  NestedNameSpecifier *Qualifier = D->getQualifier();
1289b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier) {
1290b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1291b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 D->getQualifierRange(),
1292b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1293b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!Qualifier) return 0;
1294b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1295b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1296b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1297b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1298b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (Qualifier) {
1299b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1300b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setScopeRep(Qualifier);
1301b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      SS.setRange(D->getQualifierRange());
1302b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1303c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1304c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1305c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1306b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1307b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1308b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1309b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1310b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1311b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1312b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1313b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
13142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1316dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
13171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
13192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
132017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
13222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                        NameInfo, T, TInfo,
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
132416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
132516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
132617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
132717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1328b41d899a6023385c00a61eb9dd3e44db9dc7994eCraig Silverstein                                       NameInfo, T, TInfo,
13292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
133016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
133165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
133265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
13332577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       NameInfo, T, TInfo,
13340130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
133565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                       Conversion->isExplicit());
1336dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
13382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                   NameInfo, T, TInfo,
133916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
134016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
134116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isInlineSpecified());
1342dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13436b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1344b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (Qualifier)
1345b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setQualifierInfo(Qualifier, D->getQualifierRange());
1346b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1347d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1348d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1349d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1351d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1352d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1353d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1354d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1355d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1356d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1357d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1358d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1359d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1360d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1361d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1362d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1364d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1365b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1366b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1367b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1368b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1370d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
137166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
137266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
137324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
137424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1375838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1376910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1377910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1378910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
137966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1380b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
138166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13822db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
138366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
138466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
13851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
13867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
13877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1388b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
13894eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
13904eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
13914eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
13924eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
13934eab39f0745fb1949dbb40c4145771b927888242John McCall
1394b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1395b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1396b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
13977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
14005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
14015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1402838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
14035545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14045545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
14055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
14062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
14072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
14082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1410b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1411b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1413dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1414dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1415dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1416dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
14176826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
14186826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1419dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14202dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
142165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
142265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool OverloadableAttrRequired = false;
14239f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
142465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                   /*FIXME:*/OverloadableAttrRequired);
142565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
14264ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
14274ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
14284ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
142946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
143046460a68f6508775e98c19b4bb8454bb471aac24John McCall
1431b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1432b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1433b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1434b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1435b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1436b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1437b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1438b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1439b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1440b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1441b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1442b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1443b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1444bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
14452dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14462dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1448615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1449dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1450615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1451615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
145203b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
145317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
145403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
145503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1456bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
145765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1458bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1459bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14606477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1461cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  return SemaRef.SubstParmVarDecl(D, TemplateArgs);
14622dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1464e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1465e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1466e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
1467efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const Type* T = D->getTypeForDecl();
1468efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  assert(T->isTemplateTypeParmType());
1469efed5c832de630715dd42211dd3b2aab5dd97a1bDouglas Gregor  const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1471e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1472e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
147371b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                 TTPT->getDepth() - TemplateArgs.getNumLevels(),
147461139c561f1a2c872209e32ff9143487cebf4324Nick Lewycky                                 TTPT->getIndex(), D->getIdentifier(),
1475e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1476e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
1477e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
14780f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14790f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1480e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1481550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1482550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1483550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1484550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1485e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1486e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1487e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
148833642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
148933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
149033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
149133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
1492a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
149333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (DI) {
149433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
149533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                           D->getDeclName());
149633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    if (DI) T = DI->getType();
149733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  } else {
149833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
149933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                          D->getDeclName());
150033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    DI = 0;
150133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
150233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull())
150333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    return 0;
150433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
150533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Check that this type is acceptable for a non-type template parameter.
150633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
150733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
150833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (T.isNull()) {
150933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    T = SemaRef.Context.IntTy;
151033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Invalid = true;
151133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
151233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
151333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  NonTypeTemplateParmDecl *Param
151433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
151571b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
151671b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                      D->getPosition(), D->getIdentifier(), T,
151771b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                      DI);
151833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
151933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
152033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1521d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1522550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1523550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1524550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1525550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
152633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
152733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
152833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
15290dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
15309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
15319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
15329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
15339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
15349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
15359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
15369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
15379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
15382a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
15399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
15409106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
15419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
15429106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
15439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15449106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
15459106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
15469106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
154771b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                   D->getDepth() - TemplateArgs.getNumLevels(),
154871b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                       D->getPosition(), D->getIdentifier(),
154971b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                       InstParams);
1550d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
15514469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
15529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
15539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
15549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
15559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
15569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
15579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
15589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
155948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
156048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  // Using directives are never dependent, so they require no explicit
156148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
156248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
156348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
156448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
156548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getQualifierRange(), D->getQualifier(),
156648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
156748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
156848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
156948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
157048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
157148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
157248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1573ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
15741b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
15751b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
15761b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
15771b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
15781b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
15791b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
15801b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
15811b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
15821b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
15831b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  NestedNameSpecifier *NNS =
15841b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(),
15851b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      D->getNestedNameRange(),
15861b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      TemplateArgs);
15871b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  if (!NNS)
15881b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor      return 0;
15891b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
15901b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
15911b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1592ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1593ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
15949f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
15959f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
15969f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
15979f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
15989f54ad4381370c6b771424b53d219e661d6d6706John McCall
1599ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1600ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
16019f54ad4381370c6b771424b53d219e661d6d6706John McCall
1602ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1603ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getNestedNameRange(),
1604ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
16051b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor                                       NNS,
1606ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1607ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1608ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1609ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  CXXScopeSpec SS;
16101b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  SS.setScopeRep(NNS);
1611ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SS.setRange(D->getNestedNameRange());
16129f54ad4381370c6b771424b53d219e661d6d6706John McCall
16139f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
16149f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
16159f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
16169f54ad4381370c6b771424b53d219e661d6d6706John McCall
16179f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
16189f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
16199f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
16209f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
16219f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
16229f54ad4381370c6b771424b53d219e661d6d6706John McCall
16239f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
16249f54ad4381370c6b771424b53d219e661d6d6706John McCall
16259f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
16269f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1627ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1628ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
16299f54ad4381370c6b771424b53d219e661d6d6706John McCall
1630ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1631ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1632ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1633ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16349f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
16359f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
16369f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
16379f54ad4381370c6b771424b53d219e661d6d6706John McCall
1638323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1639323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
16409f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
16419f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
16429f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
16439f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
16449f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
16457c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
16467c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   Shadow->getTargetDecl(),
16479f54ad4381370c6b771424b53d219e661d6d6706John McCall                                                   TemplateArgs));
16489f54ad4381370c6b771424b53d219e661d6d6706John McCall
16499f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
16509f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
16519f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
16529f54ad4381370c6b771424b53d219e661d6d6706John McCall
16539f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
16549f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
16559f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1656323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1657323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1658323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
16599f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1660ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1661ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1662ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1663ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1664ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
16659f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
16669f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1667ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1668ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
16717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NestedNameSpecifier *NNS =
16727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
16737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     D->getTargetNestedNameRange(),
16747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                     TemplateArgs);
16757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  if (!NNS)
16767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
16777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
16797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setRange(D->getTargetNestedNameRange());
16807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  SS.setScopeRep(NNS);
16817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1682ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1683ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1684ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
16857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
16867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1687ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
16887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
16897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
16904469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1691ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1692ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
16937ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
16947ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
16957ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
16967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
16977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NestedNameSpecifier *NNS =
16991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     D->getTargetNestedNameRange(),
17010dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson                                     TemplateArgs);
17020dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  if (!NNS)
17030dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17050dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
17060dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setRange(D->getTargetNestedNameRange());
17070dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  SS.setScopeRep(NNS);
17081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1709ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1710ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1711ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
17139488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1714ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
17157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
17167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
17174469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1718ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1719ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
17210dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
17220dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1723ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1724d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
17257e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
17262fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
17272fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
17282fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
17298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
17308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
17318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1732e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1733e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1734e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1735e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1736e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1737e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1738e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1739ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1740e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1744bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1745e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1749bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1750e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
17519148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1754e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1755ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1756e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1757e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1759e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1760e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1761e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1762e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1764e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1765ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1766ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1767ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1769ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1771ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1772ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1774d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
1775d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
1776d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
1777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1778ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1779ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1780550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1781550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1782550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
17832a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1784550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1786ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1788ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1789ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1790d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1794833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialSpecTemplateArgs
1795833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = PartialSpec->getTemplateArgsAsWritten();
1796833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1797833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1798d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1799833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1800d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc Loc;
1801d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
1802d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      return 0;
1803d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstTemplateArgs.addArgument(Loc);
1804ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1805ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1806ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1807ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1809910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Converted;
1810ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1811ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1812d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1813ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1814ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1815d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1816ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1818ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1821910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
1822910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
1823ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1824ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1825ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1826ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1827ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1828910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
1829910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
1830ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1833ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
18383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
18393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
18403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
18413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1842d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1858ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1861ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1862d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
1863ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1864ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1865d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1866ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1867ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1868ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1869ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1870ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
187113c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
187213c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
187313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1874ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     PartialSpec->getLocation(),
1875ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1876ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1877910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
1878910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
1879d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
18803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1881dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1882cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1883b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1884b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1885b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1886b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1887ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
18884469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
18894469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
1890ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
1891ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
1892cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
1893d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
1894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
1895ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
189621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
189721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
189821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
189921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
190021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
190121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
19026cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
19036cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
19046cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
19056cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
190621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
190721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
19085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
1909cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
1910cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
1911140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
19126920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19136920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1914140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
19156920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
19166920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
19176920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
19186920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        // FIXME: Variadic templates will break this.
19196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(NewProtoLoc->getArg(i));
19206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(
1921895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        OldProtoLoc->getArg(i),
1922895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor                                                        NewProtoLoc->getArg(i));
19236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1924895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
1925cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
1926cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
1927cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
1928cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
1929140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
19306920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
19316920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
19326920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
19336920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
19346920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
19356920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
19366920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
19376920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
1938cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
1939cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
194021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
19415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
19425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
19431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
1944e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
1945e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1946e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
19471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
19481eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
1949e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
1950e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
1951e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
19521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1953cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
1954cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
1955cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
19561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
19571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
1958cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
1959cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
1960cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
1961cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1962cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1963cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1964cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
19651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
1966cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
19671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
1968cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
1969bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
1970cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1971cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
1972f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
1973cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
1974cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
19751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
19770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
19780ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
19790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
19800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Proto->getNoReturnAttr()) {
19810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
19820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
19830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
19840ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
19850ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
1986b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
1987b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
1988b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
1989b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1990b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
1991b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
1992b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        assert(!Unexpanded.empty() &&
1993b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
1994b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
1995b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
1996b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        unsigned NumExpansions = 0;
1997b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
1998b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
1999b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.data(),
2000b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.size(),
2001b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
2002b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Expand, NumExpansions))
2003b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
2004b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2005b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
2006b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
2007b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // just substitute into the argument pack.
2008b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2009b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2010b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2011b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2012b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2013b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2014b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2015b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2016b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2017b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2018b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2019b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2020b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2021b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != NumExpansions; ++ArgIdx) {
2022b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2023b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2024b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2025b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2026b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2027b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2028b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2029b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2030b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
2031b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2032b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2033b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
2034b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2035b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2036b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
2037b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2038b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2039b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2040b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
20410ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
20420ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
20430ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
20440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
20450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
20460ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
20470ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20480ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
20490ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
20500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
20520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2053e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2054e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasExceptionSpec = Proto->hasExceptionSpec();
2055e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec();
2056e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2057e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
2058e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2059e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
20600ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
20610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
20620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
20630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
20640ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
20650ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2066e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
20670ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
20680ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
20691d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
20707cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2071e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2072e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2073e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
20745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
20755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
20765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
20775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
20785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
20815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2082e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2083e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
20841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20855545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2086e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
208785606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
20885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
20905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
20915545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
20925545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2093a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2094a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2095a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2096a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2097b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2098b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2099b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2100b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2101a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2102b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2103b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2104b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2105b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2106b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2107e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2108e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2109e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2110e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2111f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2112b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2113e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2114e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
211506a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
211654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
211754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2118251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2119251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2120251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
21216cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
21221eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
21233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
21241eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
21251eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
21266fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
21271eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2128e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2129e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2130e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2131e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2132e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2133e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2134e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2135e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2136e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2137e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2138e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2139e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2140e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2141e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2142cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
214358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
214458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
214562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
214658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2147e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
214858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
21491eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2150e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
21511eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2152d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2153d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
21541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2155d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
21561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2157d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
21587ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2159d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
21601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2161f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2162f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2163e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2164e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2165b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2166b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2167b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
21682a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  llvm::SmallVector<VTableUse, 16> SavedVTableUses;
216962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
21702a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
21712a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
217262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
21732a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21759679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
2176f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2177d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2178e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
217954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
218060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
218160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
218260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
218360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
218460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
218560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
218660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
218760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
21908a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
21918a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
21928a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
21938a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
21948a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    ParmVarDecl *FunctionParam = Function->getParamDecl(I);
21958a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    FunctionParam->setDeclName(PatternParam->getDeclName());
21968a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    Scope.InstantiatedLocal(PatternParam, FunctionParam);
21978a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
219854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2199b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2200b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2201b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  DeclContext *PreviousContext = CurContext;
2202b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = Function;
2203b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2205e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2206090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2207090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
22081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2209090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2210090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2211090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
221560d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2216e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
221752604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
221852604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
221952604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
22209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ActOnFinishFunctionBody(Function, Body.get(),
2221e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2222b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
22230c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
22240c01d18094100db92d38daa923c95661512db203John McCall
2225b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  CurContext = PreviousContext;
2226aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2227aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2228aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
223060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
223160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
223262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
223360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
223460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2235b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
22362a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
22372a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
22382a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2239b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
22401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
224162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
22421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22432a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
22442a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
22452a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2246b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
224762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2248b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2249a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2250a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2251a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2252a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2253a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
22547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
22557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
22567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
22577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
22597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
22607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
22617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
22627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2263e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2264e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2265e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2266e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
22677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
22687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
22697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2270e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2271e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
22727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
22737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
22767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
22777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
22780d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
22790d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
22801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22810d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
22827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
22837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
22851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2286e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
22870d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2288e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2289e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2290e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2291e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
229258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
229358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
229462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
229558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
229658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
229758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
22987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
22997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
23007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2301251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
23021028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2303251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2304251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2305251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2306251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2307251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2308251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
23091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2310251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2311251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
23121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
23147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
23157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
23161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
23187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
23197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
232062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
23217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
232262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
23231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
23257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
23267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  DeclContext *PreviousContext = CurContext;
23277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = Var->getDeclContext();
23281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23291028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2330ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
23316bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
23327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  CurContext = PreviousContext;
23337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
23347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2335583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2336583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2337583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2338583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
23397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
23407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
23417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
23421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
23447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
23451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
234662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
234962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
23501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2351a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2352815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2353090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2354090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2355090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2356090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
23571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2358090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
23599db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
23609db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2361090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2362090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
236372f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
236472f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2365090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    CXXBaseOrMemberInitializer *Init = *Inits;
2366090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2367030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2368030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2369030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2370030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2371030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
23726b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2373ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
23741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
23766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2377a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
23786b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
23796b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2380090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
23819db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2382090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2383090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2384a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2385802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2386802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2387802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2388a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
23899db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2390802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2391802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2392802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2393802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2394a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
23951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2396090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2397802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2398090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
2399090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     New->getParent());
2400090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
240100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl(
240200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
240300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
240400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
24051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2407090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2408090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2409802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2410090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
241100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
241200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
241300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet         cast<IndirectFieldDecl>(FindInstantiatedDecl(
241400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
241500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
241600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
241700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
241800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       NewArgs.size(),
241900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getSourceLocation(),
242000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getLParenLoc(),
242100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getRParenLoc());
2422090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2423090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
24249db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
24259db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2426090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
24279db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2428090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2429090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
24301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2431090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2432090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2433090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
24341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2435090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2436d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2437090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2438090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
24399db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
24409db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2441090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2442090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
244352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
244452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
244552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
244652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
244752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
244852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
244952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
245052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
245152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
245252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
245352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
245452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
245552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
245652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
245752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
24580d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
24590d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
24600d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
24610d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
24620d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
24630d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
24640d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
24650d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
24660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
24670d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
24680d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
24690d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
24700d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2474ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2476ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2477ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2478ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2479ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2480ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2482ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2483ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2485ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
248752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
248852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
248952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
249052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
249152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
249252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
249352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
249452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
249552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
249652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
249752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
249852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
249952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
250052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
250152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
250252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
250352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
250452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
250552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
250652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
250752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
250852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
250952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
251052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
251152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
251252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
251352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
251452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
251552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
251652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
251752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
251852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
251952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
252052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
252152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
252252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
252352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
252452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
252552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2526ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2527ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2528ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2529ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2530ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2531ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2532ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2533ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2534ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2535ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2536ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2537ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
25387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
25397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
25407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2541ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
25427ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
25437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
25447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
25450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
25460d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2547ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
25480d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
25490d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
255052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
255152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
255252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
255352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
255452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
255552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
255652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
255752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
255852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
255952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
256052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
256152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
256252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
256352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
256452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2565ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2567815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
25680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
25697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
25707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
25717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
25727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
25737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
25747ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
25757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
25767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
25777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
25780d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
25790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
25800d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
25810d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2582815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
25830d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
25840d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
25851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
258652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
258752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
25881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
258952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
259052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2591815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
259252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
259352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2594815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
25957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
259652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
259752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
259852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
259952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
260052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2601a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
26020d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
26030d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
26040d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2605ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2606ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2607ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2609ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2610d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2611d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2612d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
26131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2614d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2615d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2616d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2618ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2619ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2620ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2621ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2622ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2623ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2624815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2625815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2626815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2627815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2628815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
26291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2630815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2631815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2632815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2633815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2634815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2635815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2636815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2637815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2638815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2639815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
264002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
264102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
264202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
264302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
26447c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2645e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
264602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
26477c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
264802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
264902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
265002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
265102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2652ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2653ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2654815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2655815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2656815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2657815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2658815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2659815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2660815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2661815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2662815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2663815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2664815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2665815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2666815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2667815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2668815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2669815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2670815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2671815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2672815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2673815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2674815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2675815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2676ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2677ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
26787c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2679e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2680815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2681550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
26826d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2683766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
26842bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
26852bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
26868dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
26872bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2688815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2689e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2690e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2691e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2692e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
26938b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
26948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
26958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
26968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
26978b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2698e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2699e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2700e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2701e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
270224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2703e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2704e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2705e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
27063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
27073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
27083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
27093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
27103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
27113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
271231f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
27133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2714e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2715e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
27168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
27178b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
27188b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
27198b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
27208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
27218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
27228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
27238b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
27248b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2725e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2726e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2727e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2728e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
2729e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
2730e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
2731e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
2732e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
27338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
27348b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
27358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
27368b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
27378b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
27388b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
27398b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
27408b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
274252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
27431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
27448b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
2745e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
2746e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
274752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
27488b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
27498b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
27508b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
275152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
275252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
27538b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
27548b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
27558b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
27568b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
27578b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
27588b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
27598b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
276052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
27618b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
276252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
27638b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
2764e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
2765e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
2766e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
276752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2768e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
2769e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
2770e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
27717c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
27721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
277344c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
27741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2775815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
2776815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
2777815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
2778815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
27797c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
27803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
27813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
27823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
27837c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
27847c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
27853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
27863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
27873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
27883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
27893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
2790a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
2791a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
27927c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
27937c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
27947c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
2795815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
2796815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
279717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
2798815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
2799815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
2800815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
2801815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
2802815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
2803815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2804815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
2805815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
2806815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
2807815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
28081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
280917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
281017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
2811815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
28121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28139f54ad4381370c6b771424b53d219e661d6d6706John McCall    // UsingShadowDecls can instantiate to nothing because of using hiding.
281400225547b51b42f7400eed36475b6672418a1151Douglas Gregor    assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
281500225547b51b42f7400eed36475b6672418a1151Douglas Gregor            cast<Decl>(ParentDC)->isInvalidDecl())
28169f54ad4381370c6b771424b53d219e661d6d6706John McCall           && "Unable to find instantiation of declaration!");
28179f54ad4381370c6b771424b53d219e661d6d6706John McCall
2818815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
2819815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
2820815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2821815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
2822815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2823d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
28241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
2825d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
282662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruthvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
282760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
282862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
282960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
283060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
283160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
283262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
283362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
283460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
283560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
283660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
283760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
28381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
28407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
2841f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
2842f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
284358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
284458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
284558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
284658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
28477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
28487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
28491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
28517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
28527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
2853c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
2854291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
2855291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
2856291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
2857291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
2858291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2859291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
2860291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
2861291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2862291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
2863291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
2864291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
2865291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
286658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
286758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
286858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
286958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
287058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
2871291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
2872291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
2873291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
2874291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
2875f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
2876f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
2877f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
28781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
287958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
288058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
288158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
288258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
2883d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
2884d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
28850c01d18094100db92d38daa923c95661512db203John McCall
28860c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
28870c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
28880c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
28890c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
28900c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
28910c01d18094100db92d38daa923c95661512db203John McCall
28920c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
28930c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
28940c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
28950c01d18094100db92d38daa923c95661512db203John McCall      break;
28960c01d18094100db92d38daa923c95661512db203John McCall    }
28970c01d18094100db92d38daa923c95661512db203John McCall  }
28980c01d18094100db92d38daa923c95661512db203John McCall}
2899