SemaTemplateInstantiateDecl.cpp revision 5c340e803ed2e384ff47f3e560df253515c92e1e
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;
32a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
33c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
34a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                          TemplateArgs);
36a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
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;
48a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
49c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
50a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                        TemplateArgs);
52a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
53c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
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,
6219f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola                            const 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,
82a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                             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) {
99b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1004f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1014f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1024f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
10357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
10457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
10557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
10657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
10757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
10857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
10957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
11057ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1114f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
112b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1134f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1144f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1153dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1163dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1173dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1183dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1193dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1203dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1210cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1220cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1233dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1243dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1253dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1263dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1273dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1283dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
132a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
133561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
134836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
135ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
136ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
137ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
139a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
141b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
142b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
146162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
147162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
148162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
149162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
150162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
151162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
152162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
1538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
1548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
1558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
156cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
157cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
158cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
159cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
160162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (oldTag->getTypedefNameForAnonDecl() == D) {
161cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
162162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl());
163162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
164cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
165d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
166a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
167162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (TypedefNameDecl *Prev = D->getPreviousDeclaration()) {
1687c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1697c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
170b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
171b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
172a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
173162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef->setPreviousDeclaration(cast<TypedefNameDecl>(InstPrev));
1745126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
1755126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
1761d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
177d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
17846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
1818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
1828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
183162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
1843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
1853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
1863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
187162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
188162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
189162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
1903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
1913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
1923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
1933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
1943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
1963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
1983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
1993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
2093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (Pattern->getPreviousDeclaration()) {
2103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (Found.first != Found.second) {
2123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(*Found.first);
2133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
2173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
2183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
2193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
2223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
2243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
2253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
2263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
2283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
2303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
231a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
2333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
235162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
236162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
2376b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \brief Instantiate an initializer, breaking it into separate
2386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initialization arguments.
2396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2406b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param Init The initializer to instantiate.
2416b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2426b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param TemplateArgs Template arguments to be substituted into the
2436b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// initializer.
2446b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2456b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \param NewArgs Will be filled in with the instantiation arguments.
2466b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor///
2476b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor/// \returns true if an error occurred, false otherwise
2480ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smithbool Sema::InstantiateInitializer(Expr *Init,
2496b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                            const MultiLevelTemplateArgumentList &TemplateArgs,
2500ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  SourceLocation &LParenLoc,
2510ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  ASTOwningVector<Expr*> &NewArgs,
2520ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                  SourceLocation &RParenLoc) {
2536b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.clear();
2546b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  LParenLoc = SourceLocation();
2556b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  RParenLoc = SourceLocation();
2566b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2576b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (!Init)
2586b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return false;
2596b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2604765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init))
2616b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ExprTemp->getSubExpr();
2626b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2636b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
2646b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = Binder->getSubExpr();
2656b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2666b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
2676b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    Init = ICE->getSubExprAsWritten();
2686b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2696b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
2706b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    LParenLoc = ParenList->getLParenLoc();
2716b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    RParenLoc = ParenList->getRParenLoc();
2720ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith    return SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(),
2730ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                      true, TemplateArgs, NewArgs);
2746b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
2756b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2766b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
27728329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    if (!isa<CXXTemporaryObjectExpr>(Construct)) {
2780ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith      if (SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true,
2790ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                     TemplateArgs, NewArgs))
28028329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor        return true;
28128329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor
28228329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      // FIXME: Fake locations!
2830ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith      LParenLoc = PP.getLocForEndOfToken(Init->getLocStart());
284a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor      RParenLoc = LParenLoc;
28528329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor      return false;
28628329e511854fdd3b31561b2690f91f9e6a6402eDouglas Gregor    }
2876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  }
288a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2890ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith  ExprResult Result = SubstExpr(Init, TemplateArgs);
2906b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  if (Result.isInvalid())
2916b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    return true;
2926b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  NewArgs.push_back(Result.takeAs<Expr>());
2946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor  return false;
2956b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor}
2966b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor
2973d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
2989901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
2999901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3009901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3019901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3029901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3039901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3049901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
305ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
306a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3070a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3080a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3090a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3100a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3113d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3123d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
313c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
314c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
315c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
316c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
317c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
318a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
319b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
321ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 D->getInnerLocStart(),
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3230a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
32416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
32516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3263d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3273d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
328ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
3291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
330b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
331b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
332b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
333b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34046460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
341a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3426b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
343c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3446b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3456b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
346a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
347390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
348390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3493d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  bool Redeclaration = false;
3506826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3516826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
352449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
35360c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
35460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3556826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
3561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
358ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara    if (!D->isStaticDataMember())
359ea7b4880bc07cd99b3994c2a95d722a06ab56594Abramo Bagnara      D->getLexicalDeclContext()->addDecl(Var);
3607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
363f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
364f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3661d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
367a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
368251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
369251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
370251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
371a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
372cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
373a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37460c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
37560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
37660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
3771f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
3781f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
3791f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
3801f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
3811f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
3826b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
3836b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
384ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> InitArgs(SemaRef);
3850ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith    if (!SemaRef.InstantiateInitializer(D->getInit(), TemplateArgs, LParenLoc,
3860ff6f8f038f32d85c32fd984673cea51ef737b22Richard Smith                                        InitArgs, RParenLoc)) {
38734b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
38807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      // Attach the initializer to the declaration, if we have one.
38907a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      if (InitArgs.size() == 0)
39034b41d939a1328f484511c6002ba2456db879a29Richard Smith        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
39107a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      else if (D->hasCXXDirectInitializer()) {
3926eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor        // Add the direct initializer to the declaration.
393d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        SemaRef.AddCXXDirectInitializerToDecl(Var,
3946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor                                              LParenLoc,
3956eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor                                              move_arg(InitArgs),
39634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              RParenLoc,
39734b41d939a1328f484511c6002ba2456db879a29Richard Smith                                              TypeMayContainAuto);
39807a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor      } else {
39907a77b4b1d1fa95930129541eff8b79558f5d80dDouglas Gregor        assert(InitArgs.size() == 1);
4009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall        Expr *Init = InitArgs.take()[0];
40134b41d939a1328f484511c6002ba2456db879a29Richard Smith        SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto);
40283ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor      }
4036eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4066b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4076eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
408a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4091f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
410ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
411ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
412d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4133d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
414e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // Diagnose unused local variables with dependent types, where the diagnostic
415e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // will have been deferred.
416e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
417e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith      D->getType()->isDependentType())
4185764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
419bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4203d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4213d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4236206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4246206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4256206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4266206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4276206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4286206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4296206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4306206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
433a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
434561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
435836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
43607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
43707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
43807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
439a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
44007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
44107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
44907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
452b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
453b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
460ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    // The bit-width expression is not potentially evaluated.
461f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
464ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
469e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
47207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
47307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
4787a614d8380297fcd2bc23986241905d97222948cRichard Smith                                            D->hasInClassInitializer(),
479ea218b8e8f9ba82d1c76bcb7e86d121a5f65ebedSteve Naroff                                            D->getTypeSpecStartLoc(),
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
482663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
483663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
484f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
485663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4871d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
488a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
489f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
490f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
492f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
493f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
494f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
495a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
4969901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4979901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4987a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4999901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
50346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
504f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
50987c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
51087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
51187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
51287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
51387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
51487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
51587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
516b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
518b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
519b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
520b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
521a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
522b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
523b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
52487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52540e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
52687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
52787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
52840e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
52987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
53087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
53387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
53487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
53587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
53687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
53787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
53902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
54006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
54132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5424fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5434fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5454fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5464fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5474fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5484fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5494fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5504fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5514fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5524fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5534fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
55406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
555fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
55606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
55706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
55806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
559a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5619a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
56206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
56306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
564a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
565a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
56706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
56832f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
569af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
570af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
571af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
572af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
573af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
57406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
577a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
57806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5795fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5809a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
58102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
58202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
583fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
584fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
588ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor  // The expression in a static assertion is not potentially evaluated.
589f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
592ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
59660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Message(D->getMessage());
5973fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall  D->getMessage();
598d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
5999ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
600a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              Message.get(),
601a2026c96d3935e7909e049ad9096762844544ed6Abramo Bagnara                                              D->getRParenLoc());
6028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
605ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
607a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    /*PrevDecl=*/0, D->isScoped(),
608a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6091274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
6101274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) {
6111274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6121274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6131274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6141274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI,
6161274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       TemplateArgs,
6171274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       UnderlyingLoc,
6181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor                                                       DeclarationName()));
619a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
6201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      if (!Enum->getIntegerTypeSourceInfo())
6211274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
6221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6231274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    else {
6241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6261274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6271274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6305b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6315b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6328dbc3c64965f99e48830885835b7d2fc26ec3cf5Douglas Gregor  Enum->setInstantiationOfMemberEnum(D);
63306c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
634b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
63517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Enum->startDefinition();
6378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
63896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
63996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
640a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
6415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
64417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
64517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         ECEnd = D->enumerator_end();
6468dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
64860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
649ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
650ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor      // The enumerator's value expression is not potentially evaluated.
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
652f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   Sema::Unevaluated);
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
655ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
6618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
6628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
6658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
6668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
6679ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
6688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
6708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
6728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
6738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
6765b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
6775b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
6783b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
67917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
680d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
6818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
682a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
68396084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      if (D->getDeclContext()->isFunctionOrMethod()) {
68496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
68596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
68696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
68796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
6888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
6898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
691c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  // FIXME: Fixup LBraceLoc and RBraceLoc
692fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
693c6e35aae23bc3cea7daf5ee075fa695c01c0f66fMike Stump  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
694d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                        Enum,
695de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
696fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
6978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Enum;
6998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7016477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
702b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7036477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7046477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
705e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
70693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
70793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
708550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
709550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7102a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
711e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
712ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
714d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
715e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
716e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
71793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
71893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
71993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
72093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
721c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
722c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
723c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
724c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
725c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
726c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
72793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
72893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
72993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
73093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
73193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
73237574f55cd637340f651330f5cfda69742880d36Nick Lewycky  if (!isFriend && Pattern->getPreviousDeclaration()) {
73337574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
73437574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (Found.first != Found.second) {
73537574f55cd637340f651330f5cfda69742880d36Nick Lewycky      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first);
73637574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
73737574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
73837574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
73937574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
74037574f55cd637340f651330f5cfda69742880d36Nick Lewycky
74193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
74293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
74593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
74693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
747c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
749c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
75093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
75193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
75293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
75393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
76093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
76193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
76393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
770c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
7721eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
773c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
77493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
77593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
77693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
777c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
779c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
780c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
781c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
782c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
783c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
784c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
785c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
786c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
787a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
788c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
789c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
790c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
791c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
792c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
793c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
794c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
795c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
796c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
797c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
798c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
799c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
800c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
801c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
802c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
803c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
804c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
80993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
810a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
811c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
812c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
813c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
814c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
815c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
816c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
817c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
81893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
821c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
822c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
82393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
82693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
82793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
828e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
82993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
830ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
831ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
832f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
833e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
834c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
835c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
836b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
837e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
83893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
83993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
84093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
841e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
842ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
84393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
844ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
845ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
846ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
847ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
848ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
85193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
85293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
853e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
85437574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
85537574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
85693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
857a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
858f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
86024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
861ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
862259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
86493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
865e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
866259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
867a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
868e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
869d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
870d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
871d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
872d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
873d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
8745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
875d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
876d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
877d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
878d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
879d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
880d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
881e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
882e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
883e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
884d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
8857974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
8867974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
887ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
889ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
890ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
891ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
892ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
893ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (Found.first == Found.second)
894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
895a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
896ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
897ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = dyn_cast<ClassTemplateDecl>(*Found.first);
898ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
899ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
900a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
901d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
902d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
903d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
904d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
905d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9067974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9077974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
9087974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
909d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
910550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
911550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
912a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
913550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
9142a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
915895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
916d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
917d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
9181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
919d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
920a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
921a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
922a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
923a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
924a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
925a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
926a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
927a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
928a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
929a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
930a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
931d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
932d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
93346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Instantiated->setAccess(D->getAccess());
93446460a68f6508775e98c19b4bb8454bb471aac24John McCall
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
936d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
937a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
938a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
93937d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
940a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
941a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
942e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
943b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
944b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
945e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
946e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
947e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
948b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
949a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
950a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
951b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
952b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend)
953a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
954b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
955d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
956d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
957d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
958d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
959d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
960d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
961d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
9626c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  else if (D->getPreviousDeclaration()) {
9637c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
9647c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                   D->getPreviousDeclaration(),
9656c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
9666c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
9676c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
9686c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
969d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
970d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
972ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
973ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
974b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
975b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
976b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
977b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
978b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
979d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
980eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
981eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
982eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
983eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
984eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
985d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
986f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
987d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
98802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
98902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
99002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
99102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
99202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
9939901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
9949901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
9959901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
9967a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
9979901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
9989901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
999d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
100017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1001d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1002d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1003d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
100402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
100502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
100602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
100702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
100802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
10097557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1010a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1011127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1012127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1013127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1014127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  void *InsertPos = 0;
1015b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1016a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
101724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10192c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
10202c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
10212c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1023127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
10242c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
10252c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1026127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
10271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1028b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1029b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1030b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1031b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1032b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1033b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
103479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1035b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1036a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
103779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
10382a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
104121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
104221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
104321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
10442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
104521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
1046fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1047c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1048c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1049c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1050c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1051c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1052c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1053d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1054d325daa506338ab86f9dd468b48fd010673f49a6John McCall
105568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
105668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
105768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
105868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
105968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1060c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1061d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1062c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1063d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1064d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1065d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1066a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
10677c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1068d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
106968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
107002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1071ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1072ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                           D->getLocation(), D->getDeclName(), T, TInfo,
107316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
1074af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
10759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                           /*isConstexpr*/ false);
1076b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1077c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1078c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1079b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1080b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1081b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1082b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1083b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1084b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1085b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1086b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
10871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1088e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
10895cbe101b502e06d16bc77df45a27ce8bc13f33c8Douglas Gregor  if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
10901d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Adopt the already-instantiated parameters into our own context.
10911d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (unsigned P = 0; P < Params.size(); ++P)
10921d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      if (Params[P])
10931d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        Params[P]->setOwningFunction(Function);
10941d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  } else {
10951d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Since we were instantiated via a typedef of a function type, create
10961d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // new parameters.
10971d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    const FunctionProtoType *Proto
10981d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      = Function->getType()->getAs<FunctionProtoType>();
10991d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    assert(Proto && "No function prototype in template instantiation?");
11001d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
11011d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor         AE = Proto->arg_type_end(); AI != AE; ++AI) {
11021d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      ParmVarDecl *Param
11031d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
11041d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor                                             *AI);
11051d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Param->setScopeInfo(0, Params.size());
11061d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Params.push_back(Param);
11071d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    }
11081d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  }
11094278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
111002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1111ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1112a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1113a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1114a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1115a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1116a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1117a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1118a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1119a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1120a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1121a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1122a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1123a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1124a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1125a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1126a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1127d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1128a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1129a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1130a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1131a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1132b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1133b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1134d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1135d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1136d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1137d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1138d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1139d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1140d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
114166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
114266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1143a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
114424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1145838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1146910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
114724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
114824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
114966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                                InsertPos);
115080f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
115180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
115280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
115380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
115480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
115580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
115680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1157d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
115802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1159a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1160e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1161e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
11621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1163e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  bool Redeclaration = false;
1164af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1165a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
11666826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
11676826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
11686826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1169af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1170af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1171af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1172af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1173af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1174af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1175af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1176af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1177af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1178af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1179e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1180e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1181e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1182af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1183af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1184af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1185af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1186af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1187af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1188af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1189af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1190af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1191af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1192af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1193af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1194af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1195af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1196af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1197a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1198af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1199af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1200af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1201a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1202a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1203a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
12046826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1205a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1206a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1207a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1208a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1209a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
12106826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
12116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1212a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1213a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
12149f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
1215c80e8115278ba1537c8b517a083ecbd0a018b579Peter Collingbourne                                   isExplicitSpecialization, Redeclaration);
1216e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
121776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
121876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
121976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
122076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1221a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1222a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1223d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
12246826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
122576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1226a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = FunctionTemplate->getPreviousDeclaration();
122776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1228a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor      PrevDecl = Function->getPreviousDeclaration();
122976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
123076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
123176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
1232ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
123377535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1234ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1235238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    if (!SemaRef.getLangOptions().CPlusPlus0x &&
1236238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1237238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1238238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
123910620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1240238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1241a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1242238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          << Function->getDeclName();
1243238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1244a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Function->setInvalidDecl();
1245a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1246238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1247238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1248238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1249238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1250238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
125113a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
125213a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1253ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1254ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
1255ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          if (!queuedInstantiation && R->isUsed(false)) {
1256ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1257ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1258ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1259ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1260ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1261ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1262ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1263ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1264ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1265ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1266ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1267ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1268ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1269238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
12706a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
127110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
1272a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1273238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
12746a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1275238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              Function->setInvalidDecl();
1276238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1277238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1278238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1279238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1280238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1281a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1282a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
128376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
128476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
128576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
128676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1287eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1288e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1289e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
12902dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1291d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1292d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1293af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1294af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
12956b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
12966b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  void *InsertPos = 0;
1297d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1300d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1301a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
130224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13042c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
13052c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
13062c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13086b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
13092c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
13102c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
13116b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
13126b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1313b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1314b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1315b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1316b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1317b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1318b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
131979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1320a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
132179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
13222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
132348dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
13244eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
13255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
13264eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
13274eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
13284eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
13294eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
13304eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
13314eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
13324eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
13334eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
13344eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
13354eab39f0745fb1949dbb40c4145771b927888242John McCall    }
13364eab39f0745fb1949dbb40c4145771b927888242John McCall  }
13374eab39f0745fb1949dbb40c4145771b927888242John McCall
13385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
133921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *TInfo = D->getTypeSourceInfo();
134021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TInfo = SubstFunctionType(D, Params);
134121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
13422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
134321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  QualType T = TInfo->getType();
13442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1345723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1346723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1347723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
13485f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
13495f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
13505f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
13515f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
13525f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
13535f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1354723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
13555f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
13565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> ParamTypes;
1357a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1358a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                               D->getNumParams(), TemplateArgs, ParamTypes,
135912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
1360a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      return 0;
13615f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
13625f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1363c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1364c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1365c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1366b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1367a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1368c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1369b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1370b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1371b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1372b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1373c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1374b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1375c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1376b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1377c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1378c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1379c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1380b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1381b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1382b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1383b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1384b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1385b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1386b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1387b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
13882dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1389b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1390dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1392ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
13932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
13942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
139517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
13961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1397ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
139916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
14009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                        false, /*isConstexpr*/ false);
140117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
140217e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1403ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
140516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
140665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
140765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1408ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
14090130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1410f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
14119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       /*isConstexpr*/ false,
14129f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1413dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
14142577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1415ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
141616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
141716573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
1418f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->isInlineSpecified(),
14199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                   /*isConstexpr*/ false, D->getLocEnd());
1420dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14216b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1422c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1423c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1424b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1425d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1426d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1427d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1429d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1430d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1431d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1432d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1433d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1434d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1435d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1436d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1437d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1438d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1439d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1440d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1442d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1443b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1444b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1445b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1446b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1448d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
144966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
145066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1451a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
145224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1453838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1454910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1455910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1456910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
145766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor                                              InsertPos);
1458b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
145966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
14602db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
146166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1462a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
14647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
14657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1466b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
14674eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
14684eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
14694eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
14704eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
14714eab39f0745fb1949dbb40c4145771b927888242John McCall
1472b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1473b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1474b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
14757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
14785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
14795545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
14804278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
14815545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
14825545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
14835545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
14842dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
14852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
14862577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1488b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1489b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1491dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1492dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1493dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1494dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
14956826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
14966826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1497dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
14982dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
149965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  bool Redeclaration = false;
1500af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
1501af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration);
150265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
15034ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
15044ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
15054ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
150646460a68f6508775e98c19b4bb8454bb471aac24John McCall  Method->setAccess(D->getAccess());
150746460a68f6508775e98c19b4bb8454bb471aac24John McCall
15089eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
15099eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
1510b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
1511b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // If there's a function template, let our caller handle it.
1512b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
1513b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    // Don't hide a (potentially) valid declaration with an invalid one.
1514b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else {
1515b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    NamedDecl *DeclToAdd = (TemplateParams
1516b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            ? cast<NamedDecl>(FunctionTemplate)
1517b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                            : Method);
1518b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend)
1519b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Record->makeDeclVisibleInContext(DeclToAdd);
1520af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    else if (!IsClassScopeSpecialization)
1521b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      Owner->addDecl(DeclToAdd);
1522b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1523eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
1524eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  if (D->isExplicitlyDefaulted()) {
1525eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
1526eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  } else {
1527eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt    assert(!D->isDefaulted() &&
1528eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt           "should not implicitly default uninstantiated function");
1529eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  }
1530eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
15312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
15322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
15332dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1534615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1535dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1536615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1537615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
153803b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
153917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
154003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
154103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1542bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
154365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1544bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1545bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
15466477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1547fb44de956f27875def889482b5393475060392afJohn McCall  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0,
1548fb44de956f27875def889482b5393475060392afJohn McCall                                  llvm::Optional<unsigned>());
15492dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
15502dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1551e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1552e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1553e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
15544fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1556e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1557344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1558344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
15594fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
15604fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1561e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1562e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
15639a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1564a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
15650f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1566a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1567e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1568a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1569550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1570550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1571a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1572e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1573e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1574e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
157533642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
157633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
157733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
15786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
15795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
15805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
15816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1582a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
158333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
158433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
15856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
15866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1587a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
15886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
15896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
15906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
15916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
15926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
15936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1594a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
15956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
15966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
15976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1598a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
15996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
16006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
16016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
16026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
16036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
16046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
16056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1606a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
16086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
16096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
16106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else if (isa<PackExpansionTypeLoc>(TL)) {
16116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
16126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
16136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
16146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL);
16156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
16165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
16176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1618a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
16206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
16216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
16226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
16236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> OrigNumExpansions
16246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
16256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    llvm::Optional<unsigned> NumExpansions = OrigNumExpansions;
16266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
16276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1628a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
16296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1630a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
16316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
16326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1633a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
16356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
16366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
16376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1638a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
16396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
16406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
16416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1642a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
16446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
16456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
16466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
16476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
16486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
16496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
16506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1651a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
16536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
16546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
16556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
16566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
16576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
16586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
16596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
16606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
16616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
16626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1663a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
16646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
16656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
16666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1667a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
16696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
16706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
16716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1672a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
16746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
16756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
16766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1677a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
16786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
16796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
16806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1681a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1683a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
16846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
16856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
16866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
16876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
16886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
168933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1690a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
16926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1693a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1694ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1695ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1696a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1697a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
16986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
16996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
17006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
17016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
17026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
17036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1704a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1705ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
17066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1707a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1708a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1709a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
17106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1711a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17129a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
171333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
171433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1715a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1716d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1717a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1718a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1719550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1720550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
172133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
172233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
172333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
17240dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
17259106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
17269106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
17279106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
17289106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
17299106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
17309106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  {
17319106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
17329106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
17332a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
17349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
17359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
17369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor      return NULL;
1737a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
1738a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
17409106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateTemplateParmDecl *Param
17419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1742a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
1743a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                       D->getPosition(), D->isParameterPack(),
174461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       D->getIdentifier(), InstParams);
1745d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
17469a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
1747a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1748a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
17499106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
17509106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1751a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17529106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
17539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
17549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
175548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1756db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
1757db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
1758a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
175948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
176048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1761a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
1762db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
1763a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
1764a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
176548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
176648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  Owner->addDecl(Inst);
176748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
176848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
176948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
1770ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
17711b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
17721b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
17731b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
17741b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
17751b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
17761b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
17771b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
17781b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
17791b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
17805149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
17815149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
17825149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
17835149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
1784dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
17851b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
17861b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
17871b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
1788ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
1789ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
17909f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
17919f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
17929f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
17939f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
17949f54ad4381370c6b771424b53d219e661d6d6706John McCall
1795ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1796ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
17979f54ad4381370c6b771424b53d219e661d6d6706John McCall
1798ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1799ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
18005149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
1801ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
1802ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
1803ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18045149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
18055149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18069f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
18079f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
18089f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
18099f54ad4381370c6b771424b53d219e661d6d6706John McCall
18109f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
18119f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
18129f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
18139f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
18149f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
18159f54ad4381370c6b771424b53d219e661d6d6706John McCall
18169f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
18179f54ad4381370c6b771424b53d219e661d6d6706John McCall
18189f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
18199f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
1820ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
1821ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
18229f54ad4381370c6b771424b53d219e661d6d6706John McCall
1823ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1824ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
1825ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
1826ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18279f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
18289f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
18299f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
18309f54ad4381370c6b771424b53d219e661d6d6706John McCall
1831323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
1832323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
18339f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
18349f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
18359f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
18369f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
18379f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
1838b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
1839b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
1840b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
1841b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
1842b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
1843b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
18449f54ad4381370c6b771424b53d219e661d6d6706John McCall
18459f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
18469f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
18479f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
18489f54ad4381370c6b771424b53d219e661d6d6706John McCall
18499f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
18509f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
18519f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1852323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
1853323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
1854323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
18559f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
1856ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1857ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
1858ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1859ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1860ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
18619f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
18629f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
1863ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1864ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
18667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
18675149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
1868a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
18695149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
18705149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
18717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
18727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
18737ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
18745149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
1876ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
1877ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Hence, no tranformation is required for it.
1878ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
18797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
18807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1881ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
18827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
18837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
18844469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1885ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1886ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
18877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
18887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
18897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
18907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
18917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
18925149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
18935149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
18945149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
18950dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
1896a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18970dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
18985149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1900ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
1901ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1902ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
19031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
19049488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1905ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
19067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
19077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
19084469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
1909ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1910ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
19110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
19120dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
19130dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
1914af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1915af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
1916af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
1917af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
1918af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, 0, true));
1919af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1920af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
1921af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
1922af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1923af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
1924af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, 0, Previous)) {
1925af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
1926af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
1927af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
1928af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1929af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
1930af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
1931af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
1932af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
1933af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1934af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
1935af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1936af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1937ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
1938d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
19397e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
19402fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
19412fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
19422fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
19438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
19448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
19458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
1946e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
1947e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
1948e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1949e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
1950e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
1951e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
1952e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
1953ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
1954e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
1955e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
1956e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1957e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
19585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
1959e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
1960e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
1961e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1962e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
1963bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
1964e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
19659148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
1966e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
1967e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1968e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
1969ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
1970e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
1971e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1972e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
1973e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1974e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
1975e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
1976e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
1978e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1979a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
1980ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
1981ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1982ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
1983ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
1984ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1985a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
1986ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
1987ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
1988d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
1989d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
1990d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
1991ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1992ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
1993ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
1994550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
1995550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
1996550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
19972a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1998a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1999ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2000ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2001ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2002ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2003ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2004d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2005a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2006ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2007ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2008d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
2009a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2010a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                    PartialSpec->getNumTemplateArgsAsWritten(),
2011e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2012e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2013a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2014ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2015ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
20165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2017a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2018ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2019a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2020ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2021ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2022d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2023ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2024ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2025ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2026ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2027ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2028910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2029910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2030a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2031ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2032ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2033a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2034ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2035910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2036910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2038ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2039ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2040ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2041ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2042ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2043ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2044ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
20453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
20463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
20473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
20483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2049d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2050ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2051a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2052ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2053ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2054ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2055ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2056ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2057ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2058ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2059ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2060ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2061ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2062ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2063ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2064ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2065ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2066ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2067ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2068ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2069d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2070ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2071ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2072d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2073ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2074a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2076ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2077ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2078a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
207913c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2080a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2081ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2082ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2083ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2084a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2085910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2086910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2087d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
20883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2089dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2090cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2091b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2092b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2093b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2094b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2095ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
20964469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2097a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2099ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
2100cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis  ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
2101d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2102ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2103ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
210421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
210521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
21065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
210721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
210821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
210921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
21106cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
21116cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
21126cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
21136cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getDeclName());
211421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
211521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
21165545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2117cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2118cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2119140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
21206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
21216920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                  = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
2122140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
21236920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
21246920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      assert(NewProtoLoc && "Missing prototype?");
212512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs();
212612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs();
212712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
212812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx);
212912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        if (!OldParam->isParameterPack() ||
213012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor            (NewIdx < NumNewParams &&
213112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor             NewProtoLoc->getArg(NewIdx)->isParameterPack())) {
2132a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
213312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
213412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
213512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
213612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam,
213712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                               NewParam);
213812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          continue;
213912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
2140a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
214112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        // Parameter pack: make the instantiation an argument pack.
214212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(
214312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      OldParam);
214421371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        unsigned NumArgumentsInExpansion
214521371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor          = SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
214621371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor                                               TemplateArgs);
214721371ea7cf647f4f0f783faac325925cb8febb1cDouglas Gregor        while (NumArgumentsInExpansion--) {
214812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++);
214912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
215012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam,
215112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                                                                      NewParam);
215212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
21536920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2154895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2155cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2156cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2157cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2158cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2159140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
21606920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor    if (FunctionProtoTypeLoc *OldProtoLoc
21616920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor                                    = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
21626920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
21636920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
21646920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
21656920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
21666920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
21676920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2168cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2169cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
217021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
21715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
21725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
21731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2174e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2175e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2176e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
21771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
21781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2179e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
218010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Tmpl->isDeletedAsWritten())
218110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
21821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2183cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2184cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2185cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
21861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
21871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2188cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2189cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2190cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2191cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2192cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2193cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2194cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
2196cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor          = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
21971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2198cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2199bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2200cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
2201cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
2202f35f828f9883123772a9731af190a608f3236ef4Douglas Gregor      --SemaRef.NonInstantiationEntries;
2203cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2204cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
22051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22060ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
22070ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
22080ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
220960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
22100ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // The function has an exception specification or a "noreturn"
22110ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    // attribute. Substitute into each of the exception types.
22125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> Exceptions;
22130ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
22140ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      // FIXME: Poor location information!
2215b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      if (const PackExpansionType *PackExpansion
2216b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2217b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // We have a pack expansion. Instantiate it.
22185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2219b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2220b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                Unexpanded);
2221a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        assert(!Unexpanded.empty() &&
2222b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor               "Pack expansion without parameter packs?");
222360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2224b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Expand = false;
2225d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        bool RetainExpansion = false;
2226cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        llvm::Optional<unsigned> NumExpansions
2227cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                          = PackExpansion->getNumExpansions();
2228a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2229b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    SourceRange(),
2230a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                    Unexpanded,
2231b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                                    TemplateArgs,
2232a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                    Expand,
2233d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    RetainExpansion,
2234d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                    NumExpansions))
2235b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
223660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2237b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (!Expand) {
2238b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          // We can't expand this pack expansion into separate arguments yet;
2239a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // just substitute into the pattern and create a new pack expansion
2240cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          // type.
2241b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2242a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2243b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2244b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2245b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull())
2246b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2247a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2248cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor          T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2249b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2250b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          continue;
2251b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
225260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2253b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        // Substitute into the pack expansion pattern for each template
2254b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        bool Invalid = false;
2255cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor        for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2256b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2257a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2258a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2259b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                         TemplateArgs,
2260b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor                                       New->getLocation(), New->getDeclName());
2261b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          if (T.isNull()) {
2262b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            Invalid = true;
2263b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor            break;
2264b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          }
226560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2266b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          Exceptions.push_back(T);
2267b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        }
226860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2269b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        if (Invalid)
2270b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor          break;
227160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
2272b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor        continue;
2273b99268b3083c882103bd1bd08bdcc9a76a2b4795Douglas Gregor      }
2274a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
22750ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      QualType T
22760ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
22770ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                            New->getLocation(), New->getDeclName());
2278a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (T.isNull() ||
22790ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor          SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
22800ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor        continue;
22810ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
22820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      Exceptions.push_back(T);
22830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    }
228456fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    Expr *NoexceptExpr = 0;
228556fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
22863617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor      EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated);
228756fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
228856fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl      if (E.isUsable())
22895c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
22905c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor
22915c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor      if (E.isUsable()) {
22925c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        SourceLocation ErrLoc;
22935c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        llvm::APSInt NoexceptVal;
229456fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl        NoexceptExpr = E.take();
22955c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        if (!NoexceptExpr->isTypeDependent() &&
22965c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            !NoexceptExpr->isValueDependent() &&
22975c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            !NoexceptExpr->isIntegerConstantExpr(NoexceptVal, SemaRef.Context,
22985c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor                                                 &ErrLoc, /*evaluated=*/false)){
22995c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor          SemaRef.Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
23005c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor            << NoexceptExpr->getSourceRange();
23015c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor          NoexceptExpr = 0;
23025c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor        }
23035c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor      }
230456fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    }
23050ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2306a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Rebuild the function type
23070ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
2308e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
230960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2310e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.NumExceptions = Exceptions.size();
2311e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.Exceptions = Exceptions.data();
231256fb926c93786739cfd0867e7ada31ace3bda946Sebastian Redl    EPI.NoexceptExpr = NoexceptExpr;
2313e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = Proto->getExtInfo();
2314e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
23150ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    const FunctionProtoType *NewProto
23160ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor      = New->getType()->getAs<FunctionProtoType>();
23170ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    assert(NewProto && "Template instantiation without function prototype?");
23180ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor    New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
23190ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->arg_type_begin(),
23200ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor                                                 NewProto->getNumArgs(),
2321e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                                 EPI));
23220ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
23230ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
23249f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // C++0x [dcl.constexpr]p6: If the instantiated template specialization of
23259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // a constexpr function template satisfies the requirements for a constexpr
23269f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  // function, then it is a constexpr function.
23279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith  if (Tmpl->isConstexpr() &&
23289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith      SemaRef.CheckConstexprFunctionDecl(New, Sema::CCK_Instantiation))
23299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith    New->setConstexpr(true);
23309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith
233119f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  const FunctionDecl* Definition = Tmpl;
233219f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
233319f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
233419f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
233519f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
233619f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New);
23377cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2338e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2339e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2340e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
23415545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
23425545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
23435545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
23445545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
23455545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
23461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
23485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2349e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2350e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2353e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
235485606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
23555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
23565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
23575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
23585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
23595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2360a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2361a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2362a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2363a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2364b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2365b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2366b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2367b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2368a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2369b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2370b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2371b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2372b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2373b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2374e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2375e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2376e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2377e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2378f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2379b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2380e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2381e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
238210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
238354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
238454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2385af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
2386af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
2387af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2388af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
2389251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
23906cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
23911eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
23923b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2393f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
2394f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
2395f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2396f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
2397f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
2398f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
2399f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
2400dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
2401f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
24021eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
24038387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
2404f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
24058a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
24068387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
24078387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
24088387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
24098387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
24108387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
24118387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
2412a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
2413f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
24148387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
24154a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
24168387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
24178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
24188387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2419f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
2420e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2421e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2422a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2423e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2424e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2425e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2426a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2427e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2428e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2429a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2430e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2431a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
2432e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2433cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
243458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
243558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
243662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
243758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2438e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
243958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
24401eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2441e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
24421eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2443d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2444d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
24451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2446d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
24471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2448d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
24497ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2450d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
24511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2452f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2453f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2454a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
2455a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2456b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2457b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2458b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
24595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
246062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
24612a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
24622a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
246362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
24642a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2466a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
2467f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2468d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnStartOfFunctionDef(0, Function);
2469e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
247054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
247160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
247260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
247360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
247460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
247560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
247660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
247760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
247860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
24791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce the instantiated function parameters into the local
24818a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // instantiation scope, and set the parameter names to those used
24828a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  // in the template.
248312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor  unsigned FParamIdx = 0;
24848a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
24858a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
248612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    if (!PatternParam->isParameterPack()) {
248712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      // Simple case: not a parameter pack.
248812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      assert(FParamIdx < Function->getNumParams());
248912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(I);
249012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
249112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocal(PatternParam, FunctionParam);
249212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ++FParamIdx;
249312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      continue;
249412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
2495a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
249612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    // Expand the parameter pack.
249712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2498a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    for (unsigned NumFParams = Function->getNumParams();
2499a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi         FParamIdx < NumFParams;
250012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor         ++FParamIdx) {
250112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
250212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      FunctionParam->setDeclName(PatternParam->getDeclName());
250312c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
250412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor    }
25058a6c0f1a48b8167ca1457c9f05288fa637033dc9Peter Collingbourne  }
250654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2507b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Enter the scope of this instantiation. We don't use
2508b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // PushDeclContext because we don't have a scope.
2509eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  Sema::ContextRAII savedContext(*this, Function);
2510b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
25111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  MultiLevelTemplateArgumentList TemplateArgs =
2512e7089b0c6ffe8a8854150b60df00fb544099f77dDouglas Gregor    getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
2513090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2514cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  if (PatternDecl->isDefaulted()) {
2515cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, 0, /*IsInstantiation=*/true);
25161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2517cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    SetDeclDefaulted(Function, PatternDecl->getLocation());
2518cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  } else {
2519cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
2520cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
2521cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2522cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2523cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
2524cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
2525cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2526cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
2527cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2528cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2529cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
2530cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
2531a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2532cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
2533cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
2534cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt  }
2535b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
25360c01d18094100db92d38daa923c95661512db203John McCall  PerformDependentDiagnostics(PatternDecl, TemplateArgs);
25370c01d18094100db92d38daa923c95661512db203John McCall
2538eee1d5434ebfa955ffc3c493aecd68bb7b3f4838John McCall  savedContext.pop();
2539aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2540aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2541aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
25421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
254460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
254562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
254660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
254760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2548b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
25492a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
25502a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
25512a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2552b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
255462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
25551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25562a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
25578155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
25588155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
25592a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
25602a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2561b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
25628155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
25638155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
256462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2565b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2566a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2567a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2568a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2569a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2570a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
25717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
25727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
25737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
25747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
25757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
25767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
25777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
25787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
25797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2580e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2581e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2582e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2583e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
25847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
25857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
25867caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2587e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2588e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
25897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
25907caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
25911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
25937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
25947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
2595a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(Def->isStaticDataMember() && "Not a static data member?");
25960d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
25971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25980d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
25997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
26007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
26011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
26021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2603e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
26040d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2605a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      Diag(PointOfInstantiation,
2606e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2607e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2608e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
260958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
261058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
261162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
261258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
261358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
261458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
26157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
26167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
26177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2618251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
26191028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2620251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2621a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2622251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2623251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2624251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2625251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
2626a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (Var->getTemplateSpecializationKind()
2627251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor        == TSK_ExplicitInstantiationDeclaration)
2628251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
26291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2630f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  // If we already have a definition, we're done.
2631f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  if (Var->getDefinition())
2632f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    return;
2633f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
26347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
26357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
26367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
26371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
26397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
26407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
26415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
264262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
26438155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
26448155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
264562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
26468155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
26471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
26497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
2650f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
26511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26521028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
2653ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
26546bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
2655f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
2656f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
26577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
26587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
2659583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2660583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
2661583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2662583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
26637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    DeclGroupRef DG(Var);
26647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Consumer.HandleTopLevelDecl(DG);
26657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
26661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
26688155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
26698155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
26708155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
26717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
26721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
267362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
26741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26758155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
26768155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
26778155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded, "
26788155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
26798155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
26808155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
26817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
26828155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
26838155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded, "
26848155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
268562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
26861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2687a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2688815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
268954dec5f9ad13597bbebb6872ab938e2ad4b6aa1bBenjamin Kramerstatic MultiInitializer CreateMultiInitializer(SmallVectorImpl<Expr*> &Args,
269054dec5f9ad13597bbebb6872ab938e2ad4b6aa1bBenjamin Kramer                                               const CXXCtorInitializer *Init) {
26916df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // FIXME: This is a hack that will do slightly the wrong thing for an
26926df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // initializer of the form foo({...}).
26936df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // The right thing to do would be to modify InstantiateInitializer to create
26946df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  // the MultiInitializer.
26956df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl  if (Args.size() == 1 && isa<InitListExpr>(Args[0]))
26966df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl    return MultiInitializer(Args[0]);
269754dec5f9ad13597bbebb6872ab938e2ad4b6aa1bBenjamin Kramer  return MultiInitializer(Init->getLParenLoc(), Args.data(),
26986df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                          Args.size(), Init->getRParenLoc());
26996df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl}
27006df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
2701090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
2702090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
2703090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
2704090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
270690ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
27079db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor  bool AnyErrors = false;
2708a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2709090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
2710090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
271172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
271272f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
2713cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
2714090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
2715030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
2716030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
2717030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
2718030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
2719030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
27206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    SourceLocation LParenLoc, RParenLoc;
2721ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall    ASTOwningVector<Expr*> NewArgs(*this);
27221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
2724a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
27253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
27263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
27273fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc();
27285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
27293fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
27303fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
2731d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
2732cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      llvm::Optional<unsigned> NumExpansions;
2733a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
27343fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
2735a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
2736a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
2737d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
27383fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
27393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
27403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
27413fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
27423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
27433fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
2744a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2745a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
2746cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
27473fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
27483fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
27493fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
2750a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        if (InstantiateInitializer(Init->getInit(), TemplateArgs,
27513fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                   LParenLoc, NewArgs, RParenLoc)) {
27523fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
27533fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
27543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
27553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
27563fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
2757a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2758a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
2759a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
27603fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
27613fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
27623fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
27633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
27643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
27653fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
27663fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
27676df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
27686df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
27696df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                                     BaseTInfo, MultiInit,
27703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
27713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
27723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
27733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
27743fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
27753fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
2776a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
27773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
27783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewArgs.clear();
27793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
2780a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
27813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
27823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
27833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
27846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
2785a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (InstantiateInitializer(Init->getInit(), TemplateArgs,
2786a1a04786cea2445759026edacd096abd1fbf4a05Douglas Gregor                               LParenLoc, NewArgs, RParenLoc)) {
27876b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
27886b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
2789090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2790a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2791090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
2792090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    if (Init->isBaseInitializer()) {
2793a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
2794a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            TemplateArgs,
2795a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            Init->getSourceLocation(),
2796802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor                                            New->getDeclName());
2797a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      if (!BaseTInfo) {
27989db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
2799802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
2800802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
2801802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
28026df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
28036df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28046df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, MultiInit,
28056df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                     New->getParent(), EllipsisLoc);
2806090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
2807b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
280800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
280900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
281000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
2811b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
2812b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2813b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
2814b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
2815b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28176df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28186df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      NewInit = BuildMemberInitializer(Member, MultiInit,
28196df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
282000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
282100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
2822b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
282300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
282400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
282500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
2826b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
2827b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
2828b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
28296df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
2830b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
28316df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
28326df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      MultiInitializer MultiInit(CreateMultiInitializer(NewArgs, Init));
28336df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, MultiInit,
28346df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
2835090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2836090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
28379db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
28389db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
2839090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
28409db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
2841090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      // FIXME: It would be nice if ASTOwningVector had a release function.
2842090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      NewArgs.take();
28431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
284490ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
2845090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
2846090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
28471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2848090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
2849d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
2850090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
2851090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
28529db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       NewInits.data(), NewInits.size(),
28539db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
2854090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
2855090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
285652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
285752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
285852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
285952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
286052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
286152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
286252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
286352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
286452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
286552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
286652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
286752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
286852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
286952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
287052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
28710d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
28720d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
28730d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
2874a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
28750d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
28760d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
28770d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
28780d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
28790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
2880a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
28810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
28820d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
28830d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
2884a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
2885ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2886ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
2887a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
2888ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2889ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
2890ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
2891ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
2892ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
2893ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
2894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
2895ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
2896a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2897ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
2898ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2899ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
290052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
290152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
290252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
290352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
290452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
290552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
290652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
290752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
290852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
290952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
291052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
291152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
291252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
291352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
291452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
291552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
291652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
291752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
291852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
291952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
292052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
292152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
292252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
292352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
292452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
292552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
292652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
292752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
292852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
292952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
293052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
293152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
293252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
293352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
293452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
293552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
293652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
293752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
293852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2939ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
2940ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
2941ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2942ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2943ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2944ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2945ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
2946ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
2947ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
2948ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2949ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2950ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
29517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
29527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
29537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
2954ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
29557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
29567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
29577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
29580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
29590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
2960ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
29610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
29620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
296352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
296452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
296552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
296652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
296752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
296852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
296952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
297052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
297152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
297252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
297352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
297452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
297552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
297652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
297752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
2978ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
2979ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
2980815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
29810d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
29827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
29837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
29847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
29857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
29867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
29877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
29887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
29897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
29907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
29910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
29920d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
29930d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
29940d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
2995815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
29960d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
29970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
29981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
299952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
300052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
30011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
300252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
300352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3004815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
300552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
300652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3007815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
30087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
300952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
301052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
301152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
301252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
301352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3014a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
30150d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
30160d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
30170d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3018ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3019ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3020ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3021ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3022ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3023d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3024d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3025d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
30261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3027d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3028d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3029d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
30301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3031ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3032ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3033ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3034ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3035ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3036ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3037815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3038815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3039815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3040815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3041815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
30421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3043815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3044815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3045815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3046815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3047815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3048815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3049815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3050815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3051815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3052815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
305302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
305402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
305502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
305602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
30577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
3058e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
305902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
30607c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
306102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
306202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
306302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
306402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
3065ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
3066ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
3067815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3068815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
3069815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
3070815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
3071815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
3072815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3073815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
3074815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
3075815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
3076815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
3077815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
3078815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
3079815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3080815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
3081815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
3082815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3083815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
3084815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
3085815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
3087815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
3088815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// X<T>::<Kind>::KnownValue) to its instantiation
3089ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
3090ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
30917c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
3092e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
3093815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
3094550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
30956d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
3096766724566289e6951a90b6483f0d3e22fe4b9b52John McCall      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
30972bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
30982bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
3099d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3100d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3101d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
3102a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
310357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
310457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
310557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
3106a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
310757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      unsigned PackIdx = ArgumentPackSubstitutionIndex;
310857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
310957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
311057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
311157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
311257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
311357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
3114a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
311557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
311657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
3117a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
311857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
311957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
31202bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
3121815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3122e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3123e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
3124e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
3125a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31268b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // If the RecordDecl is actually the injected-class-name or a
31278b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // "templated" declaration for a class template, class template
31288b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // partial specialization, or a member class of a class template,
31298b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // substitute into the injected-class-name of the class template
31308b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor    // or partial specialization to find the new DeclContext.
3131e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    QualType T;
3132e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
3133a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3134e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (ClassTemplate) {
313524bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      T = ClassTemplate->getInjectedClassNameSpecialization();
3136e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
3137e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
3138e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate();
31393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
31403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // If we call SubstType with an InjectedClassNameType here we
31413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // can end up in an infinite loop.
31423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      T = Context.getTypeDeclType(Record);
31433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<InjectedClassNameType>(T) &&
31443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "type of partial specialization is not an InjectedClassNameType");
314531f17ecbef57b5679c017c375db330546b7b5145John McCall      T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
3146a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    }
3147a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3148e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!T.isNull()) {
31498b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // Substitute into the injected-class-name to get the type
31508b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // corresponding to the instantiation we want, which may also be
31518b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the current instantiation (if we're in a template
31528b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition). This substitution should never fail, since we
31538b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // know we can instantiate the injected-class-name or we
3154a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // wouldn't have gotten to the injected-class-name!
31558b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
31568b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // FIXME: Can we use the CurrentInstantiationScope to avoid this
31578b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // extra instantiation in the common case?
3158b46ae3964ba02535276c71332396e9a7bad2dfa5Douglas Gregor      T = SubstType(T, TemplateArgs, Loc, DeclarationName());
3159e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
3160a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3161e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      if (!T->isDependentType()) {
3162e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        assert(T->isRecordType() && "Instantiation must produce a record type");
3163e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor        return T->getAs<RecordType>()->getDecl();
3164e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
3165a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31668b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We are performing "partial" template instantiation to create
31678b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // the member declarations for the members of a class template
31688b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // specialization. Therefore, D is actually referring to something
31698b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // in the current instantiation. Look through the current
31708b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // context, which contains actual instantiations, to find the
31718b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation of the "current instantiation" that D refers
31728b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // to.
31738b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      bool SawNonDependentContext = false;
31741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (DeclContext *DC = CurContext; !DC->isFileContext();
317552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall           DC = DC->getParent()) {
31761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (ClassTemplateSpecializationDecl *Spec
31778b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor                          = dyn_cast<ClassTemplateSpecializationDecl>(DC))
3178a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          if (isInstantiationOf(ClassTemplate,
3179e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                                Spec->getSpecializedTemplate()))
318052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall            return Spec;
31818b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
31828b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor        if (!DC->isDependentContext())
31838b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor          SawNonDependentContext = true;
318452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
318552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
31868b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // We're performing "instantiation" of a member of the current
31878b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // instantiation while we are type-checking the
31888b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      // definition. Compute the declaration context and return that.
3189a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      assert(!SawNonDependentContext &&
31908b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor             "No dependent context while instantiating record");
31918b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      DeclContext *DC = computeDeclContext(T);
3192a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      assert(DC &&
319352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall             "Unable to find declaration for the current instantiation");
31948b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor      return cast<CXXRecordDecl>(DC);
319552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
31968b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3197e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3198e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3199e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
320052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3201e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3202e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3203a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32047c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
32051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
320644c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
32071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3208815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3209815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3210815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3211815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
32127c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
32133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
32143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3215eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
32163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
32177c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
32187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
32193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
32203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3221eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3222eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
32233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
32243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
32253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3226a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3227a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
32287c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
32297c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
32307c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3231815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3232815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
323317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
3234815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      Result = findInstantiationOf(Context, D, Found.first, Found.second);
3235815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3236815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3237815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3238815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3239815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3240815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3241815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3242815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3243815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
32441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
324517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
324617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3247815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
32481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3249eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3250eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3251eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3252eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3253eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3254eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3255eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3256eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3257a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
3258eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3259eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3260eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3261eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3262eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3263eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3264eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3265eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
3266eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3267eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3268eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3269eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3270eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3271a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3272815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3273815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3274815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3275815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3276815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3277d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
32781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3279d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
32808155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
32816e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
32826e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
32836e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    SmallVector<std::pair<ValueDecl *, SourceLocation>, 4> Pending;
32846e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
32856e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
32866e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
32876e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
3288a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
328960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
329062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
329160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
329260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
329360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
329462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
329562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
329660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
329760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
329860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
329960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
33001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
33027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3303f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3304f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
330558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
330658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
330758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
330858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
33097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
33107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
33111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
33137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
33147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3315c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3316291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3317291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3318291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    if (Var->getMostRecentDeclaration()->isInvalidDecl())
3319291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3320291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3321291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3322291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3323291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
3324291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3325b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
3326291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3327291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
332858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
332958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
333058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
333158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
333258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      if (Var != Var->getMostRecentDeclaration()) continue;
3333291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3334291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3335291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3336291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3337f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3338f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3339f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
33401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
334158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
334258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
334358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
334458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3345d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3346d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
33470c01d18094100db92d38daa923c95661512db203John McCall
33480c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
33490c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
33500c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
33510c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
33520c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
33530c01d18094100db92d38daa923c95661512db203John McCall
33540c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
33550c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
33560c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
33570c01d18094100db92d38daa923c95661512db203John McCall      break;
33580c01d18094100db92d38daa923c95661512db203John McCall    }
33590c01d18094100db92d38daa923c95661512db203John McCall  }
33600c01d18094100db92d38daa923c95661512db203John McCall}
3361