SemaTemplateInstantiateDecl.cpp revision fb44de956f27875def889482b5393475060392af
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) {
30c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
31c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
32c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
33c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
34c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                          TemplateArgs);
36c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
37c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
40c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
46c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
47c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
48c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
49c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
50c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                        TemplateArgs);
52c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
53c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor
56c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
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 *
10457ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
10557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
10657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
10757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
10857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
10957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
11057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
11157ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1124f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
1134f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  assert(false && "Namespaces cannot be instantiated");
1144f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor  return D;
1154f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1164f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1173dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1183dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1193dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1203dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1213dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1223dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1230cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1240cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1253dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1263dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1273dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1283dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1293dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1303dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
131162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefNameDecl(TypedefNameDecl *D,
132162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                                     bool IsTypeAlias) {
1338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
134a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
135836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
136836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
137ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
138ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
139ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
141a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
143b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
144b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
148162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
149162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
150162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
151162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
152162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
153162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
154162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
1558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
158cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
159cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
160cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
161cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
162162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (oldTag->getTypedefNameForAnonDecl() == D) {
163cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
164162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
165162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
166cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
167d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
168d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
169162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypedefNameDecl *Prev = D->getPreviousDeclaration()) {
1707c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1717c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
172b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
173b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
174b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor
175162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef->setPreviousDeclaration(cast<TypedefNameDecl>(InstPrev));
1765126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1775126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1781d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
179d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
18046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
18117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Typedef);
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
186162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
187162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsTypeAlias=*/false);
188162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
189162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
190162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
191162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  return VisitTypedefNameDecl(D, /*IsTypeAlias=*/true);
192162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
193162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
1946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
1956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
1966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1976b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param S The semantic analysis object.
1986b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
1996b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2016b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2026b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2036b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2076b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregorstatic bool InstantiateInitializer(Sema &S, Expr *Init,
2086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2096b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &LParenLoc,
2107663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                   ASTOwningVector<Expr*> &NewArgs,
2116b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                   SourceLocation &RParenLoc) {
2126b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2136b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2146b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2176b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2194765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
2206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2236b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2246b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2256b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2266b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2276b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2286b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2296b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2306b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
23191fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor    return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
23291fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                        true, TemplateArgs, NewArgs);
2336b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
23628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
23791fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor      if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
23891fc73e7ffb1fa1da0276518359d3bd4ed11c843Douglas Gregor                       TemplateArgs, NewArgs))
23928329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
24028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
24128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
24228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
243a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
24428329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
24528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
2466b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
24860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Result = S.SubstExpr(Init, TemplateArgs);
2496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
2506b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
2516b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2526b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
2536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2579901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2589901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
2599901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2609901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2619901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2629901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
2639901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
264ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
265a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
2660a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
2670a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
2680a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
2690a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
2703d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
2713d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
272c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
273c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
274c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
275c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
276c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
277c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor
278b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
2793d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
280ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 D->getInnerLocStart(),
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());
287ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
290b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
291b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
292b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
2947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
2957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
2967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
2977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
300c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor
3016b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
302c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3036b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3046b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
3054469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
306390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
307390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3083d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3096826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3106826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
311449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
31260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
31360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3146826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
317ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
318ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
322f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
323f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3251d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
3268dd0c5626455cdf94280783e85e413eed6cbf3d9Fariborz Jahanian
327251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
328251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
329251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
330251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
331cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
332251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
33360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
33460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
33560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3361f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3371f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3381f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3391f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3401f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3426b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
343ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
3446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
345a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                                InitArgs, RParenLoc)) {
34634b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
34707a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      // Attach the initializer to the declaration, if we have one.
34807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      if (InitArgs.size() == 0)
34934b41d939a1328f484511c6002ba2456db879a29Richard Smith        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
35007a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      else if (D->hasCXXDirectInitializer()) {
3516eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
352d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
3536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3546eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
35534b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              RParenLoc,
35634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              TypeMayContainAuto);
35707a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      } else {
35807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        assert(InitArgs.size() == 1);
3599ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
36034b41d939a1328f484511c6002ba2456db879a29Richard Smith        SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
36183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
3626eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
3636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
3646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
3656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
3666eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
3676eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor
3681f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
369ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
370ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
371d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
3723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3735764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  // Diagnose unused local variables.
3745764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
3755764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
376bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
3773d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3783d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3793d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3806206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3816206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3826206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3836206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3846206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3856206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3866206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3876206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
390a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
391836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor  if (DI->getType()->isDependentType() ||
392836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
39307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
39407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
39507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
396a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
39707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
39807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
40607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
409b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
410b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
417ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
418f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
421ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
426e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
42907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
43007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
435ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
438663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
439663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
440f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
441663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4431d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
444d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
445f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
446f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
449f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
450f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
4519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
4529901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4539901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4547a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4559901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
458f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
45946460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
460f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
46587c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
46687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
46787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
46887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
46987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
47087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
47187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
472b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
473b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
474b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
475b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
476b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
477b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor
478b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
479b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
48087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48140e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
48287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
48387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
48440e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
48587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
48687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
48887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
48987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
49087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
49187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
49287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
49387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
49402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
49502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
49606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
49732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
4984fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
4994fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5004fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5014fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5024fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5034fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5044fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5054fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5064fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5074fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5084fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5094fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
51006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
511fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
51206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
51306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
51406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
51506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
51606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5179a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
51806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
51906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
52006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  }
52106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor
52206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
52306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
52432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
525af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
526af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
527af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
528af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
529af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
53006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
53306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
53406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5355fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5369a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
53702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
53802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
539fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
540fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
544ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
545f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
548ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
55260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
5533fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
554d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
556a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              Message.get(),
557a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              D->getRParenLoc());
5588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
561ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
5628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
563a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
564a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
5651274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
5661274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
5671274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
5681274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
5691274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
5701274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
5711274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
5721274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
5731274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
5741274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
5751274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5761274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
5771274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
5781274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5791274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
5801274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
5811274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
5821274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
5831274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
5841274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
5851274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
5865b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
5875b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
5888dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
58906c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
590b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
59117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
59496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
59596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
59696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
597d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl*, 4> Enumerators;
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
60017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
60117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
60460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
605ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
606ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
608f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
610ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
611ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6239ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6308dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6325b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
6335b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6343b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
63517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
636d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
63896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor
63996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
64096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
64196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
64296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
64396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
647c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
648fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
649c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
650d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
651de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
652fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6576477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
6586477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  assert(false && "EnumConstantDecls can only occur within EnumDecls.");
6596477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor  return 0;
6606477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
6616477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
662e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
66393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
66493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
665550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
666550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
6672a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
668e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
669ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
671d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
672e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
673e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
67493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
67593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
67693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
67793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
678c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
679c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
680c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
681c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
682c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
683c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
68493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
68593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
68793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
68893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
68937574f55cd637340f651330f5cfda69742880d36Nick Lewycky  if (!isFriend && Pattern->getPreviousDeclaration()) {
69037574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
69137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
69237574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
69337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
69437574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
69537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
69637574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
69737574f55cd637340f651330f5cfda69742880d36Nick Lewycky
69893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
69993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
70093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
70193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
70293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
70393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
704c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
70593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
706c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
70893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
70993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
71093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
71193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
71293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
71393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
71493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
71693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
72293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
72393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
72493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
72593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
72693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
727c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7291eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
730c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
73293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
73393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
734c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
73593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
736c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
737c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
738c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
739c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
740c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
741c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
742c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
743c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
744c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (isFriend && Pattern->getIdentifier() &&
745c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
746c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
747c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
748c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
749c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
750c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
751c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
752c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
753c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
754c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
755c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
756c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
757c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
758c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
759c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
760c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
761c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
767c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Complain,
768c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
769c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
770c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
771c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
772c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
773c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
774c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
778c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
78493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
785e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
78693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
787ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
788ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
789f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
791c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
792c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
793b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
79593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
79793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
798e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
799ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
801ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
802ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
803ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
804ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
805ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
80993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
810e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
81137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
81237574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
814f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor
815f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
81724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
818ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
819259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
822e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
823259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
824e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor
825e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
826d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
827d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
828d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
829d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
830d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
831d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
832d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
833d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
834d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
835d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
836d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
837d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
838e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
839e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
840e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
841d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8427974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8437974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
846ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
847ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
848ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
849ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
850ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
851ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
852ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
853ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
854ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
856ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
858d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
859d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
860d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
861d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
862d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
8637974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
8647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
8657974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
866d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
867550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
868550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
869550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // merged with the local instantiation scope for the function template
870550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
8712a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
872895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
873d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
874d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
876d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
877ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
878a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
879a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
880a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
881a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
882a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
883a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
884a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                          D->getTemplatedDecl(),
885a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
886a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
887a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
888d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
889d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
89046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
89146460a68f6508775e98c19b4bb8454bb471aac24John McCall
8921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
893d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
89437d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  FunctionTemplateDecl *InstTemplate
895a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
89637d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
897a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  assert(InstTemplate &&
898a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
899e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
900b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
901b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
902e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
903e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
904e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
905b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
906a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
907a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
908b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
909b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
910a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
911b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
912d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
913d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
914d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
915d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
916d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
917d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
918d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9196c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9207c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9217c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9226c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9236c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9246c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9256c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
926d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
927d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
929ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
930ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
931b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
932b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
933b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
934b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
935b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
936d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
937eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
938eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
939eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
940eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
941eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
942d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
943f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
944d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
94502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
94602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
94702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
94802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
94902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9529901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9537a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9549901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9559901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
956d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
95717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
958d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
959d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
960d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
96102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
96202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
96302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
96402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
96502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
9667557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
967a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
968127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
969127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
970127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
971127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
972b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
97324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
97424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9762c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
9772c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
9782c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
980127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
9812c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
9822c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
983127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
985b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
986b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
987b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
988b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
989b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
990b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
99179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
992b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
99379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
99479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
9952a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
9961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
997e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
99821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
99921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
100021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10012dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
100221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1003fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1004c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1005c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1006c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1007c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1008c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1009c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1010d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1011d325daa506338ab86f9dd468b48fd010673f49a6John McCall
101268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
101368b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
101468b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
101568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
101668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1017c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1018d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1019c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1020d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1021d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1022d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
10237c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10247c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1025d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
102668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
102702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1028ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1029ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           D->getLocation(), D->getDeclName(), T, TInfo,
103016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
10310130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                           D->isInlineSpecified(), D->hasWrittenPrototype());
1032b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1033c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1034c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1035b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1036b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1037b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1038b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1039b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1040b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1041b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1042b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1044e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1045e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
10463019c444c672938c57f5573840071ecd73425ee7John McCall    if (Params[P])
10473019c444c672938c57f5573840071ecd73425ee7John McCall      Params[P]->setOwningFunction(Function);
1048838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Function->setParams(Params.data(), Params.size());
104902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1050ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1051a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1055a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1059a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1062a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // We are instantiating the friend function template "f" within X<int>,
1063a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1065a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1066d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1067a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1068a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1069a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1070a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1071b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1072b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1073d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1074d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1075d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1076d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1077d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1078d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1079d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
108066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
108166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
108224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
108324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1084838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1085910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
108624bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
108724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
108866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
1089d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else if (isFriend && D->isThisDeclarationADefinition()) {
1090d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // TODO: should we remember this connection regardless of whether
1091d325daa506338ab86f9dd468b48fd010673f49a6John McCall    // the friend declaration provided a body?
1092d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
109302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1094a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1095e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1096e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1098e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1099af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1100a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11016826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11026826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11036826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1104af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1105af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1106af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1107af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1108af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1109af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1110af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1111af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1112af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1113af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1114e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1115e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1116e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1117af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1118af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1119af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1120af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1121af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1122af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1123af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1124af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1125af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1126af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1127af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1128af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1129af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1130af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1131af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1132af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1133af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1134af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1135af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1136a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Look only into the namespace where the friend would be declared to
1137a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // find a previous declaration. This is the innermost enclosing namespace,
1138a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
11396826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1140a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1141a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1142a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1143a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1144a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
11456826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
11466826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1147a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1148a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
11499f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1150c80e8115278ba1537c8b517a083ecbd0a018b579Peter Collingbourne                                   isExplicitSpecialization, Redeclaration);
1151e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
115276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
115376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
115476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
115576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1156a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1157a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1158d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
11596826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
116076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1161a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
116276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1163a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
116476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
116576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
116676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1167ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
116877535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1169ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1170238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1171238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1172238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1173238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
117406a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis      if (Function->hasBody(Definition) &&
1175238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1176238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1177238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1178238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1179238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        Function->setInvalidDecl();
1180238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1181238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1182238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1183238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1184238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1185238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
118613a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
118713a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1188ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1189ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
1190ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          if (!queuedInstantiation && R->isUsed(false)) {
1191ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1192ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1193ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1194ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1195ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1196ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1197ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1198ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1199ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1200ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1201ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1202ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1203ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1204238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
12056a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
120606a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis            if (RPattern->hasBody(RPattern)) {
1207238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1208238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
12096a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1210238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1211238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1212238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1213238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1214238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1215238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1216a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1217a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
121876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
121976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
122076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
122176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1222e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1223e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1225d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1226d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1227d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                      TemplateParameterList *TemplateParams) {
12286b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1230d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1233d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
123424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
123524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12372c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
12382c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
12392c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12416b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
12422c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12432c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
12446b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
12456b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1246b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1247b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1248b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1249b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1250b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1251b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
125279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
125379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor    !(isa<Decl>(Owner) &&
125479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12552a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
125648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
12574eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
12584eab39f0745fb1949dbb40c4145771b927888242John McCall  llvm::SmallVector<TemplateParameterList *, 4> TempParamLists;
12594eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
12604eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
12614eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
12624eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
12634eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
12644eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
12654eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
12664eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
12674eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
12684eab39f0745fb1949dbb40c4145771b927888242John McCall    }
12694eab39f0745fb1949dbb40c4145771b927888242John McCall  }
12704eab39f0745fb1949dbb40c4145771b927888242John McCall
12710ca20ac8cea99c43d89510f29cf3dc876f9c9111Douglas Gregor  llvm::SmallVector<ParmVarDecl *, 4> Params;
127221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
127321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
127421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12752dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
127621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
12772dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1278723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1279723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1280723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
12815f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12825f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
12835f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
12845f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
12855f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
12865f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1287723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
12885f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
128912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    llvm::SmallVector<QualType, 4> ParamTypes;
129012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
129112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               D->getNumParams(), TemplateArgs, ParamTypes,
129212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
129312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      return 0;
12945f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
12955f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1296c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1297c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1298c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1299b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1300c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1301c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1302b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1303b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1304b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1305b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1306c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1307b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1308c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1309b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1310c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1311c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1312c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1313b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1314b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1316b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1317b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1318b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1319b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1320b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
13212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1322b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1323dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1325ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
13262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
13272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
132817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1330ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
133216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
133316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        false);
133417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
133517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1336ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
13372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
133816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
133965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
134065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1341ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
13420130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1343f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
1344f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->getLocEnd());
1345dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
13462577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1347ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
134816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
134916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
1350f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->isInlineSpecified(),
1351f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->getLocEnd());
1352dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
13536b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1354c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1355c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1356b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1357d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1358d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1359d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
13601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1361d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1362d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1363d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1364d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1365d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1366d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1367d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1368d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1369d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1370d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1371d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1372d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1374d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1375b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1376b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1377b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1378b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
13791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1380d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
138166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
138266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
138324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor    std::pair<const TemplateArgument *, unsigned> Innermost
138424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1385838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1386910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1387910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1388910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
138966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1390b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
139166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
13922db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
139366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
139466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
13967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
13977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1398b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
13994eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
14004eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
14014eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
14024eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
14034eab39f0745fb1949dbb40c4145771b927888242John McCall
1404b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1405b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1406b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
14077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
14105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
14115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
1412838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  Method->setParams(Params.data(), Params.size());
14135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14145545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
14155545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
14162dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
14172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
14182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1420b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1421b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1423dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1424dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1425dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1426dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
14276826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
14286826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1429dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14302dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
143165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
1432c80e8115278ba1537c8b517a083ecbd0a018b579Peter Collingbourne  SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
143365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
14344ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
14354ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
14364ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
143746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
143846460a68f6508775e98c19b4bb8454bb471aac24John McCall
14399eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
14409eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
1441b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1442b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1443b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1444b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1445b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1446b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1447b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1448b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1449b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1450b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1451b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    else
1452b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1453b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1454bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
14552dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
14562dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14572dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1458615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1459dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1460615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1461615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
146203b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
146317e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
146403b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
146503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1466bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
146765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1468bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1469bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
14706477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1471fb44de956f27875def889482b5393475060392afJohn McCall  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
1472fb44de956f27875def889482b5393475060392afJohn McCall                                  llvm::Optional<unsigned>());
14732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
14742dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1475e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1476e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1477e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
14784fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1480e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1481344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1482344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
14834fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
14844fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1485e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1486e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
14879a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
14889a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor
14890f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
14900f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1491e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1492550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1493550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1494550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1495550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1496e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1497e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1498e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
149933642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
150033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
150133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
15026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
15036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
15046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes;
15056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
15066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeSourceInfo *DI;
150733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
150833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
15096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
15116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack is an already-expanded pack
15126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
15136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
15146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
15156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
15166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
15176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
15186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getLocation(),
15196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
15206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
15216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
15246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
15256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
15266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
15276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
15296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
15306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
15326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
15336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
15346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else if (isa<PackExpansionTypeLoc>(TL)) {
15356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
15366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
15376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
15386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
15396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
15406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
15416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
15426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
15446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
15456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
15466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
15476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> OrigNumExpansions
15486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
15496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
15506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
15516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
15526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Unexpanded.data(),
15536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Unexpanded.size(),
15546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
15556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Expand, RetainExpansion,
15566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
15576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
15586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
15606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
15616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
15626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
15636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation(),
15646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
15656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
15666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
15676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
15696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
15706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
15716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
15726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
15736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
15746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
15756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
15766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
15786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
15796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
15806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
15816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
15826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
15836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
15846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
15856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
15866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
15876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
15886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getLocation(),
15896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
15906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
15916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
15946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
15956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
15966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
15976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
15996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
16006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
16016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
16026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
16036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
16046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
16056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
16066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
16076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
16086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Invalid = false;
16096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
16106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
16116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
16126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
16136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
16146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
161533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
161633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
16176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
16186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
16196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1620ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1621ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
162271b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
16236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getPosition(),
16246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
16256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
16266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
16276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
16286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
16296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
16306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1631ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
16326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
16336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    D->getDepth() - TemplateArgs.getNumLevels(),
16346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getPosition(),
16356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
16366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
16376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
16389a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
163933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
164033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
164133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
1642d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1643550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1644550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Introduce this template parameter's instantiation into the instantiation
1645550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1646550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
164733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
164833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
164933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
16500dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
16519106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
16529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
16539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
16549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
16559106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
16569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
16579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
16589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
16592a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
16609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
16619106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
16629106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
16639106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  }
16649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
16659106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
16669106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
16679106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
166871b87e4fa6bfb47107b099135864f9024004a4c9Douglas Gregor                                   D->getDepth() - TemplateArgs.getNumLevels(),
166961c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getPosition(), D->isParameterPack(),
167061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1671d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
16729a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
16734469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
16749106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Introduce this template parameter's instantiation into the instantiation
16759106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
16769106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
16779106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
16789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
16799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
16809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
168148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1682db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
1683db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
168448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
168548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
168648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
168748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNamespaceKeyLocation(),
1688db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
168948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getIdentLocation(),
169048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getNominatedNamespace(),
169148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
169248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
169348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
169448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
169548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1696ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
16971b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
16981b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
16991b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
17001b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
17011b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
17021b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
17031b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
17041b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
17051b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
17065149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
17075149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
17085149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
17095149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
1710dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
17111b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
17121b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
17131b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1714ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1715ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17169f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
17179f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
17189f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
17199f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
17209f54ad4381370c6b771424b53d219e661d6d6706John McCall
1721ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1722ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
17239f54ad4381370c6b771424b53d219e661d6d6706John McCall
1724ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1725ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
17265149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
1727ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1728ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1729ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17305149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
17315149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
17329f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
17339f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
17349f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
17359f54ad4381370c6b771424b53d219e661d6d6706John McCall
17369f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
17379f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
17389f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
17399f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
17409f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
17419f54ad4381370c6b771424b53d219e661d6d6706John McCall
17429f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
17439f54ad4381370c6b771424b53d219e661d6d6706John McCall
17449f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
17459f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1746ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1747ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
17489f54ad4381370c6b771424b53d219e661d6d6706John McCall
1749ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1750ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1751ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1752ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17539f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
17549f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
17559f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
17569f54ad4381370c6b771424b53d219e661d6d6706John McCall
1757323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1758323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
17599f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
17609f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
17619f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
17629f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
17639f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
1764b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1765b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
1766b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
1767b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
1768b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
1769b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
17709f54ad4381370c6b771424b53d219e661d6d6706John McCall
17719f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
17729f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
17739f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
17749f54ad4381370c6b771424b53d219e661d6d6706John McCall
17759f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
17769f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
17779f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1778323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1779323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1780323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
17819f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1782ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1783ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1784ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1785ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1786ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
17879f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
17889f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1789ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1790ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
17927ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
17935149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
17945149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
17955149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
17965149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
17977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
17987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
17997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
18005149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1802ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1803ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1804ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
18057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
18067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1807ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
18087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
18097ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
18104469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1811ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1812ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
18147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
18157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
18167ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
18177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
18185149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
18195149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
18205149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
18210dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
18225149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor
18230dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
18245149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1826ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1827ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1828ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
18291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
18309488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1831ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
18327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
18337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
18344469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1835ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1836ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18370d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
18380dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
18390dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1840ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1841d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
18427e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
18432fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
18442fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
18452fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
18468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
18478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
18488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1849e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1850e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1851e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1852e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1853e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1854e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1855e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1856ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1857e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1858e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1859e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1860e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
1861bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
1862e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1863e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1864e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1865e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1866bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1867e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
18689148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1869e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1870e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1871e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1872ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1873e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1874e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1875e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1876e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1877e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1878e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1879e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
18801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1881e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1882ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \brief Instantiate the declaration of a class template partial
1883ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1884ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1885ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1886ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1887ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1888ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param PartialSpec the (uninstantiated) class template partial
1889ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1890ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1891d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
1892d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
1893d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
1894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1895ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1896ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1897550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1898550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1899550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
19002a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1901550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor
1902ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
1903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1905ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1906ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
1907d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1908ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1909ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
1910ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
1911d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
1912e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
1913e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    PartialSpec->getNumTemplateArgsAsWritten(),
1914e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
1915e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
1916ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1917ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
1918ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
1919910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Converted;
1920ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1921ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
1922d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                        InstTemplateArgs,
1923ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
1924ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
1925d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1926ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1927ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
1928ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
1929ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
1930ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
1931910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
1932910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
1933ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1934ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
1935ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
1936ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  QualType CanonType
1937ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1938910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
1939910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
1940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1941ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
1942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
1943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
1944ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
1945ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
1946ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
1947ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
19483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
19493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
19503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
19513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
1952d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
1953ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
1954ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
1956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
1957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
1958ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
1959ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
1960ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
1961ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1962ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
1963ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
1964ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
1965ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
1966ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
1967ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
1968ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
1969ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
1970ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
1971ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1972d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
1973ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1974ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
1975d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
1976ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
1977ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1978ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
1979ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
1980ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
198113c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
198213c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
198313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     Owner,
1984ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
1985ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
1986ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
1987ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     ClassTemplate,
1988910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
1989910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
1990d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
19913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
1992dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
1993cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
1994b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1995b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
1996b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1997b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1998ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
19994469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
20004469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor
2001ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2002ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
2003cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
2004d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2005ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2006ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
200721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
200821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
200921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall                              llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
201021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
201121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
201221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
20136cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
20146cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
20156cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
20166cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
201721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
201821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
20195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2020cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2021cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2022140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
20236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
20246920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2025140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
20266920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
20276920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
202812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
202912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
203012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
203112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
203212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        if (!OldParam->isParameterPack() ||
203312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor            (NewIdx < NumNewParams &&
203412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
203512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // Simple case: normal parameter, or a parameter pack that's
203612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
203712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
203812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
203912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
204012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                               NewParam);
204112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          continue;
204212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
204312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
204412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        // Parameter pack: make the instantiation an argument pack.
204512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
204612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      OldParam);
204721371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        unsigned NumArgumentsInExpansion
204821371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
204921371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor                                               TemplateArgs);
205021371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        while (NumArgumentsInExpansion--) {
205112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
205212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
205312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
205412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      NewParam);
205512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
20566920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2057895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2058cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2059cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2060cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2061cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2062140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
20636920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
20646920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
20656920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
20666920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
20676920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
20686920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
20696920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
20706920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2071cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2072cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
207321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
20745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
20755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2077e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2078e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2079e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
20811eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2082e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
2083e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (Tmpl->isDeleted())
2084e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    New->setDeleted();
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2086cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2087cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2088cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
20891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
20901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2091cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2092cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2093cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2094cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2095cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2096cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2097cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
20981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
2099cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
21001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2101cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2102bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2103cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2104cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
2105f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
2106cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2107cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21090ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
21100ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
21110ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
211260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
21130ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
21140ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
21150ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    llvm::SmallVector<QualType, 4> Exceptions;
21160ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
21170ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
2118b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
2119b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2120b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
2121b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2122b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2123b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
2124b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        assert(!Unexpanded.empty() &&
2125b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
212660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2127b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
2128d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        bool RetainExpansion = false;
2129cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        llvm::Optional<unsigned> NumExpansions
2130cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                          = PackExpansion->getNumExpansions();
2131b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2132b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
2133b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.data(),
2134b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    Unexpanded.size(),
2135b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
2136d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    Expand,
2137d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    RetainExpansion,
2138d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    NumExpansions))
2139b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
214060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2141b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
2142b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
2143cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // just substitute into the pattern and create a new pack expansion
2144cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // type.
2145b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2146b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2147b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2148b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2149b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2150b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2151b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2152cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2153b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2154b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2155b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
215660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2157b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2158b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2159cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2160b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2161b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
2162b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2163b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2164b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2165b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2166b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2167b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2168b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
216960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2170b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2171b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
217260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2173b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2174b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
217560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2176b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2177b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2178b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor
21790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
21800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
21810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
21820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      if (T.isNull() ||
21830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
21840ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
21850ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
21860ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
21870ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
218856fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    Expr *NoexceptExpr = 0;
218956fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
219056fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
219156fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      if (E.isUsable())
219256fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl        NoexceptExpr = E.take();
219356fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    }
21940ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
21950ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // Rebuild the function type
21960ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2197e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
219860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2199e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2200e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
220156fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    EPI.NoexceptExpr = NoexceptExpr;
2202e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2203e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
22040ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
22050ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
22060ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
22070ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
22080ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
22090ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2210e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
22110ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
22120ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
22131d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
22147cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2215e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2216e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2217e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
22185545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
22195545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
22205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
22215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
22225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
22241eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
22255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2226e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2227e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22295545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2230e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
223185606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
22325545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
22335545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
22345545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
22355545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
22365545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2237a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2238a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2239a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2240a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2241b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2242b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2243b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2244b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2245a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2246b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2247b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2248b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2249b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2250b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2251e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2252e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2253e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2254e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2255f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2256b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2257e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2258e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
225906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  if (Function->isInvalidDecl() || Function->hasBody())
226054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
226154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2262251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2263251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2264251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
22656cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
22661eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
22673b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
22681eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  Stmt *Pattern = 0;
22691eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  if (PatternDecl)
22706fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis    Pattern = PatternDecl->getBody(PatternDecl);
22711eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
22728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
22738387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  if (PatternDecl->isLateTemplateParsed() && !LateTemplateParser) {
22748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
22758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
22768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
22778387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
22788387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
22798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
22808387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // a templated function definition.
22818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  if (!Pattern && PatternDecl && PatternDecl->isLateTemplateParsed() &&
22828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
22834a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
22848387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
22858387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
22868387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2287e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  if (!Pattern) {
2288e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2289e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2290e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2291e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2292e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2293e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2294e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PointOfInstantiation,
2295e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2296e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2297e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor
2298e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2299e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        Diag(PatternDecl->getLocation(),
2300e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2301cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
230258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
230358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
230462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
230558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2306e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
230758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
23081eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2309e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
23101eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2311d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2312d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
23131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2314d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
23151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2316d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
23177ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2318d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2320f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2321f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2322e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    return;
2323e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor
2324b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2325b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2326b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
23272a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  llvm::SmallVector<VTableUse, 16> SavedVTableUses;
232862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
23292a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
23302a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
233162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
23322a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
23331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23349679cafc6368cceed1a5e69d3038d0316401b352Douglas Gregor  EnterExpressionEvaluationContext EvalContext(*this,
2335f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2336d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2337e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
233854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
233960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
234060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
234160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
234260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
234360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
234460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
234560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
234660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
23498a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
23508a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
235112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  unsigned FParamIdx = 0;
23528a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
23538a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
235412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (!PatternParam->isParameterPack()) {
235512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      // Simple case: not a parameter pack.
235612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      assert(FParamIdx < Function->getNumParams());
235712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
235812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
235912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocal(PatternParam, FunctionParam);
236012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ++FParamIdx;
236112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      continue;
236212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
236312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor
236412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    // Expand the parameter pack.
236512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    Scope.MakeInstantiatedLocalArgPack(PatternParam);
236612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    for (unsigned NumFParams = Function->getNumParams();
236712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         FParamIdx < NumFParams;
236812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         ++FParamIdx) {
236912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
237012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
237112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
237212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
23738a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
237454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2375b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2376b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2377eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  Sema::ContextRAII savedContext(*this, Function);
2378b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
23791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2380e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2381090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2382090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // If this is a constructor, instantiate the member initializers.
23831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const CXXConstructorDecl *Ctor =
2384090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson        dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2385090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2386090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                               TemplateArgs);
23871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
23881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
238954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Instantiate the function body.
239060d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2391e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
239252604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor  if (Body.isInvalid())
239352604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor    Function->setInvalidDecl();
239452604ab71a74b8ec481255dfeea7dc9dba63b1a5Douglas Gregor
23959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  ActOnFinishFunctionBody(Function, Body.get(),
2396e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor                          /*IsInstantiation=*/true);
2397b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
23980c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
23990c01d18094100db92d38daa923c95661512db203John McCall
2400eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  savedContext.pop();
2401aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2402aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2403aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
240560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
240660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
240762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
240860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
240960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2410b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
24112a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
24122a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
24132a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2414b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
24151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
241662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
24171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24182a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
24192a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
24202a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2421b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
242262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2423b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2424a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2425a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2426a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2427a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2428a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
24297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
24307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
24317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
24327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
24337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
24347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
24357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
24367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
24377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2438e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2439e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2440e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2441e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
24427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
24437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
24447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2445e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2446e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
24477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
24487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
24491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
24517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
24527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
24530d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  assert(Def->isStaticDataMember() && "Not a static data member?");
24540d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
24551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24560d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
24577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
24587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
24591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
24601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2461e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
24620d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2463e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(PointOfInstantiation,
2464e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2465e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2466e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
246758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
246858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
246962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
247058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
247158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
247258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
24737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
24747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
24757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2476251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
24771028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2478251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2479251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor
2480251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2481251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2482251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2483251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
24841028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind()
2485251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2486251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
24871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
24897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
24907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
24911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
24937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
24947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
249562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
24967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive)
249762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
24981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
25007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
2501f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25031028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2504ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
25056bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
2506f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
2507f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
25087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
25097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2510583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2511583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2512583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2513583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
25147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
25157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
25167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
25171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
25197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
25201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
252162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
25221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
252462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
25251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2526a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2527815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2528090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2529090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2530090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2531090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
25321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2533090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  llvm::SmallVector<MemInitTy*, 4> NewInits;
25349db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
25359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2536090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2537090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
253872f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
253972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2540cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2541090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2542030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2543030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2544030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2545030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2546030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
25476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2548ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
25491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25503fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
25513fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25523fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
25533fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
25543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
25553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
25563fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
25573fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2558d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2559cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
25603fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
25613fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
25623fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.data(),
25633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          Unexpanded.size(),
25643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          TemplateArgs, ShouldExpand,
2565d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
25663fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
25673fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
25683fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
25693fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
25703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
25713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
25723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // Loop over all of the arguments in the argument pack(s),
2574cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
25753fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
25763fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
25783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
25793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                   LParenLoc, NewArgs, RParenLoc)) {
25803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
25813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
25823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
25833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25843fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
25853fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
25863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              TemplateArgs,
25873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              Init->getSourceLocation(),
25883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
25893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
25903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
25913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
25923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
25933fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
25943fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
25953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
25963fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     BaseTInfo,
25973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     (Expr **)NewArgs.data(),
25983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     NewArgs.size(),
25993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getLParenLoc(),
26003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     Init->getRParenLoc(),
26013fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
26023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
26033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
26043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
26053fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
26063fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
26073fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
26083fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
26093fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewArgs.clear();
26103fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
26113fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
26123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
26133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
26143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
26156b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
26166b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2617a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
26186b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
26196b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2620090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
26219db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
2622090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2623090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2624a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2625802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            TemplateArgs,
2626802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            Init->getSourceLocation(),
2627802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2628a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
26299db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2630802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2631802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2632802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
2633802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor
2634a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
26351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     (Expr **)NewArgs.data(),
2636090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     NewArgs.size(),
2637802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                     Init->getLParenLoc(),
2638090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                     Init->getRParenLoc(),
26393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     New->getParent(),
26403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                     EllipsisLoc);
2641090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
2642b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
264300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
264400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
264500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
2646b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
2647b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2648b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
2649b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
2650b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
26511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
2653090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       NewArgs.size(),
2654090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getSourceLocation(),
2655802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                       Init->getLParenLoc(),
2656090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                       Init->getRParenLoc());
265700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
265800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
2659b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
266000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
266100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
266200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
2663b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
2664b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2665b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
2666b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
2667b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
2668b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor
266900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(),
267000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       NewArgs.size(),
267100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getSourceLocation(),
267200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getLParenLoc(),
267300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                       Init->getRParenLoc());
2674090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2675090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
26769db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
26779db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2678090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
26799db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2680090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2681090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
26821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2683090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewInits.push_back((MemInitTy *)NewInit.get());
2684090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2685090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
26861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2687090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2688d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2689090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2690090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
26919db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
26929db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2693090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2694090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
269552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
269652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
269752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
269852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
269952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
270052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
270152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
270252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
270352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
270452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
270552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
270652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
270752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
270852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
270952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
27100d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
27110d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
27120d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
27130d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
27140d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
27150d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
27160d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
27170d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
27180d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
27190d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
27200d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
27210d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
27220d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2723ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregorstatic bool
2724ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2725ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2726ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  Pattern
2727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2731ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2732ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2735ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2736ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
273952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
274052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
274152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
274252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
274352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
274452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
274552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
274652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
274752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
274852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
274952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
275052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
275152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
275252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
275352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
275452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
275552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
275652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
275752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
275852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
275952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
276052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
276152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
276252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
276352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
276452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
276552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
276652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
276752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
276852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
276952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
277052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
277152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
277252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
277352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
277452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
277552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
277652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
277752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2778ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2779ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2780ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2781ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2782ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2783ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2784ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2785ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2786ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2787ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2788ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2789ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
27907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
27917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
27927ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2793ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
27947ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
27957ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
27967ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
27970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
27980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2799ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
28000d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
28010d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
280252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
280352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
280452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
280552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
280652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
280752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
280852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
280952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
281052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
281152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
281252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
281352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
281452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
281552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
281652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2817ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2818ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2819815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
28200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
28217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
28227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
28237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
28247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
28257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
28267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
28277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
28287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
28297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
28300d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
28310d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
28320d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
28330d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2834815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
28350d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
28360d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
28371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
283852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
283952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
28401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
284152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
284252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
2843815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
284452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
284552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
2846815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
28477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
284852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
284952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
285052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
285152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
285252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
2853a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
28540d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
28550d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
28560d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2857ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
2858ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2859ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2860ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
2861ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2862d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2863d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
2864d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
2866d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
2867d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
2868d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
28691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2870ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2871ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2872ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2873ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2874ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2875ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2876815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
2877815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2878815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2879815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2880815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
28811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
2882815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
2883815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
2884815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
2885815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
2886815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
2887815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
2888815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2889815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
2890815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
2891815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
289202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
289302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
289402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
289502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
28967c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
2897e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
289802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
28997c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
290002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
290102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
290202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
290302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
2904ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
2905ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
2906815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2907815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
2908815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
2909815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
2910815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
2911815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2912815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
2913815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
2914815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
2915815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
2916815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
2917815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
2918815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2919815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
2920815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
2921815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2922815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
2923815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
2924815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
2925815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
2926815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
2927815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
2928ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2929ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
29307c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
2931e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
2932815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
2933550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
29346d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
2935766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
29362bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
29372bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
2938d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
2939d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
2940d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
2941d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner
294257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
294357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
294457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
294557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
294657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      unsigned PackIdx = ArgumentPackSubstitutionIndex;
294757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
294857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
294957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
295057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
295157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
295257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
295357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
295457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
295557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
2956d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner
295757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
295857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
29592bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
2960815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
2961e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2962e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
2963e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
2964e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
29658b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
29668b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
29678b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
29688b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
29698b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
2970e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
2971e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2972e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2973e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
297424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
2975e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2976e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2977e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
29783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
29793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
29803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
29813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
29823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
29833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
298431f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
29853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
2986e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
2987e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
29888b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
29898b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
29908b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
29918b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
29928b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
29938b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // wouldn't have gotten to the injected-class-name!
29948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
29958b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
29968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
2997b46ae3964ba02535276c71332396e9a7bad2dfa5Douglas Gregor      T = SubstType(T, TemplateArgs, Loc, DeclarationName());
2998e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2999e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
3000e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
3001e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
3002e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
3003e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
3004e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
30058b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
30068b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
30078b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
30088b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
30098b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
30108b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
30118b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
30128b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
30131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
301452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
30151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
30168b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
3017e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor          if (isInstantiationOf(ClassTemplate,
3018e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
301952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
30208b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
30218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
30228b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
302352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
302452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
30258b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
30268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
30278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
30288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(!SawNonDependentContext &&
30298b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
30308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
30318b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      assert(DC &&
303252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
30338b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
303452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
30358b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3036e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3037e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3038e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
303952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3040e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3041e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3042e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor
30437c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
30441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
304544c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
30461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3047815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3048815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3049815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3050815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
30517c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
30523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
30533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3054eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
30553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
30567c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
30577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
30583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
30593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3060eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3061eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
30623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
30633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
30643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3065a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3066a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
30677c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
30687c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
30697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3070815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3071815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
307217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
3073815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
3074815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3075815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3076815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3077815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3078815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3079815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3080815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3081815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3082815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
30831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
308417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
308517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
30871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3088eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3089eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3090eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3091eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3092eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3093eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3094eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3095eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3096eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // The class in which this member exists is currently being
3097eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3098eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3099eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3100eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3101eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3102eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3103eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3104eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
3105eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3106eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3107eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3108eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3109eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3110eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor
3111815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3112815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3113815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3114815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3115815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3116d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
31171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3118d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
3119788440378c442562497c09610939cbe6218ab43dDouglas Gregor///
3120788440378c442562497c09610939cbe6218ab43dDouglas Gregor/// \returns true if anything was instantiated.
3121788440378c442562497c09610939cbe6218ab43dDouglas Gregorbool Sema::PerformPendingInstantiations(bool LocalOnly) {
3122788440378c442562497c09610939cbe6218ab43dDouglas Gregor  bool InstantiatedAnything = false;
312360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
312462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
312560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
312660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
312760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
312862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
312962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
313060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
313160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
313260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
313360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
31341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
31367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3137f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3138f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
313958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
314058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
314158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
314258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
3143788440378c442562497c09610939cbe6218ab43dDouglas Gregor      InstantiatedAnything = true;
31447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
31457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
31461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
31487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
31497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3150c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3151291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3152291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3153291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
3154291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3155291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3156291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3157291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3158291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3159291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3160291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      assert(false && "Cannot instantitiate an undeclared specialization.");
3161291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3162291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
316358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
316458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
316558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
316658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
316758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
3168291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3169291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3170291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3171291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3172f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3173f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3174f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
31751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
317658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
317758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
317858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
317958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3180788440378c442562497c09610939cbe6218ab43dDouglas Gregor    InstantiatedAnything = true;
3181d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3182788440378c442562497c09610939cbe6218ab43dDouglas Gregor
3183788440378c442562497c09610939cbe6218ab43dDouglas Gregor  return InstantiatedAnything;
3184d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
31850c01d18094100db92d38daa923c95661512db203John McCall
31860c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
31870c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
31880c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
31890c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
31900c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
31910c01d18094100db92d38daa923c95661512db203John McCall
31920c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
31930c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
31940c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
31950c01d18094100db92d38daa923c95661512db203John McCall      break;
31960c01d18094100db92d38daa923c95661512db203John McCall    }
31970c01d18094100db92d38daa923c95661512db203John McCall  }
31980c01d18094100db92d38daa923c95661512db203John McCall}
3199