SemaTemplateInstantiateDecl.cpp revision 29a46e63490176608efe13f13b293a6ce9862059
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"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
170c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
19a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Lookup.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/PrettyDeclStackTrace.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.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
607b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins// Include attribute instantiation code.
617b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins#include "clang/Sema/AttrTemplateInstantiate.inc"
627b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins
63f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
64f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
65f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) {
66f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr()) {
67f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    // The alignment expression is a constant expression.
68f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
69f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
70f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (!Result.isInvalid())
71f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
72f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
73f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
74f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(),
75f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         TemplateArgs, Aligned->getLocation(),
76f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         DeclarationName());
77f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Result)
78f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result,
79f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
80f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
81f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
82f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
83f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
84f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
85f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New) {
86f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Aligned->isPackExpansion()) {
87f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
88f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
89f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
90f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
91f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
92f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr())
93f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
94f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
95f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  else
96f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
97f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
98f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
99f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
100f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // Determine whether we can expand this attribute pack yet.
101f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  bool Expand = true, RetainExpansion = false;
102f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  Optional<unsigned> NumExpansions;
103f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // FIXME: Use the actual location of the ellipsis.
104f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SourceLocation EllipsisLoc = Aligned->getLocation();
105f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
106f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        Unexpanded, TemplateArgs, Expand,
107f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        RetainExpansion, NumExpansions))
108f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
109f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
110f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Expand) {
111f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
112f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true);
113f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
114f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    for (unsigned I = 0; I != *NumExpansions; ++I) {
115f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I);
116f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
117f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    }
118f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
119f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
120f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
1211d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
12223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            const Decl *Tmpl, Decl *New,
12323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LateInstantiatedAttrVec *LateAttrs,
12423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LocalInstantiationScope *OuterMostScope) {
125cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
126cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
127cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
12823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
1294ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
130f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
131f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Aligned && Aligned->isAlignmentDependent()) {
132f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
133f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      continue;
1344ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1354ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
136f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    assert(!TmplAttr->isPackExpansion());
13723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
13823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
13923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
14023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *Saved = 0;
14123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
14223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
14323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
14423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
145cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      // Allow 'this' within late-parsed attributes.
146cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      NamedDecl *ND = dyn_cast<NamedDecl>(New);
147cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXRecordDecl *ThisContext =
148cafeb948e6067b8dc897c441da522367917b06f9Richard Smith          dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
149cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
150cafeb948e6067b8dc897c441da522367917b06f9Richard Smith                                 ND && ND->isCXXInstanceMember());
151cafeb948e6067b8dc897c441da522367917b06f9Richard Smith
1525bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
1535bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
15431c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
15531c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
15623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
157d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
158d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
159d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1604f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1614f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
162b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1634f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1644f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1654f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
16657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
16757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
16857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
16957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
17057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
17157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
17257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
17357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1744f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
175b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1764f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1774f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1783dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1840cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1850cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1863dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1873dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1883dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
195a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
196561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
197836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
198ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
199ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
200ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
202a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
204b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
205b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // HACK: g++ has a bug where it gets the value kind of ?: wrong.
209b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // libstdc++ relies upon this bug in its implementation of common_type.
210b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // If we happen to be processing that implementation, fake up the g++ ?:
211b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // semantics. See LWG issue 2141 for more information on the bug.
212b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
213b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
214b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
215b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DT->isReferenceType() &&
216b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
217b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
218b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      D->getIdentifier() && D->getIdentifier()->isStr("type") &&
219b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
220b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    // Fold it to the (non-reference) type which g++ would have produced.
221b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    DI = SemaRef.Context.getTrivialTypeSourceInfo(
222b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DI->getType().getNonReferenceType());
223b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
225162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
226162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
227162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
228162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
229162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
230162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
231162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
235cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
236cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
237cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
238cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
239c61361b102fcb9be7b64cc493fb797ea551eb8e7Douglas Gregor    if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
240cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
24183972f128e9218c051692bf96361327a701aeb79John McCall      assert(!newTag->hasNameForLinkage());
242162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
243cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
244d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
245a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
246ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
2477c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2487c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
249b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
250b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
251a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2525df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
2535df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2545df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
2555df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
2565df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2575df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    Typedef->setPreviousDeclaration(InstPrevTypedef);
2585126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2595126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2601d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
261d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
26246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
267162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
271162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
272162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
273162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
293ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
2943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2953bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
2963bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
2973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
3013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
3023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
3033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
3043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
3063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
3083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
3093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
3103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
3123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
3143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
315a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
3173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
319162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
320162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo// FIXME: Revise for static member templates.
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
323ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VisitVarDecl(D, /*ForVarTemplate=*/false);
324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
326ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, bool ForVarTemplate) {
327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3289901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3299901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3309901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3319901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
335ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
336a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3370a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3380a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3390a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3400a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3413d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
343c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
344c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
345c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
346c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
347c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
348a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration.
350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, D->getInnerLocStart(),
3513d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                 DI->getType(), DI, D->getStorageClass());
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3549aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3569aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3579aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3589aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
362bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
363ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                     StartingScope, ForVarTemplate);
3653d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3663d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3673d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3686206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3696206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3706206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3716206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3726206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3736206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3746206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3756206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
378a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
379561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
380836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
38107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
38207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
38307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
384a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
38507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
38607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
39407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
3968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
397b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
398b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
3998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
405f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
406f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
407f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
410ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
415e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
41807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
41907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
424ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
425703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
428663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
429663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
430f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
431663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
434a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
435be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
436be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
437be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
438f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
439f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
441f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
442f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
443f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
444a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
4459901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4469901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4477a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4489901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
451f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
45246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
453f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
45876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCallDecl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
45976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  bool Invalid = false;
46076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
46176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
46276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (DI->getType()->isVariablyModifiedType()) {
46376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
46476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    << D->getName();
46576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Invalid = true;
46676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else if (DI->getType()->isInstantiationDependentType())  {
46776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
46876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           D->getLocation(), D->getDeclName());
46976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    if (!DI) {
47076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      DI = D->getTypeSourceInfo();
47176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
47276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    } else if (DI->getType()->isFunctionType()) {
47376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      // C++ [temp.arg.type]p3:
47476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   If a declaration acquires a function type through a type
47576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   dependent on a template-parameter and this causes a
47676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   declaration that does not use the syntactic form of a
47776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   function declarator to have function type, the program is
47876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   ill-formed.
47976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
48076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      << DI->getType();
48176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
48276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    }
48376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else {
48476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
48576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  }
48676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
48776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  MSPropertyDecl *Property = new (SemaRef.Context)
48876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      MSPropertyDecl(Owner, D->getLocation(),
48976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getDeclName(), DI->getType(), DI,
49076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getLocStart(),
49176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getGetterId(), D->getSetterId());
49276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
49376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
49476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           StartingScope);
49576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
49676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (Invalid)
49776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Property->setInvalidDecl();
49876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
49976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Property->setAccess(D->getAccess());
50076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Owner->addDecl(Property);
50176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  return Property;
50376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall}
50476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50587c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
50687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
50787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
50887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
50987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
51087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
51187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
512b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
513a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
514b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
515b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
516b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
518b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
519b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
52087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52140e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
52287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
52387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
52440e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
52587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
52687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
52987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
53087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
53187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
53287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
53387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
53502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
53606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
53732f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5384fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5394fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5404fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5414fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5424fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5434fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5454fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5464fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5474fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5484fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5494fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
55006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
551fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
552d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
5530216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
55406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
55506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
556a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
55706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5589a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
561a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
562a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
56406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
56532f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
566af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
567af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
568af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
569af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
570af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
57106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
574a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
57506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5765fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5779a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
57802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
57902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
580fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
581fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
585f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
586f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
587f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
590ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
594e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
5959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
596e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
597e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
598e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
5998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
60238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
60338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
60438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
60538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
60638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
60738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
60838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
60938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
61038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
611ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
61338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
614a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6151274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
616f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6171274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6191274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
621f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
622f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
623f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
625f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
626f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
627f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6281274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6301274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6311274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6321274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6345b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6355b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
636f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
63706c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
638b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
63917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
640f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6414ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
6424ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
6434ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
6444ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
6454ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
6464ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
6474ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6484ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
6494ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
6504ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
6514ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
6524ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
6534ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
6544ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
6558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
65696084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
65796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
658a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
659f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
660f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
661f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
662f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: There appears to be no wording for what happens for an enum defined
66338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // within a block scope, but we treat that much like a member template. Only
66438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // instantiate the definition when visiting the definition in that case, since
66538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // we will visit all redeclarations.
66638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (!Enum->isScoped() && Def &&
66738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith      (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
6684ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
669f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
670f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
671f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
672f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
673f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
674f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
675f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
676f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6771af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
6781af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
6791af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
6805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
683f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
684f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
6858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
68760d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
688ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
689f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
691f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
693ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
694ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
6958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
6978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
6988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
6998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
7008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
715581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7165b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7173b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
71817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
719d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
721a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
722f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
723f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
72496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
72596084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
726581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
72796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
731f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
732f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
733f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
7349ff2b421f352fe0a0769c0a2a75af922c147b878Dmitri Gribenko                        Enumerators,
735fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
7368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7386477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
739b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7406477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7416477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
74393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
74493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
745550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
746550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7472a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
748e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
749ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
751d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
75493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
75593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
758c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
759c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
760c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
761c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
762c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
763c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
76493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
76593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
769ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
77037574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
7713bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
7723bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
77337574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
77437574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
77537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
77637574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
77737574f55cd637340f651330f5cfda69742880d36Nick Lewycky
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
78293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
784c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
786c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
79393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
79493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
79593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
79793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
79893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
807c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8091eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
810c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
814c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
81593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
816c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
817c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
818c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
819c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
820c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
821c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
822c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
823c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
824a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
825c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
826c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
827c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
828c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
829c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
830c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
831c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
832c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
833c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
834c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
835c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
836c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
837c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
838c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
839c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
840c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
841c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
84293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
84393ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
84493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
84593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
84693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
847a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
848c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
849c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
850c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
851c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
852c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
853c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
854c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
85593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
85693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
85793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
858c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
859c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
86093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
86193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
86293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
86493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
865e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
86693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
867ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
868ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
869f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
870e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
871c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
872c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
873b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
874e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
87593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
87693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
87793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
878e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
879ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
88093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
881ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
882ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
883ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
884ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
885ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
88622050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Inst->setObjectOfFriendDecl();
88793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
88893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
88993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
890e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
89137574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
89237574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
89393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
894a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
895f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
8963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
89724bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
898ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
899259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
90093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9011b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9024c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9034c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
904e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
905259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
906a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9074c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9084c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9094c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9104c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9114c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
912e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
913d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
914d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
915d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
916d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
917d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
919d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
920d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
921d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
922d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
923d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
924d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
925e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
926e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
927e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
928d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9297974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9307974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
931ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
932a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
933ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
934ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
935ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
936ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
9373bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
938ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
939a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
9413bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
944a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
945d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
946d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
947d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
948d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
949d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9507974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9517974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
952ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
953ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->getTemplatedDecl()->isStaticDataMember() &&
954ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
955ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: Also only when instantiating a class?
956ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
957ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template, which
958ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will contain the instantiations of the template parameters.
959ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
960ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = D->getTemplateParameters();
961ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
962ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
963ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return NULL;
964ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
965ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *Pattern = D->getTemplatedDecl();
966ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *PrevVarTemplate = 0;
967ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
968ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Pattern->getPreviousDecl()) {
969ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
970ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Found.empty())
971ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
972ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
973ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
974ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: This, and ForVarTemplate, is a hack that is probably unnecessary.
975ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // We should use a simplified version of VisitVarDecl.
976ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *VarInst = cast_or_null<VarDecl>(VisitVarDecl(Pattern, /*ForVarTemplate=*/true));
977ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
978ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext *DC = Owner;
979ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
980ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  /* FIXME: This should be handled in VisitVarDecl, as used to produce
981ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo     VarInst above.
982ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Instantiate the qualifier.
983ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
984ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (QualifierLoc) {
985ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    QualifierLoc =
986ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
987ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!QualifierLoc)
988ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      return 0;
989ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
990ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
991ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (QualifierLoc)
992ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarInst->setQualifierInfo(QualifierLoc);
993ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  */
994ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
995ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *Inst = VarTemplateDecl::Create(
996ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
997ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarInst, PrevVarTemplate);
998ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarInst->setDescribedVarTemplate(Inst);
999ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1000ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Inst->setAccess(D->getAccess());
1001ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate)
1002ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setInstantiatedFromMemberTemplate(D);
1003ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1004ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (D->isOutOfLine()) {
1005ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1006ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
1007ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1008ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1009ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Owner->addDecl(Inst);
1010ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1011ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate) {
1012ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Queue up any out-of-line partial specializations of this member
1013ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template; the client will force their instantiation once
1014ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the enclosing class has been instantiated.
1015ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1016ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    D->getPartialSpecializations(PartialSpecs);
1017ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1018ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (PartialSpecs[I]->isOutOfLine())
1019ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        OutOfLineVarPartialSpecs.push_back(
1020ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            std::make_pair(Inst, PartialSpecs[I]));
1021ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1022ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1023ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Inst;
1024ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1025ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1026ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1027ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *D) {
1028ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->isStaticDataMember() &&
1029ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
1030ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: Also only when instantiating a class?
1031ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1032ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1033ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1034ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Lookup the already-instantiated declaration and return that.
1035ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1036ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(!Found.empty() && "Instantiation found nothing?");
1037ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1038ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1039ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(InstVarTemplate && "Instantiation did not find a variable template?");
1040ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1041ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplatePartialSpecializationDecl *Result =
1042ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1043ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return Result;
1044ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1045ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1046ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1047ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
10487974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
1049d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1050550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
1051550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
1052a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
1053550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
10542a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1055895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
1056d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1057d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
1059d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
1060a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
1062a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1063a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
1065a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
1066a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1067a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
1068a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
1069a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1070a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
1071d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
1072d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1074d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1076a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
107737d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1078a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1079a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1080e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1081b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1082b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1083e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1084e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1085e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1086b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1087a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1088a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1089b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
10901f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1091a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
10921f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
10931f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
10941f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
10951f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1096b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1097d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1098d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1099d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1100d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1101d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
1102d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1103d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1104ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
11057c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1106ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
11076c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
11086c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
11096c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
11106c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1111d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1112d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1114ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1115ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1117b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1118b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1119b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1120b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1121d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1122eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1123eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1124eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1125eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1126eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1127d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1128f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1129d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
113002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
113102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
113222050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  if (D->getFriendObjectKind())
113322050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Record->setObjectOfFriendDecl();
113402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
11359901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
11369901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
11379901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
11387a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
11399901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
11409901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1141d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
114217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1143d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1144d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1145d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
114671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
114771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
114871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
114971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
115071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
115171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
115271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
115371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
115471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1155bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1156bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1157bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1158bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1159bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1160bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1161bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1162bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1163bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1164bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  return Context.getFunctionType(NewFunc->getResultType(),
11650567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                 NewFunc->getArgTypes(), NewEPI);
116671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
116771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
116802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
116902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
117002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
117102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
117202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
11737557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1174a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1175127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1176127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1177127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1178b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1179c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
11801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11811e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
11822c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1183c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(),
11842c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1186127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
11872c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
11882c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1189127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1191b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1192b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1193b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1194b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1195b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1196b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
119779c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1198b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1199a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
120079c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12012a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
120464b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
120521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
120771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1208fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1209c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1210c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1211c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1212c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1213c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1214c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1215d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1216d325daa506338ab86f9dd468b48fd010673f49a6John McCall
121768b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
121868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
121968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
122068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
122168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1222c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1223d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1224c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1225d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1226d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1227d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1228a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
12297c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1230d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
123168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
123202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1233ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1234635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
1235459ef03126f9f0420efb3355e3b2ed3c1fdfb38aRafael Espindola                           D->getCanonicalDecl()->getStorageClass(),
1236af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
123786c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1238de9ed71c696bee936a21323f61548164de0eda13Enea Zaffanella  Function->setRangeEnd(D->getSourceRange().getEnd());
1239b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1240d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1241d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1242d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1243c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1244c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1245b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1246b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1247b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1248b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1249b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1250b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1251b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1252b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1254e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1255c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  for (unsigned P = 0; P < Params.size(); ++P)
1256c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (Params[P])
1257c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      Params[P]->setOwningFunction(Function);
12584278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
125902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1260ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1261a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1262a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1263a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1264a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1265a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1266a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1267a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1268a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1269a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1270a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1271a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1272a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1273a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1274a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1275a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1276d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1277a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1278a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1279a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1280a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1281b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1282b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1283d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1284d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1285d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1286d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1287d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1288d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1289d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
129066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
129166724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1292c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1293838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1294910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
1295c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.begin(),
1296c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.size()),
12971e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
129880f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
129980f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
130080f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
130180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
130280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
130380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
130480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1305d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
130602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1307a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1308e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1309e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1311af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1312a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13136826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
13146826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
13156826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1316af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1317af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1318af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1319af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1320af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
132122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Function->setObjectOfFriendDecl();
1322af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1323af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1324af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1325af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1326e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1327e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1328e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1329af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1330af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1331af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1332af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1333af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1334af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1335af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1336af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1337af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1338af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1339af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1340af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1341af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1342af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1343af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1344a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1345af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1346af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1347af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1348a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1349a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1350a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
13516826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1352a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1353a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1354a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1355a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1356a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13576826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13586826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1359a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1360a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13619f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
13622c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1363e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
136476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
136576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
136676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
136776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1368a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1369a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1370d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
137122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    PrincipalDecl->setObjectOfFriendDecl();
13721b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1373ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137477535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1375ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
137753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
137853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
137953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
138053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
138153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
138253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
138353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
138453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
138553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
138680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((!SemaRef.getLangOpts().CPlusPlus11 ||
138753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
138853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
138953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
139053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1391238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1392238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1393238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
139410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1395238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
139653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
139780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                     SemaRef.getLangOpts().CPlusPlus11 ?
139853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
139953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1400238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
140180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        if (!SemaRef.getLangOpts().CPlusPlus11)
140253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1403a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1404238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1405238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1406238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1407238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1408238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
140913a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
141013a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1411ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1412ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
141380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          if (!SemaRef.getLangOpts().CPlusPlus11 &&
141453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1415ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1416ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1417ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1418ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1419ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1420ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1421ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1422ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1423ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1424ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1425ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1426ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1427ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1428238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
14296a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
143010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
143153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
143280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                           SemaRef.getLangOpts().CPlusPlus11 ?
143353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
143453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1435238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
14366a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
143780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith              if (!SemaRef.getLangOpts().CPlusPlus11)
143853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1439238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1440238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1441238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1442238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1443238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1444a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1445a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
144676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
144776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
144876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
144976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1450eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1451e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1452e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14532dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1454d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1455d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1456af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1457af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
14586b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1459d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1462d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1463c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
14641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14651e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
14662c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1467c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(),
1468c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                             Innermost.size(),
14692c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14716b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
14722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
14732c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
14746b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
14756b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1476b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1477b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1478b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1479b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1480b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1481b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
148279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1483a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
148479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
14852a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
148648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
14874eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
14885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
14894eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
14904eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
14914eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
14924eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
14934eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
14944eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
14954eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
14964eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
14974eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
14984eab39f0745fb1949dbb40c4145771b927888242John McCall    }
14994eab39f0745fb1949dbb40c4145771b927888242John McCall  }
15004eab39f0745fb1949dbb40c4145771b927888242John McCall
15015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1502dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
150321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
15042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
150571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
15062dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1507c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1508c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1509c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1510b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1511a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1512c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1513b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1514b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1515b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1516b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1517c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1518b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1519c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1520b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1521c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1522c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1523c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1524b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1525b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1526b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1527b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1528b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1529b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1530b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1531b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15322dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1533b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1534dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1536ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
153917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1541ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
154316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
154486c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
1545b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
15464841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // Claim that the instantiation of a constructor or constructor template
15474841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // inherits the same constructor that the template does.
1548b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1549b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            Constructor->getInheritedConstructor())) {
1550b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // If we're instantiating a specialization of a function template, our
1551b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // "inherited constructor" will actually itself be a function template.
1552b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // Instantiate a declaration of it, too.
1553b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      if (FunctionTemplate) {
1554b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1555b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               !Inh->getParent()->isDependentContext() &&
1556b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               "inheriting constructor template in dependent context?");
1557b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1558b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith                                         Inh);
1559b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (Inst)
1560b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1561b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1562b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        LocalInstantiationScope LocalScope(SemaRef);
1563b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
1564b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // Use the same template arguments that we deduced for the inheriting
1565b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // constructor. There's no way they could be deduced differently.
1566b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        MultiLevelTemplateArgumentList InheritedArgs;
1567b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1568b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Inh = cast_or_null<CXXConstructorDecl>(
1569b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1570b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (!Inh)
1571b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1572b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      }
15734841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
1574b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    }
157517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
157617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1577ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15782577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
157916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
158065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
158165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1582ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15830130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1584f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
158586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
15869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1587dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
158872fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola    StorageClass SC = D->isStatic() ? SC_Static : SC_None;
15892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1590ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
159172fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola                                   SC, D->isInlineSpecified(),
159286c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1593dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15946b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1595d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1596d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1597d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1598c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1599c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1600b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1601d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1602d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1603d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1607d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1611d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1616d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1618d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1619b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1620b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
162122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith      FunctionTemplate->setObjectOfFriendDecl();
1622b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
16231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1624d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
162566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
162666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1627c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1628838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1629910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1630c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.begin(),
1631c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.size()),
16321e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1633b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
163466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
16352db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
163666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1637a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
16397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
16407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1641b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
16424eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
16434eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
16444eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
16454eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
16464eab39f0745fb1949dbb40c4145771b927888242John McCall
1647b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
164822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Method->setObjectOfFriendDecl();
1649b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
16507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
16535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
16545545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
16554278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
16592dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
16602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
16612577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
16621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1663b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1664b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1666dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1667dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1668dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1669dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
16706826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
16716826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1672dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1674af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
16752c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
167665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
16774ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
16784ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
16794ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
16801f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
16811f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
16821f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
16831f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
16841f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
16851f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
16861f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
16871f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
16881f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
168946460a68f6508775e98c19b4bb8454bb471aac24John McCall
16909eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
16919eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
16923bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1693ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1694ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
16953bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1696ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
16973bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
16981f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1699b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
17001f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17011f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17021f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1703b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
17041f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17051f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17061f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
17071f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
17081f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
17091f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
17101f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
17111f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
17121f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17131f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
17141f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17151f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
17161f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
17171f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
17181f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
17191f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1720b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1721eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
17222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
17232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1725615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1726dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1727615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1728615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
172903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
173017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
173103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
173203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1733bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
173465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1735bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1736bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
1737ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
173866874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
173966874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
17402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17412dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1744e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
17454fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
17461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1748344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1749344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
17504fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
17514fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
17549a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1755a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17560f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1757a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1759a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1760550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1761550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1762a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1763e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1764e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1765e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
176633642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
176733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
176833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
17696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
17705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
17715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
17726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1773a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
177433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
177533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
17766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
17776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1778a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
17796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
17806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
17816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
17826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
17836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
17846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1785a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
17866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
17876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
17886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1789a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
17926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
17946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
17956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
17966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1797a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
17996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
18006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
18016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
18026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
18036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
18046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
180539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
18066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
18075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
18086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1809a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
18116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
18126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
18136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1814dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1816dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
18176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
18186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1819a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
18206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1821a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
18226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
18236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1824a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
18266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
18286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1829a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
18306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
18316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
18326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1833a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
18376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
18396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
18406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
18416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1842a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
18446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
18466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
18476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
18486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
18516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
18526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
18536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1854a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
18556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
18566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
18576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1858a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
18606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
18616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
18626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1863a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
18666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
18676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1868a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
18696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
18706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
18716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1872a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1874a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
18756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
18766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
18776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
18786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
18796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
188033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1881a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
18836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1884a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1885ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1886ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1887a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
18896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
18906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
18916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
18926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
18936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
18946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1895a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1896ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
18976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1898a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1900a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
19016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1902a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19039a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
190433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
190533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1906a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1907d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1908a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1909a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1910550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1911550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
191233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
191333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
191433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
19156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
19166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
19176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
19186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
19196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
19206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
19216964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
19226964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
19236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
19246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
19256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
19266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
19276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
19286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
19296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
19306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
19316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19320dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
19339106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
19349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
19359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
19369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
19379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
19386964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
19396964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19406964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
19416964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19426964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
19436964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
19446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
19456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
19466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
19476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
19486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
19496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
19516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
19526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
19536964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
19556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19566964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19576964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
19586964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
19596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
19606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
19616964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
19626964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
19636964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
19646964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
19656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
19666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
19706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
1971dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
19746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
19756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
19766964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
19776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
19786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
19796964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19806964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
19816964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
19826964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
19836964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
19846964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
19856964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
19866964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith          return 0;
19876964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
19886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
19896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
19916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
19926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
19936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
19946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
19956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
19966964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
19976964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
19986964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
19996964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20006964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
20016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
20026964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
20036964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
20046964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
20056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
20069106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
20079106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
20082a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
20099106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
20109106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
20116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
2012a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
2013a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
20156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
20166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
20176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
20196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
20206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20216964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
20226964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
20236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
20246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
2026a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
20276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
20296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
2030d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
20319a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
2032a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2033a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
20349106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
20359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2036a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20379106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
20389106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
20399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
204048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2041db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
2042db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
2043a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
204448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
204548c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2046a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
2047db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
2048a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
2049a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
205048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
2051536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
2052536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
2053536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
2054536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
2055536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2056536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
205748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
205848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
205948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2060ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
20611b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20621b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
20631b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
20641b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
20651b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
20661b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
20671b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
20681b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
20691b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
20705149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
20715149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
20725149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
20735149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
2074dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
20751b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20761b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
20771b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2078ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2079ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20809f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
20819f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
20829f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
20839f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
20849f54ad4381370c6b771424b53d219e661d6d6706John McCall
2085ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2086ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
20879f54ad4381370c6b771424b53d219e661d6d6706John McCall
2088ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
20898d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->getUsingLoc(),
20905149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2091ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
20928d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->hasTypename());
2093ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20945149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
20955149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
20969f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
20979f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
20989f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
20999f54ad4381370c6b771424b53d219e661d6d6706John McCall
21009f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
21018d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
21028d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                            D->hasTypename(), SS,
21039f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
21049f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
21059f54ad4381370c6b771424b53d219e661d6d6706John McCall
21069f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
21079f54ad4381370c6b771424b53d219e661d6d6706John McCall
21089f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
21098d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella      SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS,
2110ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2111ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
21129f54ad4381370c6b771424b53d219e661d6d6706John McCall
2113ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2114ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2115ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2116ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21179f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
21189f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
21199f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
21209f54ad4381370c6b771424b53d219e661d6d6706John McCall
2121c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2122c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2123c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
2124c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2125c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2126c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2127323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2128323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
21299f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
21309f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
21319f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
21329f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
21339f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2134b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2135b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
2136b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
2137b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
2138b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
2139b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
21409f54ad4381370c6b771424b53d219e661d6d6706John McCall
21419f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
21429f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
21439f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
21449f54ad4381370c6b771424b53d219e661d6d6706John McCall
21459f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
21469f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
21479f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2148323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2149323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2150323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
21519f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2152ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2153ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2154ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2155ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2156ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
21579f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
21589f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
2159ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2160ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
21635149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2164a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21655149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21665149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
21687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21697ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
21705149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2172ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2173accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2174ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
21757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
21767ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2177ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
21787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
21797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
21804469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2181ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2182ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
21847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
21857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
21885149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
21895149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
21905149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21910dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
2192a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
21930dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
21945149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2196ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2197ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2198ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
22009488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2201ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
22027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
22044469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2205ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2206ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
22080dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
22090dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2210af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2211af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2212af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2213af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
22146b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
22156b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
2216af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2217af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2218af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2219af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
22206b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
22216b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
22226b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
22236b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
22246b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
22256b02009359a462ffe633696a4441313b462e6566Nico Weber  }
22266b02009359a462ffe633696a4441313b462e6566Nico Weber
2227af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
22286b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
22296b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2230af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2231af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2232af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2233af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2234af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2235af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2236af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2237af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2238af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2239af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2240af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2241af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2242c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey BataevDecl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2243c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                     OMPThreadPrivateDecl *D) {
22446af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  SmallVector<Expr *, 5> Vars;
22456af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  for (ArrayRef<Expr *>::iterator I = D->varlist_begin(),
22466af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev                                  E = D->varlist_end();
2247c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev       I != E; ++I) {
2248c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2249c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
22506af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev    Vars.push_back(Var);
2251c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  }
2252c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2253c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OMPThreadPrivateDecl *TD =
2254c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2255c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2256c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  return TD;
2257c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev}
2258c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2259ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
2260ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitFunctionDecl(D, 0);
2261ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2262ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2263ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
2264ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitCXXMethodDecl(D, 0);
2265ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2266ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2267ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2268ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("There are only CXXRecordDecls in C++");
2269ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2270ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2271ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *
2272ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanTemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2273ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    ClassTemplateSpecializationDecl *D) {
2274ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Only ClassTemplatePartialSpecializationDecls occur"
2275ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                   "inside templates");
2276ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2277ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2278ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2279ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *D) {
2280ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2281ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgumentListInfo VarTemplateArgsInfo;
2282ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2283ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(VarTemplate &&
2284ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "A template specialization without specialized template?");
2285ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2286ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the current template arguments.
2287ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2288ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2289ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2290ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2291ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2292ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
2293ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2294ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2295ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this template.
2296ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2297ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  bool ExpansionIntoFixedList = false;
2298ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(
2299ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, VarTemplate->getLocStart(),
2300ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2301ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted, &ExpansionIntoFixedList))
2302ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2303ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2304ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Find the variable template specialization declaration that
2305ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // corresponds to these arguments.
2306ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2308ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted.data(), Converted.size(), InsertPos))
2309ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a variable template specialization, return it.
2310ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return VarSpec;
2311ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2312ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2313ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                            VarTemplateArgsInfo, Converted);
2314ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2316ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2317ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2318ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
2319ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted) {
2320ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is the variable for an anonymous struct or union,
2322ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiate the anonymous struct/union type first.
2323ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        return 0;
2327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2329ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
2330ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2331ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                        D->getTypeSpecStartLoc(), D->getDeclName());
2332ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2333ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2336ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2337ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << D->isStaticDataMember() << DI->getType();
2338ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration
2342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Converted.size());
2346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Var->setTemplateArgsInfo(TemplateArgsInfo);
2347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplate->AddSpecialization(Var, InsertPos);
2348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
2351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2353ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
2354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                     StartingScope);
2355ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Var;
2357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2358ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2359ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2360ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("@defs is not supported in Objective-C++");
2361ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2362ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2363ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2364ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  // FIXME: We need to be able to instantiate FriendTemplateDecls.
2365ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2366ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               DiagnosticsEngine::Error,
2367ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               "cannot instantiate %0 yet");
2368ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  SemaRef.Diag(D->getLocation(), DiagID)
2369ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    << D->getDeclKindName();
2370ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2371ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return 0;
2372ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2373ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2374ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2375ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Unexpected decl");
2376ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2377ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2378ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2379d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
23807e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
23812fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
23822fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
23832fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
23848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
23858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
23868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2387e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2388e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2389e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2390e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2391e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2392e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2393e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2394ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2395e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2396e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2397e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2398e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
23995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2400e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2401e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2402e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2403e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2404bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2405e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
24069148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2407e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2408e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2409e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2410ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2411e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2412e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2413e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2414e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2415e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2416e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2417e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
24181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2419e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2420a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2422ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2423ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2424ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2425ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2426a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2427ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2428ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2429d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2430d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2431d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2432ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2433ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2435550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2436550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2437550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
24382a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2439a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2440ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2441ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2442ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2443ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2444ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2445d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2446a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2447ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2448ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2449c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2450c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2451c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2452c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2453c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2454c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2455e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2456e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2457a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2458ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2459ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
24605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2461a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2462ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2463a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2464ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2465ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2466d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2467ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2468ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2472910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2473910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2474a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2476ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2477a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2478ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2479910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2480910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2482ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2483ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2485ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2487ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
24893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
24903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
24913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
24923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2493d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2494ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2495a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2496ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2497ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2498ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2499ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2500ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2501ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2502ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2503ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2504ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2505ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2506ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2507ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2510ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2511ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2512ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2513d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2514ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2515ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2516d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2517ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2518a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2519a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2520ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2521ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2522a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
252313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2524a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2525ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2526ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2527ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2528a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2529910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2530910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2531d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
25323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2533dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2534cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2535b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2536b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2537b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2538b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2539ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
25404469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2541a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2542ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2543ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
25441e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2545d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2546ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2547ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2548ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the declaration of a variable template partial
2549ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization.
2550ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2551ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param VarTemplate the (instantiated) variable template that is partially
2552ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialized by the instantiation of \p PartialSpec.
2553ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2554ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param PartialSpec the (uninstantiated) variable template partial
2555ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization that we are instantiating.
2556ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2557ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \returns The instantiated partial specialization, if successful; otherwise,
2558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// NULL to indicate an error.
2559ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplatePartialSpecializationDecl *
2560ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoTemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2561ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate,
2562ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *PartialSpec) {
2563ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template partial
2564ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization, which will contain the instantiations of the template
2565ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // parameters.
2566ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
2567ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2568ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template parameters of the variable template partial
2569ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2570ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2571ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2572ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
2573ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2574ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2575ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template arguments of the variable template partial
2576ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2577c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2578c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2579c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2580c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2581c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2582c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2583ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    InstTemplateArgs, TemplateArgs))
2584ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2585ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2586ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this
2587ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // class template.
2588ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2589ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2590ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        InstTemplateArgs, false, Converted))
2591ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2592ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2593ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Figure out where to insert this variable template partial specialization
2594ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // in the member template's set of variable template partial specializations.
2595ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2596ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *PrevDecl =
2597ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate->findPartialSpecialization(Converted.data(), Converted.size(),
2598ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                             InsertPos);
2599ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2600ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the canonical type that describes the converted template
2601ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // arguments of the variable template partial specialization.
2602ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2603ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), Converted.data(), Converted.size());
2604ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2605ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the fully-sugared type for this variable template
2606ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization as the user wrote in the specialization
2607ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // itself. This means that we'll pretty-print the type retrieved
2608ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // from the specialization's declaration the way that the user
2609ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // actually wrote the specialization, rather than formatting the
2610ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // name based on the "canonical" representation used to store the
2611ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // template arguments in the specialization.
2612ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2613ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2614ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CanonType);
2615ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2616ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (PrevDecl) {
2617ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We've already seen a partial specialization with the same template
2618ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // parameters and template arguments. This can happen, for example, when
2619ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // substituting the outer template arguments ends up causing two
2620ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template partial specializations of a member variable template
2621ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // to have identical forms, e.g.,
2622ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2623ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   template<typename T, typename U>
2624ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   struct Outer {
2625ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename X, typename Y> pair<X,Y> p;
2626ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<T, Y> p;
2627ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<U, Y> p;
2628ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   };
2629ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2630ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   Outer<int, int> outer; // error: the partial specializations of Inner
2631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //                          // have the same signature.
2632ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2633ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_var_partial_spec_redeclared)
2634ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << WrittenTy->getType();
2635ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PrevDecl->getLocation(),
2636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::note_var_prev_partial_spec_here);
2637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2638ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2639ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2640ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2641ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI = SemaRef.SubstType(
2642ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSourceInfo(), TemplateArgs,
2643ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2644ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2645ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2646ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2647ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2648ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2649ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_variable_instantiates_to_function)
2650ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << PartialSpec->isStaticDataMember() << DI->getType();
2651ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2652ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2653ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2654ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create the variable template partial specialization declaration.
2655ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplatePartialSpecializationDecl *InstPartialSpec =
2656ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplatePartialSpecializationDecl::Create(
2657ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2658ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2659ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          DI, PartialSpec->getStorageClass(), Converted.data(),
2660ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted.size(), InstTemplateArgs,
2661ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate->getNextPartialSpecSequenceNumber());
2662ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2664ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(PartialSpec, InstPartialSpec))
2665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setTypeAsWritten(WrittenTy);
2669ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2670ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setAccess(PartialSpec->getAccess());
2671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: How much of BuildVariableInstantiation() should go in here?
2672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2673ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Add this partial specialization to the set of variable template partial
2674ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specializations. The instantiation of the initializer is not necessary.
2675ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2676ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstPartialSpec;
2677ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2678ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
267921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
268021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
26815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
268221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
268321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
268421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2685cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2686cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2687cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2688cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2689cafeb948e6067b8dc897c441da522367917b06f9Richard Smith    ThisContext = cast<CXXRecordDecl>(Owner);
2690cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2691cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2692cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
26936cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
26946cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
26956cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2696cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2697cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
269821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
269921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
27005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2701c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2702c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2703c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (NewTInfo != OldTInfo) {
2704c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // Get parameters from the new type info.
2705140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
270639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2707500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
270839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
270912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
271039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
2711500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2712500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2713dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2714500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2715500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2716500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2717500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2718500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2719a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
272012c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
272139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
272212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2723500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2724500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2725500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2726500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2727500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
272839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
2729500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2730500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2731500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
273212c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
27336920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2734c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    } else {
2735c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // The function type itself was not dependent and therefore no
2736c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // substitution occurred. However, we still need to instantiate
2737c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // the function parameters themselves.
2738c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      const FunctionProtoType *OldProto =
2739c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          cast<FunctionProtoType>(OldProtoLoc.getType());
274039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2741c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        ParmVarDecl *OldParam = OldProtoLoc.getArg(i);
2742c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        if (!OldParam) {
2743c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
2744c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner              D, D->getLocation(), OldProto->getArgType(i)));
2745c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          continue;
2746c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        }
2747c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
2748ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman        ParmVarDecl *Parm =
2749c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner            cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
27506920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
27516920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
27526920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
27536920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2754cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2755c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  } else {
2756c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // If the type of this function, after ignoring parentheses, is not
2757c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // *directly* a function type, then we're instantiating a function that
2758c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // was declared via a typedef or with attributes, e.g.,
2759c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2760c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   typedef int functype(int, int);
2761c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   functype func;
2762c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   int __cdecl meth(int, int);
2763c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2764c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // In this case, we'll just go instantiate the ParmVarDecls that we
2765c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // synthesized in the method declaration.
2766c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    SmallVector<QualType, 4> ParamTypes;
2767c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
2768c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               D->getNumParams(), TemplateArgs, ParamTypes,
2769c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               &Params))
2770c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      return 0;
2771cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
2772c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
277321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
27745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
27755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2776e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2777e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2778e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2779e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2780e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2781e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2782e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2783e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2784e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2785e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2786e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2787e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2788e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2789e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2790e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2791e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2792e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2793e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2794e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2795e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2796e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2797e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2798dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
2799e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2800500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2801500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2802500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2803e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2804e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2805e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2806e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2807e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2808e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2809e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2810e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2811e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2812e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2813e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
281413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
281513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2816e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2817e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2818e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2819e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2820e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2821e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2822e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2823e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2824e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2825e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2826e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2827e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2828e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
282980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
2830e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2831e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2832e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2833e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2834e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2835e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2836e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2837e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2838e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2839e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2840e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2841e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2842e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2843e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2844e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2845e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2846e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2847cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
2848e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2849e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2850e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2851e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2852e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2853e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2854e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2855e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2856e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2857e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2858e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2859e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2860e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2861e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2862e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2863e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2864e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2865e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2866e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2867e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2868e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2869e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2870e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2871e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2872e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2873e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2874e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2875e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2876e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2877e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2878e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2879e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2880e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2881e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2882e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2883e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2884e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2885e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2886e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2887e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2888e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2889e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2890e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2891e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2892e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2893e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2894e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2895e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2896e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2897e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2898e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2899e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2900e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2901e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2902e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2903e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2904e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2905e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2906e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2907e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2908e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2909e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2910e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2911e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2912e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2913e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2914e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2915e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2916ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2917ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2918ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2919ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2920e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2921e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2922e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2923e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2924e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2925e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2926e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2927e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2928e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2929e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2930e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2931e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2932e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2933e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2934e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
29350567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                               NewProto->getArgTypes(), EPI));
2936e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2937e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2938e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2939e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
294013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
294113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2942e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2943e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2944e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2945e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2946b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (Inst) {
2947b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
2948b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
2949b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2950b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
2951b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Decl->setType(Context.getFunctionType(Proto->getResultType(),
29520567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                          Proto->getArgTypes(), EPI));
2953e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2954b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
2955e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2956e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
2957e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
2958e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
2959e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
2960e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2961e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
2962e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2963e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
296413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
296513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
2966e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
296713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
296813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
296913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
2970e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2971e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
29721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2973e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2974e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2975e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
29761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
29771eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2978e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
297985f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
298010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
29811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2982cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2983cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2984cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
29851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
29861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2987cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2988cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2989cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2990cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2991cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2992cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2993cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
29941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
29954a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
29961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2997cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2998bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2999cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
30004a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
3001cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
3002cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
30031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30040ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
30050ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
30060ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
300760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
3008e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
3009e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3010e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
3011e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
301280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
3013e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
3014e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
3015e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
301613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
301713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
301813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
30194841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      ExceptionSpecificationType NewEST = EST_Uninstantiated;
30204841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      if (EPI.ExceptionSpecType == EST_Unevaluated)
30214841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith        NewEST = EST_Unevaluated;
302213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
3023e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
3024e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
3025e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
3026e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
3027e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
30284841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      EPI.ExceptionSpecType = NewEST;
3029e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
303013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
30310567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      New->setType(SemaRef.Context.getFunctionType(
30320567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner          NewProto->getResultType(), NewProto->getArgTypes(), EPI));
3033e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
3034e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
3035e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
30360ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
30370ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
303819f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
3039e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
304019f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
304119f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
304223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
304323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
30447cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
3045e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
3046e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3047e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
30485545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
30495545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
30505545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
30515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
30525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
30531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
30541eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
30555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
3056e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
3057e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
30581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
3060e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
306185606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
30625545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
30635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
30645545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
30655545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
3066a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3067a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
3068a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3069a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
3070b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
3071b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
3072b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
3073b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3074a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
3075b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
3076b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
3077b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3078b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
3079b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
3080e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3081e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3082e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
3083e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
3084f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
3085b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
3086e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
3087e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
308810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
308954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
309054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
3091af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
3092af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
3093af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3094af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
3095251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
30966cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
30971eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
30983b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
3099f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
3100f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
3101f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3102f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
3103f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
3104f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
3105f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
3106dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
3107f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
31081eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
31098387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
3110f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
31118a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
31128387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
31138387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
31148387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
31158387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31168387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
31178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
3118a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
3119f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
31208387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
3121ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    // FIXME: Optimize to allow individual templates to be deserialized.
3122ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    if (PatternDecl->isFromASTFile())
3123ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith      ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3124ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith
3125ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3126ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    assert(LPT && "missing LateParsedTemplate");
3127ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateTemplateParser(OpaqueParser, *LPT);
31288387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
31298387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31308387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3131f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
3132e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3133e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
3134a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3135e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
3136e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
3137e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
3138a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3139e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
3140e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
3141a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3142e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
3143a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
3144e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
3145cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
314658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
314758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
314862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
314958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
3150e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
315158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
31521eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
3153e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
31541eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
315560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // C++1y [temp.explicit]p10:
315660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   Except for inline functions, declarations with types deduced from their
315760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   initializer or return value, and class template specializations, other
315860e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   explicit instantiation declarations have the effect of suppressing the
315960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   implicit instantiation of the entity to which they refer.
31601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
3161d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
316260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->isInlined() &&
316360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->getResultType()->isUndeducedType())
3164d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
31651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3166d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (PatternDecl->isInlined())
3167d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
3168d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
3169f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3170f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
3171a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
3172a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3173e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
3174e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3175e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
3176b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
3177b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
3178b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
31795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
318062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
318186648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  std::deque<PendingImplicitInstantiation>
318286648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                              SavedPendingLocalImplicitInstantiations;
318386648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  SavedPendingLocalImplicitInstantiations.swap(
318486648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                                  PendingLocalImplicitInstantiations);
31852a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
31862a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
318762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
31882a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
31891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3190a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
3191f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
3192e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
319354dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
319460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
319560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
319660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
319760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
319860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
319960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
320060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
320160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
32021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32031d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
32041d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
32051d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
32061d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    ActOnStartOfFunctionDef(0, Function);
32077c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32081d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
32091d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
32101d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
32117c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32121d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
32131d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith      getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
321454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
32151d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
32161d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
32171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3218cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
3219cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
3220cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3221cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3222cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
3223cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
3224cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3225cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
3226cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3227cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3228cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
3229cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
3230a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3231cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
3232cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
3233b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
32341d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
32350c01d18094100db92d38daa923c95661512db203John McCall
32361d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
32371d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
3238aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
3239aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
3240aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
32411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
324260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
324360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
324462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
324560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
324660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
3247b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
32482a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
32492a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
32502a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3251b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
32521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
325362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
32541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32552a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
32568155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
32578155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
32582a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
32592a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3260b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
32618155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
32628155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
326362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
3264b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
326586648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  SavedPendingLocalImplicitInstantiations.swap(
326686648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                            PendingLocalImplicitInstantiations);
3267a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3268a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3269ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3270ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3271ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentList &TemplateArgList,
3272ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
3273ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted,
3274ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SourceLocation PointOfInstantiation, void *InsertPos,
3275ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LateInstantiatedAttrVec *LateAttrs,
3276ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LocalInstantiationScope *StartingScope) {
3277ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (FromVar->isInvalidDecl())
3278ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3279ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3280ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3281ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Inst)
3282ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3283ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3284ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  MultiLevelTemplateArgumentList TemplateArgLists;
3285ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3286ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3287ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateDeclInstantiator Instantiator(
3288ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      *this, VarTemplate->getDeclContext(),
3289ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      MultiLevelTemplateArgumentList(TemplateArgList));
3290ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3291ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // TODO: Set LateAttrs and StartingScope ...
3292ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3293ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return cast_or_null<VarTemplateSpecializationDecl>(
3294ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Instantiator.VisitVarTemplateSpecializationDecl(
3295ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3296ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3297ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3298ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiates a variable template specialization by completing it
3299ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// with appropriate type information and initializer.
3300ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3301ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3302ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3303ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3304ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
3305ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
3306ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs,
3307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3308ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
3309ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3310ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3311ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Update the type of this variable template specialization.
3312ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarSpec->setType(DI->getType());
3313ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3314ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Instantiate the initializer.
3315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3316ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3317ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VarSpec;
3318ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3319ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3320ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// BuildVariableInstantiation - Used after a new variable has been created.
3321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// Sets basic variable data and decides whether to postpone the
3322ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// variable instantiation.
3323ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::BuildVariableInstantiation(
3324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *NewVar, VarDecl *OldVar,
3325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs,
3326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LateInstantiatedAttrVec *LateAttrs,
3327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LocalInstantiationScope *StartingScope,
3328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool ForVarTemplate) {
3329ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3330ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If we are instantiating a static data member defined
3331ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line, the instantiation will have the same lexical
3332ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // context (which will be a namespace scope) as the template.
3333ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->isOutOfLine())
3334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setTSCSpec(OldVar->getTSCSpec());
3336ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setInitStyle(OldVar->getInitStyle());
3337ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3338ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setConstexpr(OldVar->isConstexpr());
3339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setAccess(OldVar->getAccess());
3340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!OldVar->isStaticDataMember()) {
3342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setUsed(OldVar->isUsed(false));
3343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setReferenced(OldVar->isReferenced());
3344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (NewVar->hasAttrs())
3349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    CheckAlignasUnderalignment(NewVar);
3350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: In theory, we could have a previous declaration for variables that
3352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // are not static data members.
3353ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // FIXME: having to fake up a LookupResult is dumb.
3354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LookupResult Previous(*this, NewVar->getDeclName(), NewVar->getLocation(),
3355ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
3356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!isa<VarTemplateSpecializationDecl>(NewVar))
3358ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
3359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  CheckVariableDeclaration(NewVar, Previous);
3361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3362ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->isOutOfLine()) {
3363ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    OldVar->getLexicalDeclContext()->addDecl(NewVar);
3364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!ForVarTemplate)
3365ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
3366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else {
3367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!ForVarTemplate)
3368ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->addDecl(NewVar);
3369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (NewVar->getDeclContext()->isFunctionOrMethod())
3370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3371ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3372ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Link instantiations of static data members back to the template from
3374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // which they were instantiated.
3375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (NewVar->isStaticDataMember() && !ForVarTemplate)
3376ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setInstantiationOfStaticDataMember(OldVar,
3377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                               TSK_ImplicitInstantiation);
3378ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3379ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (isa<VarTemplateSpecializationDecl>(NewVar)) {
3380ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Do not instantiate the variable just yet.
3381ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else
3382ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3383ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3384ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Diagnose unused local variables with dependent types, where the diagnostic
3385ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will have been deferred.
3386ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!NewVar->isInvalidDecl() &&
3387ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() &&
3388ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      OldVar->getType()->isDependentType())
3389ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DiagnoseUnusedDecl(NewVar);
3390ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3391ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3392ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the initializer of a variable.
3393ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableInitializer(
3394ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *Var, VarDecl *OldVar,
3395ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3396ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3397ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->getAnyInitializer())
3398ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We already have an initializer in the class.
3399ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return;
3400ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3401ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->getInit()) {
3402ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3403ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3404ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    else
3405ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3406ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3407ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate the initializer.
3408ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ExprResult Init =
3409ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        SubstInitializer(OldVar->getInit(), TemplateArgs,
3410ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                         OldVar->getInitStyle() == VarDecl::CallInit);
3411ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Init.isInvalid()) {
3412ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      bool TypeMayContainAuto = true;
3413ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (Init.get()) {
3414ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        bool DirectInit = OldVar->isDirectInit();
3415ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        AddInitializerToDecl(Var, Init.take(), DirectInit, TypeMayContainAuto);
3416ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else
3417ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        ActOnUninitializedDecl(Var, TypeMayContainAuto);
3418ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    } else {
3419ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // FIXME: Not too happy about invalidating the declaration
3420ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // because of a bogus initializer.
3421ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setInvalidDecl();
3422ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
3423ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3424ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PopExpressionEvaluationContext();
3425ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3426ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             !Var->isCXXForRangeDecl())
3427ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ActOnUninitializedDecl(Var, false);
3428ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3429ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3430a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
3431a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3432a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
34337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
34347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
34357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
34367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
34377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
34387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
34397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
34407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
34417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
3442e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3443e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3444e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
3445e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
34467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
34477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
34487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
3449e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
3450e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
3451ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3452ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                DefinitionRequired);
3453ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3454ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3455ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3456ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         VarDecl *Var, bool Recursive,
3457ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         bool DefinitionRequired) {
3458ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
34597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
34607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
34611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3462ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *VarSpec =
3463ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      dyn_cast<VarTemplateSpecializationDecl>(Var);
3464ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert((VarSpec || Var->isStaticDataMember()) &&
3465ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Not a static data member, nor a variable template specialization?");
3466ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *PatternDecl = 0;
3467ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3468ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is a variable template specialization, make sure that it is
3469ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // non-dependent, then find its instantiation pattern.
3470ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarSpec) {
3471ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
3472ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3473ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo               VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3474ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Only instantiate variable template specializations that are "
3475ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "not type-dependent");
34763151b7c6dd49947b0a91b3e22c31f4864629e355Larisse Voufo    (void)InstantiationDependent;
3477ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3478ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Find the variable initialization that we'll be substituting.
3479ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(VarSpec->getSpecializedTemplate() &&
3480ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Specialization without specialized template?");
3481ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    llvm::PointerUnion<VarTemplateDecl *,
3482ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                       VarTemplatePartialSpecializationDecl *> PatternPtr =
3483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getSpecializedTemplateOrPartial();
3484ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>())
3485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PatternDecl = cast<VarDecl>(
3486ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PatternPtr.get<VarTemplatePartialSpecializationDecl *>());
3487ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    else
3488ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PatternDecl = (PatternPtr.get<VarTemplateDecl *>())->getTemplatedDecl();
3489ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(PatternDecl && "instantiating a non-template");
3490ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3491ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3492ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is a static data member, find its out-of-line definition.
34937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
3494ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->isStaticDataMember()) {
3495ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(Def && "This data member was not instantiated from a template?");
3496ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(Def->isStaticDataMember() && "Not a static data member?");
3497ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Def = Def->getOutOfLineDefinition();
3498ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3499ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3500ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If the instantiation pattern does not have an initializer, or if an
3501ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line definition is not found, we won't perform any instantiation.
3502ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Rather, we rely on the user to instantiate this definition (or provide
3503ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // a specialization for it) in another translation unit.
3504ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if ((VarSpec && !PatternDecl->getInit()) ||
3505ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      (!VarSpec && Var->isStaticDataMember() && !Def)) {
3506e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3507ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!Var->isStaticDataMember()) {
3508ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3509ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_var_template)
3510ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            << PatternDecl;
3511ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PatternDecl->getLocation(),
3512ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::note_explicit_instantiation_here);
3513ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else {
3514ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Def = Var->getInstantiatedFromStaticDataMember();
3515ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3516ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_member)
3517ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            << 3 << Var->getDeclName() << Var->getDeclContext();
3518ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
3519ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
3520ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (VarSpec)
3521ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Var->setInvalidDecl();
352258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
352358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
352462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
352558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
352658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
352758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
35287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
35297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
35307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3531234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3532234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
3533251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
3534234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
3535251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
3536a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3537251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
3538251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
3539251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
3540251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
3541ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //
3542ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // C++11 [temp.explicit]p10:
3543ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   Except for inline functions, [...] explicit instantiation declarations
3544ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   have the effect of suppressing the implicit instantiation of the entity
3545ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   to which they refer.
3546234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
3547251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
35481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3549afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
3550afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
3551afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
3552afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
3553afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3554afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3555afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
3556afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3557afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
3558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (Var->isStaticDataMember())
3559ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3560ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      else {
3561ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        DeclGroupRef DG(Var);
3562ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Consumer.HandleTopLevelDecl(DG);
3563ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
3564afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
3565afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
3566025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
3567ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec) {
3568ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a definition, we're done.
3569ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (VarDecl *Def = Var->getDefinition()) {
3570ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // We may be explicitly instantiating something we've already implicitly
3571ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // instantiated.
3572ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3573ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         PointOfInstantiation);
3574ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      return;
3575ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
357695e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
3577f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
35787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
35797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
35807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
35811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
35837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
35847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
35855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
358662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
35878155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
35888155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
358962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
35908155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
35911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
35937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3594ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  ContextRAII PreviousContext(*this, Var->getDeclContext());
35957bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
3596ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
35971028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3598ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec)
3599ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3600ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                          getTemplateInstantiationArgs(Var)));
3601ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  else
3602ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Construct a VarTemplateSpecializationDecl to avoid name clashing with
3603ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the primary template. (Note that unlike function declarations, variable
3604ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // declarations cannot be overloaded.)
3605ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // In fact, there is no need to construct a new declaration from scratch.
3606ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Thus, simply complete its definition with an appropriately substituted
3607ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // type and initializer.
3608ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = CompleteVarTemplateSpecializationDecl(
3609ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec, PatternDecl, getTemplateInstantiationArgs(Var));
3610f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3611ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PreviousContext.pop();
36127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
36137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3614583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
3615ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!VarSpec)
3616ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      assert(MSInfo && "Missing member specialization information?");
3617ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3618ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PassToConsumerRAII.Var = Var;
3619ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (MSInfo)
3620ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setTemplateSpecializationKind(
3621ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          MSInfo->getTemplateSpecializationKind(),
3622ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          MSInfo->getPointOfInstantiation());
36237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3624ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3625ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // This variable may have local implicit instantiations that need to be
3626ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiated within this scope.
3627ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PerformPendingInstantiations(/*LocalOnly=*/true);
3628ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
36297bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
36307bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
36317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
36328155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
36338155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
36348155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
36357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
36361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
363762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
36381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36398155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
36408155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
3641ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "VTableUses should be empty before it is discarded.");
36428155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
36438155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
36447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
36458155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
3646ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "PendingInstantiations should be empty before it is discarded.");
364762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
36481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3649a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3650815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3651090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
3652090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
3653090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
3654090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
36551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365690ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
365754b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
3658a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3659090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
3660090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
366172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
366272f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
3663cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
3664090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
3665030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
3666030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
3667030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
3668030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
3669030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
36703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
3671a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
36723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
36733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
367476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
367598a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      SmallVector<UnexpandedParameterPack, 4> Unexpanded;
36763fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
367798a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
36783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
3679d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
3680dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
3681a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
36823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
3683a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
3684a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
3685d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
36863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
36873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
36883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
36893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
36903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
36913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
3692a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3693a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
3694cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
36953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
36963fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
36973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
36985b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
36995b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
37005b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
37013fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37023fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37033fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
37043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37053fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
370676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
3707a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
3708a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
37093fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
37103fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
37113fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
37143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37153fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
37166df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
37175b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
37183fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
37193fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
37203fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
37213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
3724a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
37263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
3727a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37283fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
37293fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
37303fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37316b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
37325b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
37335b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
37345b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
37356b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
37366b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
3737090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3738a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3739090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
374076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
374176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
374276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
374376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
374476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
374576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
37469db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
3747802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
3748802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
3749802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
37506df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
375176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
37525b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
375376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
375476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
37555b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
375676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
3757090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
3758b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
375900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
376000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
376100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
3762b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
3763b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3764b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
3765b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
3766b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
37671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37685b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
37696df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
377000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
377100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
3772b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
377300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
377400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
377500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
3776b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
3777b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3778b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
37796df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
3780b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
37816df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
37825b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
37836df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
3784090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3785090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
37869db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
37879db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
3788090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
37899db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
379090ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
3791090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3792090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
37931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3794090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
3795d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
3796090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
3797090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
379893c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
37999db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
3800090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
3801090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
380252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
380352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
380452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
380552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
380652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
380752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
380852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
380952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
381052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
381152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
381252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
381352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
381452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
381552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
381652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
38170d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
38180d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
38190d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3820a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38210d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
38220d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
38230d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
38240d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
38250d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3826a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38270d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
38280d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
38290d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3830a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3831ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3832ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3833a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3834ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3835ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3838ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3839ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3840ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3841ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
3842a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3843ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
3844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
3845ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
384652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
384752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
384852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
384952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
385052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
385152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
385252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
385352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
385452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
385552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
385652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
385752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
385852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
385952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
386052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
386152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
386252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
386352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
386452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
386552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
386652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
386752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
386852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
386952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
387052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
387152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
387252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
387352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
387452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
387552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
387652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
387752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
387852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
387952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
388052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
388152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
388252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
388352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
388452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3885ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3886ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3887ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3888ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3889ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3890ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3891ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3892ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3893ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3894ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3895ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3896ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
38977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
38987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
38997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3900ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
39017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
39027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
39037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
39040d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
39050d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3906ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
39070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
39080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
390952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
391052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
391152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
391252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
391352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
391452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
391552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
391652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
391752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
391852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
391952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
392052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
392152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
392252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
392352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3924ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3925ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3926815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
39270d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
39287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
39297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
39307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
39317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
39327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
39337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
39347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
39357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
39367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
39370d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
39380d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
39390d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
39400d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
3941815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
39420d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
39430d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
39441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
394552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
394652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
39471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
394852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
394952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3950815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
395152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
395252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3953815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
39547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
395552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
395652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
395752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
395852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
395952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3960a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
39610d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
39620d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
39630d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3964ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3965ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3966ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3967ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3968ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3969d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3970d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3971d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
39721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3973d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3974d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3975d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
39761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3977ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3978ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3979ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3980ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3981ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3982ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3983815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3984815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3985815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3986815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3987815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
39881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3989815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3990815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3991815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3992815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3993815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3994815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3995815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3996815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3997815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3998815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
399902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
400002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
400102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
400202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
40037c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
4004e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
400502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
40067c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
400702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
400802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
400902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
401002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
4011ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
4012ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
4013815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4014815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
4015815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
4016815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
4017815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
4018815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4019815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
4020815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
4021815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
4022815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
4023815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
4024815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
4025815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4026815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
4027815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
4028815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4029815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
4030815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
4031815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4032041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4033041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// \p EnumConstantDecl for \p KnownValue (which refers to
4034041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4035041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4036041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// this mapping from within the instantiation of <tt>X<int></tt>.
40377c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
4038e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
4039815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
4040550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
40416d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
40427bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
40437bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
40442bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
40452bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
4046d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4047d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
4048d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
4049a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
405057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
405157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
405257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
4053a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40549a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
40559a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
405657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
405757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
405857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
4059dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // If we're performing a partial substitution during template argument
4060dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // deduction, we may not have values for template parameters yet. They
4061dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // just map to themselves.
4062dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4063dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov        isa<TemplateTemplateParmDecl>(D))
4064dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov      return D;
4065dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov
406629a46e63490176608efe13f13b293a6ce9862059Serge Pavlov    if (D->isInvalidDecl())
406729a46e63490176608efe13f13b293a6ce9862059Serge Pavlov      return 0;
406829a46e63490176608efe13f13b293a6ce9862059Serge Pavlov
406957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
407057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
407157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
4072a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
407357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
407457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
4075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
407657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
407757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
40782bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
4079815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4080ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // For variable template specializations, update those that are still
4081ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // type-dependent.
4082ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec =
4083ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          dyn_cast<VarTemplateSpecializationDecl>(D)) {
4084ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
4085ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &VarTemplateArgs =
4086ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getTemplateArgsInfo();
4087ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (TemplateSpecializationType::anyDependentTemplateArguments(
4088ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            VarTemplateArgs, InstantiationDependent))
4089ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      D = cast<NamedDecl>(
4090ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4091ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return D;
4092ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
4093ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4094e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4095e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
4096e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
4097a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40982c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
40992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
4100e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
41012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
41022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
41032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
41042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
41052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
4106ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41072c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
41082c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
41092c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
41102c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
41112c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
41122c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
41132c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
41142c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
4115ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41162c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
41172c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
41182c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
41192c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
41202c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
41212c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
41222c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
41232c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
41242c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
4125ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41262c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
41272c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
41282c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
4129e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
4130ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41312c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
41322c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
41332c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
41342c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
41352c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
41362c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
413752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
4138ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41392c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
414052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
41418b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
4142e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
4143e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
4144e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
414552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4146e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
4147e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
4148a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
41497c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
41501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
415144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
41521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4153815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
4154815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
4155815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
4156815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
41577c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
41583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
41593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
4160eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
41613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
41627c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
41637c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
41643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
41653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
4166eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
4167eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
41683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
41693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
41703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
4171a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
4172a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
41737c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
41747c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
41757c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
4176815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
4177815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
417817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
41793bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
4180815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
4181815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
4182815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
4183815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
4184815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4185815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
4186815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
4187815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4188815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
41891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
419017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
419117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
4192815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
41931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4194eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
4195eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
4196eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
4197eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
4198eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
4199eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
4200eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
4201eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
4202a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
4203eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
4204eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
4205eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
4206eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
4207eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
4208eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
4209eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4210eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
42110724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
42120724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
42130724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
42140724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
42150724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
42160724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
42170724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
42180724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
42190724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
42200724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
42210724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
42220724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
42230724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
42240724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
4225eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
4226eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
4227eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
4228eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
4229eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
4230a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4231815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
4232815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
4233815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4234815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
4235815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4236d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
42371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
4238d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
42398155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
42406e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
42416e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
4242b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    SmallVector<PendingImplicitInstantiation, 4> Pending;
42436e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
42446e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
42456e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
42466e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
4247a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
424860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
424962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
425060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
425160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
425260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
425362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
425462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
425560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
425660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
425760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
425860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
42591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
42617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
4262f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4263f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
426458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
426558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
426658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
426758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
42687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
42697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
42701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4271ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate variable definitions
42727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
4273ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4274ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert((Var->isStaticDataMember() ||
4275ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            isa<VarTemplateSpecializationDecl>(Var)) &&
4276ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Not a static data member, nor a variable template"
4277ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           " specialization?");
4278c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
4279291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
4280291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
4281ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
4282291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
4283291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4284291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
4285291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
4286ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
4287291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
4288b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
4289291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
4290291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
429158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
429258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
429358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
429458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
4295ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
4296291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
4297291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
4298291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
4299291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4300ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4301ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        "instantiating variable definition");
430258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
430358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
4304ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4305ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate static data member definitions or variable template
4306ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // specializations.
4307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4308ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                  DefinitionRequired);
4309d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
4310d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
43110c01d18094100db92d38daa923c95661512db203John McCall
43120c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
43130c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
43140c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
43150c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
43160c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
43170c01d18094100db92d38daa923c95661512db203John McCall
43180c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
43190c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
43200c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
43210c01d18094100db92d38daa923c95661512db203John McCall      break;
43220c01d18094100db92d38daa923c95661512db203John McCall    }
43230c01d18094100db92d38daa923c95661512db203John McCall  }
43240c01d18094100db92d38daa923c95661512db203John McCall}
4325