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"
15651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/ASTMutationListener.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
180c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
20a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.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
2857907e56191adea0fa870c052054eb0fe0c4681fBill Wendlingstatic bool isDeclWithinFunction(const Decl *D) {
2957907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  const DeclContext *DC = D->getDeclContext();
3057907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (DC->isFunctionOrMethod())
3157907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    return true;
3257907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
3357907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (DC->isRecord())
3457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    return cast<CXXRecordDecl>(DC)->isLocalClass();
3557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
3657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  return false;
3757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling}
3857907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
39b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
40b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
41c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
42c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
43a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
44c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
45a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
46c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                          TemplateArgs);
47a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
48c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
49b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
50a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
52b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
53b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
55b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
56b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
57c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
58c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
59a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
60c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
61a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
62c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                        TemplateArgs);
63a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
64c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
65b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
66a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
67c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
68b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
69b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
70b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
717b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins// Include attribute instantiation code.
727b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins#include "clang/Sema/AttrTemplateInstantiate.inc"
737b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins
74f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
75f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
76f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) {
77f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr()) {
78f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    // The alignment expression is a constant expression.
79f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
80f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
81f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (!Result.isInvalid())
82ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(),
83f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
84f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
85f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(),
86f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         TemplateArgs, Aligned->getLocation(),
87f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         DeclarationName());
88f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Result)
89f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result,
90f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
91f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
92f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
93f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
94f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
95f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
96f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New) {
97f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Aligned->isPackExpansion()) {
98f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
99f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
100f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
101f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
102f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
103f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr())
104f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
105f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
106f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  else
107f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
108f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
109f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
110f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
111f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // Determine whether we can expand this attribute pack yet.
112f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  bool Expand = true, RetainExpansion = false;
113f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  Optional<unsigned> NumExpansions;
114f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // FIXME: Use the actual location of the ellipsis.
115f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SourceLocation EllipsisLoc = Aligned->getLocation();
116f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
117f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        Unexpanded, TemplateArgs, Expand,
118f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        RetainExpansion, NumExpansions))
119f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
120f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
121f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Expand) {
122f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
123f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true);
124f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
125f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    for (unsigned I = 0; I != *NumExpansions; ++I) {
126f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I);
127f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
128f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    }
129f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
130f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
131f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
132651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic void instantiateDependentEnableIfAttr(
133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    const EnableIfAttr *A, const Decl *Tmpl, Decl *New) {
1356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Expr *Cond = nullptr;
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  {
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
138651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ExprResult Result = S.SubstExpr(A->getCond(), TemplateArgs);
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Result.isInvalid())
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return;
141ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    Cond = Result.getAs<Expr>();
142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (A->getCond()->isTypeDependent() && !Cond->isTypeDependent()) {
144651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
145651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Converted.isInvalid())
146651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      return;
147ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    Cond = Converted.get();
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
149651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
150651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SmallVector<PartialDiagnosticAt, 8> Diags;
151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (A->getCond()->isValueDependent() && !Cond->isValueDependent() &&
152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(Tmpl),
153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                                Diags)) {
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    S.Diag(A->getLocation(), diag::err_enable_if_never_constant_expr);
155651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (int I = 0, N = Diags.size(); I != N; ++I)
156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      S.Diag(Diags[I].first, Diags[I].second);
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return;
158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  EnableIfAttr *EIA = new (S.getASTContext())
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                        EnableIfAttr(A->getLocation(), S.getASTContext(), Cond,
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     A->getMessage(),
163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     A->getSpellingListIndex());
164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  New->addAttr(EIA);
165651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
166651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1671d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
16823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            const Decl *Tmpl, Decl *New,
16923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LateInstantiatedAttrVec *LateAttrs,
17023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LocalInstantiationScope *OuterMostScope) {
171651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *TmplAttr : Tmpl->attrs()) {
1724ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
173f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
174f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Aligned && Aligned->isAlignmentDependent()) {
175f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
176f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      continue;
1774ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1784ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
179651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    const EnableIfAttr *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr);
180651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (EnableIf && EnableIf->getCond()->isValueDependent()) {
181651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl,
182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                       New);
183651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      continue;
184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
185651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
186f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    assert(!TmplAttr->isPackExpansion());
18723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
18823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
18923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
1906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      LocalInstantiationScope *Saved = nullptr;
19123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
19223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
19323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
19423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
195cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      // Allow 'this' within late-parsed attributes.
196cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      NamedDecl *ND = dyn_cast<NamedDecl>(New);
197cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXRecordDecl *ThisContext =
198cafeb948e6067b8dc897c441da522367917b06f9Richard Smith          dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
199cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
200cafeb948e6067b8dc897c441da522367917b06f9Richard Smith                                 ND && ND->isCXXInstanceMember());
201cafeb948e6067b8dc897c441da522367917b06f9Richard Smith
2025bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
2035bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
20431c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
20531c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
20623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
207d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
208d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
209d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
2104f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
2114f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
212b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
2134f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
2144f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
2154f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
21657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
21757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
21857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
21957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
22057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
22157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
22257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
22357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
2244f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
225b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
2264f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
2274f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
2283dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
2293dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
2303dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
2313dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
2323dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
2333dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
2340cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
2350cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
2363dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
2373dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
2383dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
2393dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
2403dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
2413dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
2423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
2433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
245a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
246561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
247836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
248ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
249ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
250ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
252a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
254b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
255b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
258b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // HACK: g++ has a bug where it gets the value kind of ?: wrong.
259b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // libstdc++ relies upon this bug in its implementation of common_type.
260b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // If we happen to be processing that implementation, fake up the g++ ?:
261b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // semantics. See LWG issue 2141 for more information on the bug.
262b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
263b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
264b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
265b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DT->isReferenceType() &&
266b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
267b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
268b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      D->getIdentifier() && D->getIdentifier()->isStr("type") &&
269b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
270b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    // Fold it to the (non-reference) type which g++ would have produced.
271b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    DI = SemaRef.Context.getTrivialTypeSourceInfo(
272b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DI->getType().getNonReferenceType());
273b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith
2748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
275162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
276162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
277162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
278162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
279162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
280162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
281162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
2828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
285cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
286cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
287cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
288cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
289c61361b102fcb9be7b64cc493fb797ea551eb8e7Douglas Gregor    if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
290cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
29183972f128e9218c051692bf96361327a701aeb79John McCall      assert(!newTag->hasNameForLinkage());
292162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
293cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
294d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
295a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
296ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
2977c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2987c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
299b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
3006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
301a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3025df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
3035df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
3045df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
3055df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
3065df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
307bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola    Typedef->setPreviousDecl(InstPrevTypedef);
3085126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
3095126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
3101d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
311d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
31246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
3158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
3168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
317162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
3183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
3193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
3203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
321162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
322162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
323162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
3243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
3253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
3263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
3273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
3283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
3303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
3313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
3323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
3333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
3343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
3363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
3373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
3386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3403e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
3413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TypeAliasTemplateDecl *PrevAliasTemplate = nullptr;
343ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
3443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
3453bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
3463bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
3473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
3483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
3493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
3513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
3523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
3536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3543e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3553e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
3563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
3583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
359bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola    Inst->setPreviousDecl(PrevAliasTemplate);
3603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
3623e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
3643e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
365a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3663e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
3673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
369162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
370162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
3713d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
372567f917df048d42732997a479b2b257403fc88efLarisse Voufo  return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false);
373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
375567f917df048d42732997a479b2b257403fc88efLarisse VoufoDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D,
376567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                             bool InstantiatingVarTemplate) {
377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3789901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3799901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3809901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3819901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3829901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
3849901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
385ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
386a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3870a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3880a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3890a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3900a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3923d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
393c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
394c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
395c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
3966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
397c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
398a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
399a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  DeclContext *DC = Owner;
400a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
401a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
402a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
403ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration.
404a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
4053d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
406ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                 DI->getType(), DI, D->getStorageClass());
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4089aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
4094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
4109aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
4119aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
4129aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
413ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
414ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
4156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
416bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
417a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
418567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                     StartingScope, InstantiatingVarTemplate);
4196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
4206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (D->isNRVOVariable()) {
4216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType();
4226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (SemaRef.isCopyElisionCandidate(ReturnType, Var, false))
4236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Var->setNRVOVariable(true);
4246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
4256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
4266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Var->setImplicit(D->isImplicit());
4276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
4283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4303d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4316206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4326206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4336206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4346206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4356206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4366206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4376206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4386206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4398dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
441a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
442561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
443836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
44407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
44507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
44607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
447a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
44807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
44907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4518dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4568dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
45707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
460b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
461b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    BitWidth = nullptr;
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
468f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
469f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
470f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
473ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      BitWidth = nullptr;
4778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
478ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      BitWidth = InstantiatedBitWidth.getAs<Expr>();
4798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4808dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
48107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
48207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
487ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
488703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4898dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                            nullptr);
491663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
492663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
4936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
494663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
497a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
498be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
499be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
500be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
501f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
502f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
504f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
505f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
506f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
507a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
5089901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5099901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5107a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
5119901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
51546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
516f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
52176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCallDecl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
52276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  bool Invalid = false;
52376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
52476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
52576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (DI->getType()->isVariablyModifiedType()) {
52676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
527651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      << D;
52876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Invalid = true;
52976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else if (DI->getType()->isInstantiationDependentType())  {
53076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
53176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           D->getLocation(), D->getDeclName());
53276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    if (!DI) {
53376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      DI = D->getTypeSourceInfo();
53476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
53576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    } else if (DI->getType()->isFunctionType()) {
53676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      // C++ [temp.arg.type]p3:
53776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   If a declaration acquires a function type through a type
53876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   dependent on a template-parameter and this causes a
53976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   declaration that does not use the syntactic form of a
54076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   function declarator to have function type, the program is
54176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   ill-formed.
54276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
54376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      << DI->getType();
54476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
54576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    }
54676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else {
54776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
54876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  }
54976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
550651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  MSPropertyDecl *Property = MSPropertyDecl::Create(
551651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(),
552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      DI, D->getLocStart(), D->getGetterId(), D->getSetterId());
55376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
55476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
55576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           StartingScope);
55676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
55776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (Invalid)
55876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Property->setInvalidDecl();
55976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Property->setAccess(D->getAccess());
56176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Owner->addDecl(Property);
56276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  return Property;
56476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall}
56576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
56687c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
56787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
56887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
56987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
57087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
571651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *PI : D->chain()) {
572651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI,
573b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
574b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
5756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
576a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
577b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
578b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
57987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
58040e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
58187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
58287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
58340e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
58487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
58587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
58687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
58787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
58887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
58987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
59087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
59187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
59287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
59302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
59402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
59506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
59632f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5974fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5984fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5994fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
6004fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
6014fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
6024fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
6034fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
6044fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
6054fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
6064fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
6074fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
6084fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
6096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
610fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
611d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
6120216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
61306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
6146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
615a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
61606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
6179a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
61806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
61906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
620a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
621a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
62206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
62306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
62432f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
625af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
626af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
627af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
628af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
629af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
6306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!NewND) return nullptr;
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
633a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
63406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
6355fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
6369a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
63702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
63802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
639fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
640fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
6418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
6428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
644f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
645f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
646f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
6471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
649ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
6508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
6516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
6528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
653e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
6549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
655e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
656e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
657e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
6588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
6616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EnumDecl *PrevDecl = nullptr;
66238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
66338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
66438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
66538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
6666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!Prev) return nullptr;
66738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
66838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
66938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
670ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
67238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
673a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6741274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
675f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6761274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6771274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6781274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6791274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
680f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
681f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
682f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6831274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
684f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
685f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
686f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6871274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6881274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6891274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6901274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6911274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6921274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6935b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6945b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
695f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
69606c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
697651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Forward the mangling number from the template to the instantiated decl.
698651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D));
6996bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (SubstQualifier(D, Enum)) return nullptr;
70017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
701f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
7024ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
7034ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
7044ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
7054ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
7064ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
7074ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
7084ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
7094ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
7104ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
7114ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
7124ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
7134ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
7144ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
7154ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
7168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
717f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
718f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
719f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
72057907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  //
72157907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // DR1484 clarifies that enumeration definitions inside of a template
72257907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // declaration aren't considered entities that can be separately instantiated
72357907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // from the rest of the entity they are declared inside of.
72457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) {
72557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
7264ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
72757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  }
728f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
729f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
730f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
731f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
732f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
733f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
734f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
735f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
7361af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
7371af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
7381af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
7395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
7408dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EnumConstantDecl *LastEnumConst = nullptr;
742651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *EC : Pattern->enumerators()) {
7438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
744ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    ExprResult Value((Expr *)nullptr);
745ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
746f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
748f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
750ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
751ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
7528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
7548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
7558dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
756ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Value = nullptr;
7578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7688dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst);
7735b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7743b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
77517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
776d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
778a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
779f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
780f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
78196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
78296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
783651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst);
78496084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
789f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
790f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
7919ff2b421f352fe0a0769c0a2a75af922c147b878Dmitri Gribenko                        Enumerators,
7926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                        nullptr, nullptr);
7938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7956477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
796b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7976477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7986477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
799e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
802550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
803550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
8042a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
805e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
806ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
8086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
815c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
816c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
817c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
818c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
819c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
8206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
82293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
8236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CXXRecordDecl *PrevDecl = nullptr;
8246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ClassTemplateDecl *PrevClassTemplate = nullptr;
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
826ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
82737574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
8283bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
8293bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
83037574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
83137574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
83237574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
83337574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
83437574f55cd637340f651330f5cfda69742880d36Nick Lewycky
83593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
83693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
83793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
83893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
83993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
84093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
841c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
84293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
843c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
84493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
8456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (!DC) return nullptr;
84693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
84793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
84893ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
85193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
85293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
85393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
85493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
85593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
85693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
85793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
85893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
85993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
86093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
86193ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
86293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
86393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
864c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
86593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8661eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
867c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
8686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
86993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
87093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
871c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
87293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
873c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
874c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
875c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
876c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
877c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
878c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
879c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
880c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
881a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
882c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
883c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
884c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
885c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
886c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
887c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
888c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
889c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
8906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          if (cast<Decl>(DCParent)->isInStdNamespace())
891c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
892c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
893c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
894c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
89593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
89693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
89793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
89893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
89993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
900a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
901c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
902c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
9036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
904c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
905c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
906c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
907c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
90893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
90993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
91093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
911c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
912c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
91393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
9146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
91593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
91693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
91793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
918e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
91993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
920ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
921ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
922f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
923e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
924c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
925c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
926b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
927e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
92893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
92993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
93093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
931e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
932ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
93393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
934ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
935ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
936ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
937ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
938ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
93922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Inst->setObjectOfFriendDecl();
94093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
94193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
94293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
943e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
94437574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
94537574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
94693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
947a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
948f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
9493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
95024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
951ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
952259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
95393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9541b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9554c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9564c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
957e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
958259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
959a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9604c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9614c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9624c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9634c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9644c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
965e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
966d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
967d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
968d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
969d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
970d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
972d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
973d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
974bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola      if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
975d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
976d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
977d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
978e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
979e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
980e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
981d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9827974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9837974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
984ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
985a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
986ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
987ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
988ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
989ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
9903bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
9916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
992a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
993ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
9943bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
995ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
9966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
997a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
998d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
999d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
1000d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
1001d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
1002d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
10037974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
10047974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
1005ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
1006ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->getTemplatedDecl()->isStaticDataMember() &&
1007ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
1008ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1009ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template, which
1010ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will contain the instantiations of the template parameters.
1011ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
1012ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = D->getTemplateParameters();
1013ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1014ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
10156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1016ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1017ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *Pattern = D->getTemplatedDecl();
10186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VarTemplateDecl *PrevVarTemplate = nullptr;
1019ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1020ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Pattern->getPreviousDecl()) {
1021ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
1022ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Found.empty())
1023ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1024ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1025ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1026dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  VarDecl *VarInst =
1027567f917df048d42732997a479b2b257403fc88efLarisse Voufo      cast_or_null<VarDecl>(VisitVarDecl(Pattern,
1028567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                         /*InstantiatingVarTemplate=*/true));
1029ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1030ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext *DC = Owner;
1031ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1032ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *Inst = VarTemplateDecl::Create(
1033ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
1034651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      VarInst);
1035ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarInst->setDescribedVarTemplate(Inst);
1036651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Inst->setPreviousDecl(PrevVarTemplate);
1037ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1038ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Inst->setAccess(D->getAccess());
1039ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate)
1040ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setInstantiatedFromMemberTemplate(D);
1041ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1042ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (D->isOutOfLine()) {
1043ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1044ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
1045ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1046ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1047ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Owner->addDecl(Inst);
1048ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1049ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate) {
1050ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Queue up any out-of-line partial specializations of this member
1051ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template; the client will force their instantiation once
1052ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the enclosing class has been instantiated.
1053ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1054ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    D->getPartialSpecializations(PartialSpecs);
1055ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1056bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola      if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1057ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        OutOfLineVarPartialSpecs.push_back(
1058ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            std::make_pair(Inst, PartialSpecs[I]));
1059ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1060ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1061ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Inst;
1062ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1063ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1064ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1065ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *D) {
1066ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->isStaticDataMember() &&
1067ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
1068ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1069ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1070ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1071ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Lookup the already-instantiated declaration and return that.
1072ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1073ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(!Found.empty() && "Instantiation found nothing?");
1074ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1075ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1076ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(InstVarTemplate && "Instantiation did not find a variable template?");
1077ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1078ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplatePartialSpecializationDecl *Result =
1079ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1080ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return Result;
1081ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1082ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1083ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1084ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
10857974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
1086d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1087550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
1088550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
1089a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
1090550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
10912a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1092895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
1093d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1094d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
10966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1097a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
10986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  FunctionDecl *Instantiated = nullptr;
1099a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1100a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1101a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
1102a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
1103a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1104a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
1105a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
1106a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1107a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
11086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1109d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1111d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1112a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1113a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
111437d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1115a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1116a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1117e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1118b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1119b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1120e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1121e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1122e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1123b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1124a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1125a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1126b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
11271f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1128a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
11291f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
11301f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
11311f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
11321f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1133b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1134d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1135d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1136d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1137d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
11386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CXXRecordDecl *PrevDecl = nullptr;
1139d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1140d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1141ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
11427c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1143ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
11446c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
11456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!Prev) return nullptr;
11466c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
11476c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1148d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1149d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1151ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1152ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1153b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1154b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1155b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
11566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
1157b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1158d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1159eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1160eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1161eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1162eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1163eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1164d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1165f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1166d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
116702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
116802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
116922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  if (D->getFriendObjectKind())
117022050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Record->setObjectOfFriendDecl();
117102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
11729901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
117357907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isAnonymousStructOrUnion())
11749901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
117557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
117657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isLocalClass())
117757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1178d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
1179651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Forward the mangling number from the template to the instantiated decl.
1180651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SemaRef.Context.setManglingNumber(Record,
1181651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    SemaRef.Context.getManglingNumber(D));
1182651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
118317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
118457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
118557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // DR1484 clarifies that the members of a local class are instantiated as part
118657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // of the instantiation of their enclosing entity.
118757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isCompleteDefinition() && D->isLocalClass()) {
1188651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
1189651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             TSK_ImplicitInstantiation,
1190651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             /*Complain=*/true);
1191651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
1192651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    TSK_ImplicitInstantiation);
119357907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  }
1194d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1195d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1196d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
119771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
119871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
119971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
120071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
120171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
120271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
120371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
120471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
120571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1206bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1207bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1208bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1209bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1210bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1211bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1212bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1213bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1214bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1215651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return Context.getFunctionType(NewFunc->getReturnType(),
1216651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 NewFunc->getParamTypes(), NewEPI);
121771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
121871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
121902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
122002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
122102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
122202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
12237557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1224a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1225127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1226127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1227127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1228b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1229c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    void *InsertPos = nullptr;
12322c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1233ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      = FunctionTemplate->findSpecialization(Innermost, InsertPos);
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1235127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
12362c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
12372c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1238127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1240b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1241b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1242b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1243b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1244b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1245b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
12466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool MergeWithParentScope = (TemplateParams != nullptr) ||
1247b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1248a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
124979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12502a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
125364b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
125421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
125671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1257fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1258c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1259c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1260c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1261c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1262c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
12636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1264d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1265d325daa506338ab86f9dd468b48fd010673f49a6John McCall
126668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
1267a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // in the enclosing namespace; otherwise we need to find the instantiated
1268a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // context.
126968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
1270a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl()) {
127168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1272a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
1273a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (isFriend && QualifierLoc) {
1274d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1275c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1276d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
12776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!DC) return nullptr;
1278d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1279a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
12807c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1281d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
128268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
128302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1284ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1285635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
1286459ef03126f9f0420efb3355e3b2ed3c1fdfb38aRafael Espindola                           D->getCanonicalDecl()->getStorageClass(),
1287af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
128886c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1289de9ed71c696bee936a21323f61548164de0eda13Enea Zaffanella  Function->setRangeEnd(D->getSourceRange().getEnd());
1290b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1291d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1292d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1293d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1294c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1295c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1296b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1297a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
1298a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    Function->setLocalExternDecl();
1299a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
1300b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1301a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
1302b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1303b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1304b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1305b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1306b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1308e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1309c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  for (unsigned P = 0; P < Params.size(); ++P)
1310c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (Params[P])
1311c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      Params[P]->setOwningFunction(Function);
13124278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
131302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1314ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1315a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1316a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1317a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1318a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1319a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1320a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1321a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1322a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1323a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1324a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1325a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1326a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1327a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1328a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1329a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1330d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1331a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1332a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1333a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1334a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1335b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1336b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1337d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1338d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1339d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1340d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1341d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1342d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1343d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
134466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
134566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1346c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1347838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1348910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
1349c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.begin(),
1350c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.size()),
13516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                /*InsertPos=*/nullptr);
135280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
135380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
135480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
135580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
135680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
135780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
135880f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1359d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
136002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1361a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1362e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1363e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
13641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1365af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1366a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1367a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
1368a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      SemaRef, Function->getDeclName(), SourceLocation(),
1369a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
1370a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                             : Sema::LookupOrdinaryName,
1371a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
13726826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1373af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1374af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1375af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1376af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1377af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
137822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Function->setObjectOfFriendDecl();
1379af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1380af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1381af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1382af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1383e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1384e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
13856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1386af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1387af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1388af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1389af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1390af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1391af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
13926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      if (!Temp) return nullptr;
1393af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1394af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1395af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1396af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1397af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1398af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1399af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1400af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1401a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1402af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1403af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1404af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1405a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1406a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1407a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
14086826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1409a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1410a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1411a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1412a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1413a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
14146826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
14156826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1416a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1417a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
14186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous,
14192c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1420e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
142176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
142276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
142376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
142476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1425a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1426a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1427d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
142822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    PrincipalDecl->setObjectOfFriendDecl();
14291b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1430ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1431651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    bool QueuedInstantiation = false;
1432ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
1433651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // C++11 [temp.friend]p4 (DR329):
1434651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   When a function is defined in a friend function declaration in a class
1435651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   template, the function is instantiated when the function is odr-used.
1436651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   The same restrictions on multiple declarations and definitions that
1437651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   apply to non-template function declarations and definitions also apply
1438651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    //   to these implicit definitions.
1439651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (D->isThisDeclarationADefinition()) {
1440238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
14416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      const FunctionDecl *Definition = nullptr;
144210620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1443238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1444651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1445651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            << Function->getDeclName();
1446238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1447a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1448238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1449238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1450651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      else for (auto R : Function->redecls()) {
1451651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (R == Function)
145213a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1453651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1454651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // If some prior declaration of this function has been used, we need
1455651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // to instantiate its definition.
1456651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (!QueuedInstantiation && R->isUsed(false)) {
1457651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          if (MemberSpecializationInfo *MSInfo =
1458651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                  Function->getMemberSpecializationInfo()) {
1459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            if (MSInfo->getPointOfInstantiation().isInvalid()) {
1460651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              SourceLocation Loc = R->getLocation(); // FIXME
1461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              MSInfo->setPointOfInstantiation(Loc);
1462651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              SemaRef.PendingLocalImplicitInstantiations.push_back(
1463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                               std::make_pair(Function, Loc));
1464651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              QueuedInstantiation = true;
1465ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1466ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1467651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        }
1468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1469651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // If some prior declaration of this function was a friend with an
1470651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // uninstantiated definition, reject it.
1471651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (R->getFriendObjectKind()) {
1472651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          if (const FunctionDecl *RPattern =
1473651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                  R->getTemplateInstantiationPattern()) {
147410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
1475651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1476238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
14776a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1478238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1479238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1480651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          }
1481238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1482238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1483238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1484a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1485a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1486a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (Function->isLocalExternDecl() && !Function->getPreviousDecl())
1487a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1488a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
148976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
149076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
149176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
149276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1493eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1494e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1495e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14962dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1497d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1498d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1499af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1500af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
15016b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1502d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
15031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
15041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1505d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1506c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    void *InsertPos = nullptr;
15092c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1510ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      = FunctionTemplate->findSpecialization(Innermost, InsertPos);
15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15126b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
15132c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
15142c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
15156b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
15166b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1517b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1518b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1519b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1520b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1521b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1522b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool MergeWithParentScope = (TemplateParams != nullptr) ||
1524a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
152579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
15262a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
152748dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
15284eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
15295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
15304eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
15314eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
15324eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
15334eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
15344eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
15354eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
15364eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
15376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
15384eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
15394eab39f0745fb1949dbb40c4145771b927888242John McCall    }
15404eab39f0745fb1949dbb40c4145771b927888242John McCall  }
15414eab39f0745fb1949dbb40c4145771b927888242John McCall
15425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1543dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
154421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
15456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
154671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
15472dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1548c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1549c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1550c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1551b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1552a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
15536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1554b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1555b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1556b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1557b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1558c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1559b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1560c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1561b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1562c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1563c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
15646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
1565b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1566b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1567b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1568b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1569b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
15706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    if (!DC) return nullptr;
1571b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1572b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1574b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
15756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CXXMethodDecl *Method = nullptr;
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15782577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15792577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
158017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1582ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
158416573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
158586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
1586b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
15874841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // Claim that the instantiation of a constructor or constructor template
15884841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // inherits the same constructor that the template does.
1589b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1590b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            Constructor->getInheritedConstructor())) {
1591b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // If we're instantiating a specialization of a function template, our
1592b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // "inherited constructor" will actually itself be a function template.
1593b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // Instantiate a declaration of it, too.
1594b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      if (FunctionTemplate) {
1595b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1596b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               !Inh->getParent()->isDependentContext() &&
1597b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               "inheriting constructor template in dependent context?");
1598b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1599b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith                                         Inh);
1600d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker        if (Inst.isInvalid())
16016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
1602b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1603b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        LocalInstantiationScope LocalScope(SemaRef);
1604b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
1605b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // Use the same template arguments that we deduced for the inheriting
1606b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // constructor. There's no way they could be deduced differently.
1607b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        MultiLevelTemplateArgumentList InheritedArgs;
1608b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1609b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Inh = cast_or_null<CXXConstructorDecl>(
1610b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1611b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (!Inh)
16126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
1613b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      }
16144841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
1615b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    }
161617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
161717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1618ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
16192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
162016573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
162165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
162265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1623ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
16240130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1625f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
162686c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
16279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1628dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
162972fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola    StorageClass SC = D->isStatic() ? SC_Static : SC_None;
16302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1631ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
163272fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola                                   SC, D->isInlineSpecified(),
163386c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1634dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16356b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1636d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1637d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1638d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1639c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1640c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1641b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1642d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1643d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1644d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
16451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1646d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1647d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1648d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1649d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1650d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1651d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1652d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1653d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1654d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1655d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1656d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1657d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1659d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1660b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1661b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
166222050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith      FunctionTemplate->setObjectOfFriendDecl();
1663b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1665d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
166666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
166766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1668c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1669838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1670910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1671c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.begin(),
1672c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.size()),
16736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                              /*InsertPos=*/nullptr);
1674b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
167566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
16762db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
167766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1678a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
16807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
16817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1682b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
16834eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
16844eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
16854eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
16864eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
16874eab39f0745fb1949dbb40c4145771b927888242John McCall
1688b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
168922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Method->setObjectOfFriendDecl();
1690b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
16917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16935545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
16945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
16955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
16964278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
17002dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
17012577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
17022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
17031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1704b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1705b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1707dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1708dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1709dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1710dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
17116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
17126826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1713dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
17142dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1715af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
17166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, false);
171765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
17184ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
17194ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
17204ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
17211f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
17221f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
17231f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
17241f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
17251f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
17261f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
17271f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
17281f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
17291f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
173046460a68f6508775e98c19b4bb8454bb471aac24John McCall
17319eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
17329eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
17333bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1734ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1735ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
17363bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1737ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
17383bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
17391f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1740b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
17411f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17421f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17431f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1744b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
17451f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17461f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17471f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
17481f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
17491f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
17501f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
17511f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
17521f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
17531f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17541f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
17551f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17561f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
17571f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
17581f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
17591f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
17601f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1761b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1762eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
17632dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
17642dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17652dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1766615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1767dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1768615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1769615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
177003b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
177117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
177203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
177303b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1774bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
177565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1776bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1777bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
1778ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
177966874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
178066874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
17812dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17822dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1783e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1784e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1785e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
17864fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1789344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1790344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
17914fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
17924fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1793e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1794e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
17959a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1796a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17979d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
17989d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TypeSourceInfo *InstantiatedDefaultArg =
17999d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs,
18009d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                          D->getDefaultArgumentLoc(), D->getDeclName());
18019d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (InstantiatedDefaultArg)
18029d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Inst->setDefaultArgument(InstantiatedDefaultArg, false);
18039d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1804e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1805a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1806550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1807550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1808a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1809e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1810e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1811e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
181233642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
181333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
181433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
18165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
18175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
18186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1819a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
182033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
182133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
18226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
18236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1824a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
18256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
18266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
18286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
18296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
18306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1831a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
18326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
18336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
18346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
1835a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
18406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
18416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
18426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1843a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
18466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
18476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
18486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
18496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
185139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
18526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
18535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
18546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1855a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
18576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
18586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
18596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1860dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
18616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1862dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
18636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
18646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1865a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
18666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1867a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
18686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
18696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1870a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
18726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
18736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
18746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1875a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
18766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
18776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
18786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
1879a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
18826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
18836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
18856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
18866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
18876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
18906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
18916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
18926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
18936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
18946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
18966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
18976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
18986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
18996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1900a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
19016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
19026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
19036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
1904a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
19066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
19076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
19086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
1909a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
19116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
19126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
19136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1914a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
19156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
19166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
19176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
1918a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1920a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
19216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
19226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
19236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
19246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
19256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
192633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1927a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
19296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1930a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1931ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1932ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1933a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1934a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
19356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
19366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
19376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
19386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
19396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
19406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1941a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1942ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
19436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1944a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1945a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1946a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
19476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1948a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19499a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
195033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
195133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1952a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19539d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
19549d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs);
19559d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!Value.isInvalid())
19569d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(Value.get(), false);
19579d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1958a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1959a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1960550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1961550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
196233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
196333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
196433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
19656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
19666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
19706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
19716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
19746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
19756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
19766964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
19776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
19786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
19796964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
19806964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
19816964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19820dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
19839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
19849106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
19859106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
19869106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
19879106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
19886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
19896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
19916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
19936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
19946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
19956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
19966964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
19976964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
19986964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
19996964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
20006964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
20016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
20026964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
20036bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
20046964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
20056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
20066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
20086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
20096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
20106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
20116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
20126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
20136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
20146964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
20156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
20166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
20186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
20196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
20206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
2021dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
20226964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
20236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
20246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
20256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
20266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
20276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
20286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
20296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
20316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
20326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
20336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
20346964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
20356964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
20366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
20376964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
20386964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
20396964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20406964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
20416964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
20426964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
20436964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
20446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
20456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
20466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
20476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
20486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
20496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
20516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
20526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
20536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
20546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
20556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
20569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
20579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
20582a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
20599106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
20609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
20616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
2062a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
2063a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
20656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
20666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
20676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
20696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
20706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
20726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
20736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
20746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
2076a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
20776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
20796964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
20809d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
20819d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    NestedNameSpecifierLoc QualifierLoc =
20829d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateQualifierLoc();
20839d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    QualifierLoc =
20849d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
20859d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TemplateName TName = SemaRef.SubstTemplateName(
20869d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
20879d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
20889d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!TName.isNull())
20899d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(
20909d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          TemplateArgumentLoc(TemplateArgument(TName),
20919d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateQualifierLoc(),
20929d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateNameLoc()),
20939d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          false);
20949d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
20959a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
2096a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2097a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
20989106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
20999106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2100a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
21019106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
21029106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
21039106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
210448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2105db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
2106db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
2107a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
210848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
210948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2110a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
2111db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
2112a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
2113a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
211448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
2115536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
2116536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
2117536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
2118536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
2119536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2120536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
212148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
212248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
212348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2124ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
21251b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
21261b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
21271b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
21281b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
21291b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
21301b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
21311b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
21321b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
21331b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
21345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
21355149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21365149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21375149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
21391b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
21401b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
21411b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2142ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2143ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21449f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
21459f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
21469f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
21479f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
21489f54ad4381370c6b771424b53d219e661d6d6706John McCall
2149ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2150ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
21519f54ad4381370c6b771424b53d219e661d6d6706John McCall
2152ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
21538d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->getUsingLoc(),
21545149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2155ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
21568d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->hasTypename());
2157ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21585149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
21595149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21609f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
21619f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
21629f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
21639f54ad4381370c6b771424b53d219e661d6d6706John McCall
21649f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
21658d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
21668d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                            D->hasTypename(), SS,
21679f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
21689f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
21699f54ad4381370c6b771424b53d219e661d6d6706John McCall
21709f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
21719f54ad4381370c6b771424b53d219e661d6d6706John McCall
21729f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
2173651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS, NameInfo,
2174ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2175ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
21769f54ad4381370c6b771424b53d219e661d6d6706John McCall
2177ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2178ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2179ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2180ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21819f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
21829f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
21839f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
21849f54ad4381370c6b771424b53d219e661d6d6706John McCall
2185c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
21866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SemaRef.CheckInheritingConstructorUsingDecl(NewUD);
2187c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2188c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2189c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2190323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2191323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
21929f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
2193651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *Shadow : D->shadows()) {
21949f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2195f06a2893bc9778857295c64ee32b4a899a338480Richard Smith        cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2196f06a2893bc9778857295c64ee32b4a899a338480Richard Smith            Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs));
2197b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
21986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
21999f54ad4381370c6b771424b53d219e661d6d6706John McCall
22006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    UsingShadowDecl *PrevDecl = nullptr;
2201f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    if (CheckRedeclaration) {
2202f06a2893bc9778857295c64ee32b4a899a338480Richard Smith      if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl))
2203f06a2893bc9778857295c64ee32b4a899a338480Richard Smith        continue;
2204f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    } else if (UsingShadowDecl *OldPrev = Shadow->getPreviousDecl()) {
2205f06a2893bc9778857295c64ee32b4a899a338480Richard Smith      PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl(
2206f06a2893bc9778857295c64ee32b4a899a338480Richard Smith          Shadow->getLocation(), OldPrev, TemplateArgs));
2207f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    }
22089f54ad4381370c6b771424b53d219e661d6d6706John McCall
2209f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    UsingShadowDecl *InstShadow =
22106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget,
22116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                     PrevDecl);
22129f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2213323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2214323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2215323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
22169f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2217ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2218ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2219ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2220ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2221ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
22229f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
22236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
2224ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2225ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
22277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
22285149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2229a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
22305149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
22315149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
22326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
22337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
22355149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
22367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2237ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2238accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2239ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
22407ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
22416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(),
22426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                  D->getUsingLoc(), SS, NameInfo, nullptr,
22437ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22447ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
22454469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2246ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2247ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
22497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
22507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
22527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
22535149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
22545149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
22555149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
22566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2257a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
22580dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
22595149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2261ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2262ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2263ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
22641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
22656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(),
22666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                  D->getUsingLoc(), SS, NameInfo, nullptr,
22677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
22694469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2270ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2271ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
22730dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
22740dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2275af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2276af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2277af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2278af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
22796b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
22806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                                nullptr, true));
2281af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2282af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2283af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2284af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
22856b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
22866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  TemplateArgumentListInfo *TemplateArgsPtr = nullptr;
22876b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
22886b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
22896b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
22906b02009359a462ffe633696a4441313b462e6566Nico Weber  }
22916b02009359a462ffe633696a4441313b462e6566Nico Weber
2292af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
22936b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
22946b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2295af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2296af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2297af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2298af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2299af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2300af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2301af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2302af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2303af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2304af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2305af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2306af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2307c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey BataevDecl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2308c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                     OMPThreadPrivateDecl *D) {
23096af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  SmallVector<Expr *, 5> Vars;
2310651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto *I : D->varlists()) {
2311ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get();
2312c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
23136af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev    Vars.push_back(Var);
2314c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  }
2315c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2316c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OMPThreadPrivateDecl *TD =
2317c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2318c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2319651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  TD->setAccess(AS_public);
2320651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Owner->addDecl(TD);
2321651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2322c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  return TD;
2323c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev}
2324c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2325ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
23266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return VisitFunctionDecl(D, nullptr);
2327ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2328ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2329ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
23306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return VisitCXXMethodDecl(D, nullptr);
2331ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2332ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2333ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2334ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("There are only CXXRecordDecls in C++");
2335ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2336ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2337ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *
2338ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanTemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2339ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    ClassTemplateSpecializationDecl *D) {
2340b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // As a MS extension, we permit class-scope explicit specialization
2341b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // of member class templates.
2342b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
2343b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  assert(ClassTemplate->getDeclContext()->isRecord() &&
2344b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling         D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2345b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling         "can only instantiate an explicit specialization "
2346b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling         "for a member class template");
2347b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2348b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Lookup the already-instantiated declaration in the instantiation
2349b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // of the class template. FIXME: Diagnose or assert if this fails?
2350b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  DeclContext::lookup_result Found
2351b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    = Owner->lookup(ClassTemplate->getDeclName());
2352b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (Found.empty())
23536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2354b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  ClassTemplateDecl *InstClassTemplate
2355b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    = dyn_cast<ClassTemplateDecl>(Found.front());
2356b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (!InstClassTemplate)
23576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2358b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2359b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Substitute into the template arguments of the class template explicit
2360b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // specialization.
2361b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc().
2362b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                        castAs<TemplateSpecializationTypeLoc>();
2363b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(),
2364b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                            Loc.getRAngleLoc());
2365b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  SmallVector<TemplateArgumentLoc, 4> ArgLocs;
2366b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  for (unsigned I = 0; I != Loc.getNumArgs(); ++I)
2367b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    ArgLocs.push_back(Loc.getArgLoc(I));
2368b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(),
2369b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                    InstTemplateArgs, TemplateArgs))
23706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2371b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2372b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Check that the template argument list is well-formed for this
2373b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // class template.
2374b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  SmallVector<TemplateArgument, 4> Converted;
2375b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (SemaRef.CheckTemplateArgumentList(InstClassTemplate,
2376b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                        D->getLocation(),
2377b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                        InstTemplateArgs,
2378b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                        false,
2379b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                        Converted))
23806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2381b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2382b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Figure out where to insert this class template explicit specialization
2383b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // in the member template's set of class template explicit specializations.
23846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void *InsertPos = nullptr;
2385b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  ClassTemplateSpecializationDecl *PrevDecl =
2386ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      InstClassTemplate->findSpecialization(Converted, InsertPos);
2387b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2388b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Check whether we've already seen a conflicting instantiation of this
2389b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // declaration (for instance, if there was a prior implicit instantiation).
2390b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  bool Ignored;
2391b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (PrevDecl &&
2392b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(),
2393b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                                     D->getSpecializationKind(),
2394b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                                     PrevDecl,
2395b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                                     PrevDecl->getSpecializationKind(),
2396b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                                     PrevDecl->getPointOfInstantiation(),
2397b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                                     Ignored))
23986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2399b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2400b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // If PrevDecl was a definition and D is also a definition, diagnose.
2401b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // This happens in cases like:
2402b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //
2403b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //   template<typename T, typename U>
2404b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //   struct Outer {
2405b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //     template<typename X> struct Inner;
2406b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //     template<> struct Inner<T> {};
2407b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //     template<> struct Inner<U> {};
2408b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //   };
2409b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //
2410b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //   Outer<int, int> outer; // error: the explicit specializations of Inner
2411b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  //                          // have the same signature.
2412b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (PrevDecl && PrevDecl->getDefinition() &&
2413b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      D->isThisDeclarationADefinition()) {
2414b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl;
2415b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    SemaRef.Diag(PrevDecl->getDefinition()->getLocation(),
2416b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                 diag::note_previous_definition);
24176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2418b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  }
2419b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2420b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Create the class template partial specialization declaration.
2421b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  ClassTemplateSpecializationDecl *InstD
2422b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    = ClassTemplateSpecializationDecl::Create(SemaRef.Context,
2423b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              D->getTagKind(),
2424b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              Owner,
2425b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              D->getLocStart(),
2426b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              D->getLocation(),
2427b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              InstClassTemplate,
2428b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              Converted.data(),
2429b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              Converted.size(),
2430b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                                              PrevDecl);
2431b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2432b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Add this partial specialization to the set of class template partial
2433b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // specializations.
2434b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (!PrevDecl)
2435b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling    InstClassTemplate->AddSpecialization(InstD, InsertPos);
2436b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2437b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Substitute the nested name specifier, if any.
2438b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (SubstQualifier(D, InstD))
24396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2440b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2441b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Build the canonical type that describes the converted template
2442b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // arguments of the class template explicit specialization.
2443b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2444b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      TemplateName(InstClassTemplate), Converted.data(), Converted.size(),
2445b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      SemaRef.Context.getRecordType(InstD));
2446b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2447b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Build the fully-sugared type for this class template
2448b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // specialization as the user wrote in the specialization
2449b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // itself. This means that we'll pretty-print the type retrieved
2450b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // from the specialization's declaration the way that the user
2451b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // actually wrote the specialization, rather than formatting the
2452b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // name based on the "canonical" representation used to store the
2453b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // template arguments in the specialization.
2454b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2455b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs,
2456b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      CanonType);
2457b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2458b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setAccess(D->getAccess());
2459b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
2460b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setSpecializationKind(D->getSpecializationKind());
2461b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setTypeAsWritten(WrittenTy);
2462b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setExternLoc(D->getExternLoc());
2463b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc());
2464b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2465b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  Owner->addDecl(InstD);
2466b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2467b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // Instantiate the members of the class-scope explicit specialization eagerly.
2468b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // We don't have support for lazy instantiation of an explicit specialization
2469b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  // yet, and MSVC eagerly instantiates in this case.
2470b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  if (D->isThisDeclarationADefinition() &&
2471b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling      SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs,
2472b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                               TSK_ImplicitInstantiation,
2473b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling                               /*Complain=*/true))
24746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2475b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling
2476b3617be53923ac3e5885ff5001aa748bd0223102Bill Wendling  return InstD;
2477ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2478ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2479ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2480ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *D) {
2481ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2482ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgumentListInfo VarTemplateArgsInfo;
2483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2484ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(VarTemplate &&
2485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "A template specialization without specialized template?");
2486ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2487ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the current template arguments.
2488ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2489ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2490ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2491ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2492ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2493ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
24946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2495ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2496ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this template.
2497ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2498ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(
2499ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, VarTemplate->getLocStart(),
2500ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2501651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          Converted))
25026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2503ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2504ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Find the variable template specialization declaration that
2505ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // corresponds to these arguments.
25066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void *InsertPos = nullptr;
2507ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2508ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          Converted, InsertPos))
2509ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a variable template specialization, return it.
2510ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return VarSpec;
2511ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2512ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2513ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                            VarTemplateArgsInfo, Converted);
2514ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2515ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2516ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2517ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2518ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
2519ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    ArrayRef<TemplateArgument> Converted) {
2520ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2521ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is the variable for an anonymous struct or union,
2522ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiate the anonymous struct/union type first.
2523ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2524ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2525ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
25266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
2527ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2528ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2529ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
2530ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2531ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                        D->getTypeSpecStartLoc(), D->getDeclName());
2532ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
25336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2534ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2535ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2536ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2537ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << D->isStaticDataMember() << DI->getType();
25386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2539ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2540ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2541ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration
2542ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2543ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2544ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2545ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Converted.size());
2546ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Var->setTemplateArgsInfo(TemplateArgsInfo);
2547d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (InsertPos)
2548d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    VarTemplate->AddSpecialization(Var, InsertPos);
2549ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2550ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2551ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
25526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2553ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2554ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
2555a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     Owner, StartingScope);
2556ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2557ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Var;
2558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2559ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2560ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2561ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("@defs is not supported in Objective-C++");
2562ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2563ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2564ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2565ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  // FIXME: We need to be able to instantiate FriendTemplateDecls.
2566ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2567ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               DiagnosticsEngine::Error,
2568ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               "cannot instantiate %0 yet");
2569ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  SemaRef.Diag(D->getLocation(), DiagID)
2570ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    << D->getDeclKindName();
2571ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
25726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
2573ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2574ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2575ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2576ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Unexpected decl");
2577ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2578ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2579ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2580d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
25817e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
25822fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
25836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
25842fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
25858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
25868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
25878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2588e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2589e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2590e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2591e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2592e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2593e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2594e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2595ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2596e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2597e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2598e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2599e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
26005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2601e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2602e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2603e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2604e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2605bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2606e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
26079148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2608e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2609e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2610e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2611ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
26126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2613e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2614e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2615e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2616e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2617e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2618e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
26191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2620e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2621a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2622ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2623ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2624ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2625ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2626ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2627a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2628ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2629ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2630d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2631d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2632d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2633ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2636550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2637550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2638550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
26392a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2640a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2643ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2644ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2645ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
26466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2647a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2648ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2649ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2650c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2651c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2652c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2653c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2654c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2655c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2656e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
26576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2658a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2660ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
26615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2662a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2663ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2664a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2665ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2666ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
26676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2668ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2669ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2670ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
26716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void *InsertPos = nullptr;
2672ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2673ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    = ClassTemplate->findPartialSpecialization(Converted, InsertPos);
2674a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2675ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2676ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2677a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2678ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2679910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2680910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2681ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2684ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2685ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2688ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
26893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
26903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
26913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
26923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2693d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2694ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2695a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2696ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2697ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2698ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2699ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2700ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2701ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2702ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2703ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2704ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2705ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2706ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2707ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2708ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2709ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2710ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2711ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2712ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2713d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2714ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2715ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
27166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2717ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2718a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2719a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2720ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2722a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
272313c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2724a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2725ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2726ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2727ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2728a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2729910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2730910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2731d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
27323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
27336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                     nullptr);
2734b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2735b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
27366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2737b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
27394469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2740a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2741ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2742ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
27436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ClassTemplate->AddPartialSpecialization(InstPartialSpec,
27446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          /*InsertPos=*/nullptr);
2745d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2748ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the declaration of a variable template partial
2749ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization.
2750ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2751ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param VarTemplate the (instantiated) variable template that is partially
2752ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialized by the instantiation of \p PartialSpec.
2753ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2754ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param PartialSpec the (uninstantiated) variable template partial
2755ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization that we are instantiating.
2756ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2757ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \returns The instantiated partial specialization, if successful; otherwise,
2758ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// NULL to indicate an error.
2759ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplatePartialSpecializationDecl *
2760ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoTemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2761ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate,
2762ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *PartialSpec) {
2763ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template partial
2764ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization, which will contain the instantiations of the template
2765ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // parameters.
2766ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
2767ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2768ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template parameters of the variable template partial
2769ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2770ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2771ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2772ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
27736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2774ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2775ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template arguments of the variable template partial
2776ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2777c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2778c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2779c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2780c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2781c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2782c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2783ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    InstTemplateArgs, TemplateArgs))
27846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2785ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2786ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this
2787ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // class template.
2788ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2789ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2790ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        InstTemplateArgs, false, Converted))
27916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2792ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2793ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Figure out where to insert this variable template partial specialization
2794ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // in the member template's set of variable template partial specializations.
27956bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void *InsertPos = nullptr;
2796ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *PrevDecl =
2797ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      VarTemplate->findPartialSpecialization(Converted, InsertPos);
2798ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2799ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the canonical type that describes the converted template
2800ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // arguments of the variable template partial specialization.
2801ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2802ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), Converted.data(), Converted.size());
2803ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2804ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the fully-sugared type for this variable template
2805ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization as the user wrote in the specialization
2806ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // itself. This means that we'll pretty-print the type retrieved
2807ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // from the specialization's declaration the way that the user
2808ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // actually wrote the specialization, rather than formatting the
2809ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // name based on the "canonical" representation used to store the
2810ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // template arguments in the specialization.
2811ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2812ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2813ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CanonType);
2814ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2815ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (PrevDecl) {
2816ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We've already seen a partial specialization with the same template
2817ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // parameters and template arguments. This can happen, for example, when
2818ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // substituting the outer template arguments ends up causing two
2819ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template partial specializations of a member variable template
2820ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // to have identical forms, e.g.,
2821ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2822ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   template<typename T, typename U>
2823ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   struct Outer {
2824ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename X, typename Y> pair<X,Y> p;
2825ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<T, Y> p;
2826ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<U, Y> p;
2827ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   };
2828ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2829ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   Outer<int, int> outer; // error: the partial specializations of Inner
2830ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //                          // have the same signature.
2831ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2832ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_var_partial_spec_redeclared)
2833ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << WrittenTy->getType();
2834ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PrevDecl->getLocation(),
2835ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::note_var_prev_partial_spec_here);
28366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2837ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2838ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2839ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2840ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI = SemaRef.SubstType(
2841ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSourceInfo(), TemplateArgs,
2842ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2843ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
28446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2845ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2846ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2847ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2848ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_variable_instantiates_to_function)
2849ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << PartialSpec->isStaticDataMember() << DI->getType();
28506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2851ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2852ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2853ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create the variable template partial specialization declaration.
2854ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplatePartialSpecializationDecl *InstPartialSpec =
2855ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplatePartialSpecializationDecl::Create(
2856ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2857ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2858ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          DI, PartialSpec->getStorageClass(), Converted.data(),
285937fd27dbb941d27f4bd7412e534e7e5089d6781bRichard Smith          Converted.size(), InstTemplateArgs);
2860ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2861ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2862ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(PartialSpec, InstPartialSpec))
28636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
2864ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2865ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2866ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setTypeAsWritten(WrittenTy);
2867ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2868ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Add this partial specialization to the set of variable template partial
2869ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specializations. The instantiation of the initializer is not necessary.
28706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr);
287104592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
287204592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs,
2873a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     LateAttrs, Owner, StartingScope);
287404592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
2875ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstPartialSpec;
2876ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2877ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
287821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
287921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
28805f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
288121ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
288221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
288321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
28846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
28856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CXXRecordDecl *ThisContext = nullptr;
2886cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2887cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2888cafeb948e6067b8dc897c441da522367917b06f9Richard Smith    ThisContext = cast<CXXRecordDecl>(Owner);
2889cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2890cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2891cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
28926cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
28936cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
28946cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2895cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2896cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
289721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
28986bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
28995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2900c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2901c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2902c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (NewTInfo != OldTInfo) {
2903c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // Get parameters from the new type info.
2904140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
290539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2906500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
2907651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
290812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
2909651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
2910500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2911500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2912dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2913500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2914500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2915500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2916500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2917500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2918a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
291912c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
2920651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++);
292112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2922500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2923500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2924500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2925500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2926500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
2927651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++);
2928500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2929500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2930500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
293112c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
29326920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2933c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    } else {
2934c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // The function type itself was not dependent and therefore no
2935c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // substitution occurred. However, we still need to instantiate
2936c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // the function parameters themselves.
2937c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      const FunctionProtoType *OldProto =
2938c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          cast<FunctionProtoType>(OldProtoLoc.getType());
2939651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end;
2940651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines           ++i) {
2941651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        ParmVarDecl *OldParam = OldProtoLoc.getParam(i);
2942c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        if (!OldParam) {
2943c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
2944651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines              D, D->getLocation(), OldProto->getParamType(i)));
2945c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          continue;
2946c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        }
2947c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
2948ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman        ParmVarDecl *Parm =
2949c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner            cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
29506920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
29516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
29526920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
29536920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2954cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2955c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  } else {
2956c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // If the type of this function, after ignoring parentheses, is not
2957c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // *directly* a function type, then we're instantiating a function that
2958c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // was declared via a typedef or with attributes, e.g.,
2959c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2960c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   typedef int functype(int, int);
2961c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   functype func;
2962c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   int __cdecl meth(int, int);
2963c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2964c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // In this case, we'll just go instantiate the ParmVarDecls that we
2965c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // synthesized in the method declaration.
2966c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    SmallVector<QualType, 4> ParamTypes;
2967c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
2968c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               D->getNumParams(), TemplateArgs, ParamTypes,
2969c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               &Params))
29706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
2971cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
2972c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
297321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
29745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
29755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2976e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2977e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2978e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2979e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2980e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2981e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2982e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2983e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2984e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2985e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2986e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2987e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2988e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2989e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2990651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // If the parameter's type is not dependent, update it to match the type
2991651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // in the pattern. They can differ in top-level cv-qualifiers, and we want
2992651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // the pattern's type here. If the type is dependent, they can't differ,
2993651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // per core issue 1668.
2994651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // FIXME: Updating the type to work around this is at best fragile.
2995651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!PatternDecl->getType()->isDependentType())
2996651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        FunctionParam->setType(PatternParam->getType());
2997651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
2998e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2999e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
3000e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
3001e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
3002e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
3003e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3004e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
3005e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
3006dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
3007e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
3008500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
3009500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
3010500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
3011e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
3012651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (!PatternDecl->getType()->isDependentType())
3013651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        FunctionParam->setType(PatternParam->getType());
3014651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3015e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
3016e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
3017e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
3018e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
3019e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
3020e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
3021e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3022e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
3023e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
3024e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
302513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
302613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
3027e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
3028e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
3029e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
3030e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
3031e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
3032e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
30336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  CXXRecordDecl *ThisContext = nullptr;
3034e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
3035e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
3036e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
3037e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
3038e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
3039e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
304080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
3041e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3042e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
3043e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
3044e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
3045e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
3046e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
3047e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
3048e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
3049e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
3050e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
3051e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
3052e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
3053e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
3054e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
3055e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3056e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
3057e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
3058cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
3059e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
3060e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
3061e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
3062e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
3063e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
3064e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
3065e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
3066e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
3067e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3068e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
3069e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
3070e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
3071e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
3072e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
3073e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
3074e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
3075e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
3076e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
3077e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
3078e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3079e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
3080e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
3081e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
3082e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
3083e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3084e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
3085e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
3086e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
3087e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
3088e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3089e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
3090e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
3091e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
3092e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
3093e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
3094e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
3095e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
3096e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3097e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
3098e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
3099e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3100e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
3101e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
3102e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3103e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
3104e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
3105e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3106e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
3107e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
3108e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
3109e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
3110e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
3111e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
3112e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3113e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
3114e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
31156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Expr *NoexceptExpr = nullptr;
3116e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
3117e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
3118e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
3119e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
3120e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
3121e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
3122e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3123e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
3124ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      NoexceptExpr = E.get();
3125e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
3126e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
3127ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
3128ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
31296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines              nullptr, diag::err_noexcept_needs_constant_expression,
3130ef8225444452a1486bd721f3285301fe84643b00Stephen Hines              /*AllowFold*/ false).get();
3131e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
3132e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
3133e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  FunctionProtoType::ExtProtoInfo EPI;
3135e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
3136e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
3137e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
3138e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
3139e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SemaRef.UpdateExceptionSpec(New, EPI);
3141e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
3142e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3143e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
3144e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
314513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
314613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
3147e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
3148e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3149e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
3150e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
3151d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid()) {
3152b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
3153b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
3154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    FunctionProtoType::ExtProtoInfo EPI;
3155b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
3156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    UpdateExceptionSpec(Decl, EPI);
3157e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
3158b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
3159e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3160e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
3161e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
3162e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
3163e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
3164e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3165e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
31666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true);
3167e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
316813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
316913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
3170e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
317113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
317213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
317313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
3174e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
3175e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
31761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
3177e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
3178e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
3179e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
31801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
31811eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
3182e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
318385f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
318410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
31851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3186651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Forward the mangling number from the template to the instantiated decl.
3187651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SemaRef.Context.setManglingNumber(New,
3188651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                    SemaRef.Context.getManglingNumber(Tmpl));
3189651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3190cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
3191cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
3192cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
31931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
31941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
3195cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
3196cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
3197cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
3198cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
3199cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
3200cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
3201cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
32021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
32034a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
32041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
3205cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
3206bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
3207cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
32084a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
3209cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
3210cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
32111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32120ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
32130ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
32140ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
321560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
3216e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
3217e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3218e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
3219e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
322080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
3221e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
3222e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
3223e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
322413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
322513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
322613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
32274841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      ExceptionSpecificationType NewEST = EST_Uninstantiated;
32284841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      if (EPI.ExceptionSpecType == EST_Unevaluated)
32294841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith        NewEST = EST_Unevaluated;
323013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
3231e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
3232e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
3233e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
3234e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
3235e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
32364841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      EPI.ExceptionSpecType = NewEST;
3237e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
323813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
32390567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      New->setType(SemaRef.Context.getFunctionType(
3240651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          NewProto->getReturnType(), NewProto->getParamTypes(), EPI));
3241e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
3242e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
3243e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
32440ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
32450ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
324619f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
3247e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
324819f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
324919f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
325023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
325123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
32527cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
3253e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
3254e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3255e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
32565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
32575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
32585545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
32595545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
32605545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
32611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
32621eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
32635545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
3264e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
3265e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
32661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32675545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
3268e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
326985606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
32705545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
32715545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
32725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
32735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
3274a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3275a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
3276a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3277a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
3278b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
3279b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
3280b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
3281b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3282a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
3283b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
3284b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
3285b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3286b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
3287b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
3288e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3289e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3290e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
3291e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
3292f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
3293b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
3294e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
3295e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
329610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
329754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
329854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
3299af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
3300af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
3301af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3302af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
3303251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
33046cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
33051eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
33063b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
3307f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
3308f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
3309f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3310f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
3311f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
3312f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
3313f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
3314dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
3315f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
33161eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
33178387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
3318f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
33198a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
33208387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
33218387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
33228387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
33238387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
33248387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3325360d23ef628bf891514e77c519d1d77305ca1743David Majnemer  // Call the LateTemplateParser callback if there is a need to late parse
3326a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
3327f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
33288387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
3329ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    // FIXME: Optimize to allow individual templates to be deserialized.
3330ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    if (PatternDecl->isFromASTFile())
3331ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith      ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3332ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith
3333ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3334ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    assert(LPT && "missing LateParsedTemplate");
3335ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateTemplateParser(OpaqueParser, *LPT);
33368387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
33378387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
33388387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3339f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
3340e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3341e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
3342a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3343e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
3344e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
3345e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
3346a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3347e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
3348e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
3349a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3350e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
3351a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
3352e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
3353cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
335458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
335558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
335662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
335758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
3358e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
335958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
33601eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
3361e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
33621eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
336360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // C++1y [temp.explicit]p10:
336460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   Except for inline functions, declarations with types deduced from their
336560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   initializer or return value, and class template specializations, other
336660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   explicit instantiation declarations have the effect of suppressing the
336760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   implicit instantiation of the entity to which they refer.
3368651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (Function->getTemplateSpecializationKind() ==
3369651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          TSK_ExplicitInstantiationDeclaration &&
337060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->isInlined() &&
3371651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      !PatternDecl->getReturnType()->getContainedAutoType())
3372d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
33731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3374ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (PatternDecl->isInlined()) {
3375ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    // Function, and all later redeclarations of it (from imported modules,
3376ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    // for instance), are now implicitly inline.
3377ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    for (auto *D = Function->getMostRecentDecl(); /**/;
3378ef8225444452a1486bd721f3285301fe84643b00Stephen Hines         D = D->getPreviousDecl()) {
3379ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      D->setImplicitlyInline();
3380ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      if (D == Function)
3381ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        break;
3382ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    }
3383ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  }
3384d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
3385f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3386d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
3387a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
3388a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3389e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
3390e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3391e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
3392b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
3393b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
3394b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
33955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
339662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
339765173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling  SavePendingLocalImplicitInstantiationsRAII
339865173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling      SavedPendingLocalImplicitInstantiations(*this);
33992a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
34002a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
340162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
34022a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
34031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3404a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
3405f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
3406e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
340754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
340860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
340960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
341060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
341160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
341260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
341360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
341460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
341560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
34161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34171d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
34181d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
34191d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
34206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ActOnStartOfFunctionDef(nullptr, Function);
34217c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
34221d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
34231d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
34241d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
34257c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
34261d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
34276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl);
342854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
34291d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
34301d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
34311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3432cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
3433cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
3434cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3435cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3436cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
3437cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
3438cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3439cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
3440cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3441cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3442cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
3443cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
3444a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3445cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
3446cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
3447b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
34481d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
34490c01d18094100db92d38daa923c95661512db203John McCall
3450651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (auto *Listener = getASTMutationListener())
3451651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Listener->FunctionDefinitionInstantiated(Function);
3452651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
34531d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
34541d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
3455aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
3456aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
3457aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
34581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
345960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
346060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
346162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
346260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
346360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
3464b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
34652a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
34662a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
34672a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3468b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
34691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
347062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
34711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34722a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
34738155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
34748155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
34752a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
34762a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3477b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
34788155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
34798155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
348062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
3481b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
3482a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3483a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3484ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3486ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentList &TemplateArgList,
3487ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
3488ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted,
3489ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SourceLocation PointOfInstantiation, void *InsertPos,
3490ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LateInstantiatedAttrVec *LateAttrs,
3491ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LocalInstantiationScope *StartingScope) {
3492ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (FromVar->isInvalidDecl())
34936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3494ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3495ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3496d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
34976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3498ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3499ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  MultiLevelTemplateArgumentList TemplateArgLists;
3500ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3501ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3502d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // Instantiate the first declaration of the variable template: for a partial
3503d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // specialization of a static data member template, the first declaration may
3504d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // or may not be the declaration in the class; if it's in the class, we want
3505d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // to instantiate a member in the class (a declaration), and if it's outside,
3506d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // we want to instantiate a definition.
3507651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  //
3508651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // If we're instantiating an explicitly-specialized member template or member
3509651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // partial specialization, don't do this. The member specialization completely
3510651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // replaces the original declaration in this case.
3511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool IsMemberSpec = false;
3512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (VarTemplatePartialSpecializationDecl *PartialSpec =
3513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar))
3514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    IsMemberSpec = PartialSpec->isMemberSpecialization();
3515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate())
3516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    IsMemberSpec = FromTemplate->isMemberSpecialization();
3517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (!IsMemberSpec)
3518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    FromVar = FromVar->getFirstDecl();
3519d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
35202e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek  MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList);
35212e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek  TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(),
35222e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek                                        MultiLevelList);
3523ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3524ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // TODO: Set LateAttrs and StartingScope ...
3525ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3526ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return cast_or_null<VarTemplateSpecializationDecl>(
3527ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Instantiator.VisitVarTemplateSpecializationDecl(
3528ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3529ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3530ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3531ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiates a variable template specialization by completing it
3532ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// with appropriate type information and initializer.
3533ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3534ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3535ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3536ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3537ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
3538ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
3539d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs,
3540ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3541ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
35426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
3543ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3544ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Update the type of this variable template specialization.
3545ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarSpec->setType(DI->getType());
3546ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3547ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Instantiate the initializer.
3548ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3549ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3550ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VarSpec;
3551ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3552ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3553ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// BuildVariableInstantiation - Used after a new variable has been created.
3554ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// Sets basic variable data and decides whether to postpone the
3555ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// variable instantiation.
3556ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::BuildVariableInstantiation(
3557ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *NewVar, VarDecl *OldVar,
3558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs,
3559a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner,
3560a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LocalInstantiationScope *StartingScope,
3561567f917df048d42732997a479b2b257403fc88efLarisse Voufo    bool InstantiatingVarTemplate) {
3562ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3563a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // If we are instantiating a local extern declaration, the
3564a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // instantiation belongs lexically to the containing function.
3565ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If we are instantiating a static data member defined
3566ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line, the instantiation will have the same lexical
3567ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // context (which will be a namespace scope) as the template.
3568a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (OldVar->isLocalExternDecl()) {
3569a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLocalExternDecl();
3570a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLexicalDeclContext(Owner);
3571a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (OldVar->isOutOfLine())
3572ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3573ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setTSCSpec(OldVar->getTSCSpec());
3574ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setInitStyle(OldVar->getInitStyle());
3575ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3576ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setConstexpr(OldVar->isConstexpr());
357704fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith  NewVar->setInitCapture(OldVar->isInitCapture());
3578dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  NewVar->setPreviousDeclInSameBlockScope(
3579dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      OldVar->isPreviousDeclInSameBlockScope());
3580ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setAccess(OldVar->getAccess());
3581ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
358290618ff0b53f063a880c4933d44c70d6c1cb8dc0Richard Smith  if (!OldVar->isStaticDataMember()) {
3583e7bd89af8aa96a779c0031baf1a21e960a51d0f0Rafael Espindola    if (OldVar->isUsed(false))
3584e7bd89af8aa96a779c0031baf1a21e960a51d0f0Rafael Espindola      NewVar->setIsUsed();
3585ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setReferenced(OldVar->isReferenced());
3586ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3587ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3588aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // See if the old variable had a type-specifier that defined an anonymous tag.
3589aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // If it did, mark the new variable as being the declarator for the new
3590aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // anonymous tag.
3591aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) {
3592aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    TagDecl *OldTag = OldTagType->getDecl();
3593aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    if (OldTag->getDeclaratorForAnonDecl() == OldVar) {
3594aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl();
3595aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      assert(!NewTag->hasNameForLinkage() &&
3596aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer             !NewTag->hasDeclaratorForAnonDecl());
3597aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      NewTag->setDeclaratorForAnonDecl(NewVar);
3598aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    }
3599aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  }
3600aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer
3601ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3602ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3603a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
3604a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      *this, NewVar->getDeclName(), NewVar->getLocation(),
3605a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
3606a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                  : Sema::LookupOrdinaryName,
3607a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
3608ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3609651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() &&
3610651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() ||
3611651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines       OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) {
3612dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // We have a previous declaration. Use that one, so we merge with the
3613dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // right type.
3614dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    if (NamedDecl *NewPrev = FindInstantiatedDecl(
3615dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith            NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
3616dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      Previous.addDecl(NewPrev);
3617dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  } else if (!isa<VarTemplateSpecializationDecl>(NewVar) &&
3618dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith             OldVar->hasLinkage())
3619ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
3620567f917df048d42732997a479b2b257403fc88efLarisse Voufo  CheckVariableDeclaration(NewVar, Previous);
3621ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3622a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!InstantiatingVarTemplate) {
3623a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
3624a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
3625ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
3626a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  }
3627a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
3628a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!OldVar->isOutOfLine()) {
3629ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (NewVar->getDeclContext()->isFunctionOrMethod())
3630ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3632ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3633ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Link instantiations of static data members back to the template from
3634ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // which they were instantiated.
3635567f917df048d42732997a479b2b257403fc88efLarisse Voufo  if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate)
3636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setInstantiationOfStaticDataMember(OldVar,
3637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                               TSK_ImplicitInstantiation);
3638ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3639651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // Forward the mangling number from the template to the instantiated decl.
3640651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar));
3641651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar));
3642651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3643d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // Delay instantiation of the initializer for variable templates until a
3644651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // definition of the variable is needed. We need it right away if the type
3645651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // contains 'auto'.
3646651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if ((!isa<VarTemplateSpecializationDecl>(NewVar) &&
3647651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines       !InstantiatingVarTemplate) ||
3648651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      NewVar->getType()->isUndeducedType())
3649ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3650ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3651ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Diagnose unused local variables with dependent types, where the diagnostic
3652ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will have been deferred.
3653ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!NewVar->isInvalidDecl() &&
3654ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() &&
3655ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      OldVar->getType()->isDependentType())
3656ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DiagnoseUnusedDecl(NewVar);
3657ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3658ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3659ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the initializer of a variable.
3660ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableInitializer(
3661ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *Var, VarDecl *OldVar,
3662ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3664ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->getAnyInitializer())
3665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We already have an initializer in the class.
3666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return;
3667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->getInit()) {
3669ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3670ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    else
3672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3673ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3674ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate the initializer.
3675ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ExprResult Init =
3676ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        SubstInitializer(OldVar->getInit(), TemplateArgs,
3677ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                         OldVar->getInitStyle() == VarDecl::CallInit);
3678ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Init.isInvalid()) {
3679ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      bool TypeMayContainAuto = true;
3680ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Expr *InitExpr = Init.get();
3681ef8225444452a1486bd721f3285301fe84643b00Stephen Hines
3682ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      if (Var->hasAttr<DLLImportAttr>() && InitExpr &&
3683ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          !InitExpr->isConstantInitializer(getASTContext(), false)) {
3684ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        // Do not dynamically initialize dllimport variables.
3685ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      } else if (InitExpr) {
3686ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        bool DirectInit = OldVar->isDirectInit();
3687ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        AddInitializerToDecl(Var, InitExpr, DirectInit, TypeMayContainAuto);
3688ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else
3689ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        ActOnUninitializedDecl(Var, TypeMayContainAuto);
3690ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    } else {
3691ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // FIXME: Not too happy about invalidating the declaration
3692ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // because of a bogus initializer.
3693ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setInvalidDecl();
3694ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
3695ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3696ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PopExpressionEvaluationContext();
3697ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3698ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             !Var->isCXXForRangeDecl())
3699ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ActOnUninitializedDecl(Var, false);
3700ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3701ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3702a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
3703a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3704a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
37057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
37067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
37077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
37087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
37097caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
37107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
37117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
37127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
37137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
3714e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3715e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3716e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
3717e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
37187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
37197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
37207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
3721e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
3722e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
3723ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3724ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                DefinitionRequired);
3725ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3726ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3727ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3728ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         VarDecl *Var, bool Recursive,
3729ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         bool DefinitionRequired) {
37307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
37317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
37321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3733ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *VarSpec =
3734ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      dyn_cast<VarTemplateSpecializationDecl>(Var);
37356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VarDecl *PatternDecl = nullptr, *Def = nullptr;
3736d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
3737d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      getTemplateInstantiationArgs(Var);
3738ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3739ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarSpec) {
3740d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a variable template specialization, make sure that it is
3741d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // non-dependent, then find its instantiation pattern.
3742ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
3743ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3744ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo               VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3745ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Only instantiate variable template specializations that are "
3746ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "not type-dependent");
37473151b7c6dd49947b0a91b3e22c31f4864629e355Larisse Voufo    (void)InstantiationDependent;
3748ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3749d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Find the variable initialization that we'll be substituting. If the
3750d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // pattern was instantiated from a member template, look back further to
3751d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // find the real pattern.
3752ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(VarSpec->getSpecializedTemplate() &&
3753ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Specialization without specialized template?");
3754ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    llvm::PointerUnion<VarTemplateDecl *,
3755ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                       VarTemplatePartialSpecializationDecl *> PatternPtr =
3756ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getSpecializedTemplateOrPartial();
3757439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) {
3758d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      VarTemplatePartialSpecializationDecl *Tmpl =
3759d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          PatternPtr.get<VarTemplatePartialSpecializationDecl *>();
3760d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      while (VarTemplatePartialSpecializationDecl *From =
3761d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                 Tmpl->getInstantiatedFromMember()) {
3762d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        if (Tmpl->isMemberSpecialization())
3763d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          break;
3764439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3765d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        Tmpl = From;
3766d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3767d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PatternDecl = Tmpl;
3768439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    } else {
3769d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>();
3770d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      while (VarTemplateDecl *From =
3771d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                 Tmpl->getInstantiatedFromMemberTemplate()) {
3772d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        if (Tmpl->isMemberSpecialization())
3773d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          break;
3774d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3775d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        Tmpl = From;
3776d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3777d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PatternDecl = Tmpl->getTemplatedDecl();
3778d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    }
3779d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3780d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a static data member template, there might be an
3781d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // uninstantiated initializer on the declaration. If so, instantiate
3782d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // it now.
3783d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    if (PatternDecl->isStaticDataMember() &&
3784bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola        (PatternDecl = PatternDecl->getFirstDecl())->hasInit() &&
3785d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        !Var->hasInit()) {
3786d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // FIXME: Factor out the duplicated instantiation context setup/tear down
3787d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // code here.
3788d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3789d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker      if (Inst.isInvalid())
3790d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        return;
3791d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3792d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // If we're performing recursive template instantiation, create our own
3793d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // queue of pending implicit instantiations that we will instantiate
3794d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // later, while we're still within our own instantiation context.
3795d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      SmallVector<VTableUse, 16> SavedVTableUses;
3796d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
3797d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (Recursive) {
3798d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VTableUses.swap(SavedVTableUses);
3799d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PendingInstantiations.swap(SavedPendingInstantiations);
3800d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3801d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3802d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      LocalInstantiationScope Local(*this);
3803d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3804d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Enter the scope of this instantiation. We don't use
3805d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // PushDeclContext because we don't have a scope.
3806d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      ContextRAII PreviousContext(*this, Var->getDeclContext());
3807d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs);
3808d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PreviousContext.pop();
3809d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3810d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // FIXME: Need to inform the ASTConsumer that we instantiated the
3811d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // initializer?
3812d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3813d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // This variable may have local implicit instantiations that need to be
3814d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // instantiated within this scope.
3815d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PerformPendingInstantiations(/*LocalOnly=*/true);
3816d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3817d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Local.Exit();
3818d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3819d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (Recursive) {
3820d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Define any newly required vtables.
3821d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        DefineUsedVTables();
3822d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3823d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Instantiate any pending implicit instantiations found during the
3824d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // instantiation of this template.
3825d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PerformPendingInstantiations();
3826439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3827d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Restore the set of pending vtables.
3828d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        assert(VTableUses.empty() &&
3829d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith               "VTableUses should be empty before it is discarded.");
3830d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VTableUses.swap(SavedVTableUses);
3831439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3832d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Restore the set of pending implicit instantiations.
3833d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        assert(PendingInstantiations.empty() &&
3834d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith               "PendingInstantiations should be empty before it is discarded.");
3835d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PendingInstantiations.swap(SavedPendingInstantiations);
3836d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3837439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    }
3838ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3839d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Find actual definition
3840d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def = PatternDecl->getDefinition(getASTContext());
3841d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  } else {
3842d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a static data member, find its out-of-line definition.
3843d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(Var->isStaticDataMember() && "not a static data member?");
3844d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    PatternDecl = Var->getInstantiatedFromStaticDataMember();
3845d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3846d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(PatternDecl && "data member was not instantiated from a template?");
3847d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(PatternDecl->isStaticDataMember() && "not a static data member?");
3848d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def = PatternDecl->getOutOfLineDefinition();
3849ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3850ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3851d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // If we don't have a definition of the variable template, we won't perform
3852d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // any instantiation. Rather, we rely on the user to instantiate this
3853d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // definition (or provide a specialization for it) in another translation
3854d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // unit.
3855d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (!Def) {
3856e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3857d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (VarSpec)
3858ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3859d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith             diag::err_explicit_instantiation_undefined_var_template) << Var;
3860d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      else
3861ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3862ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_member)
3863d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith            << 2 << Var->getDeclName() << Var->getDeclContext();
3864d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Diag(PatternDecl->getLocation(),
3865d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith           diag::note_explicit_instantiation_here);
3866ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (VarSpec)
3867ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Var->setInvalidDecl();
386858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
386958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
387062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
387158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
387258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
387358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
38747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
38757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
38767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3877234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3878234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
3879251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
3880234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
3881251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
3882a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3883ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // C++11 [temp.explicit]p10:
3884ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   Except for inline functions, [...] explicit instantiation declarations
3885ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   have the effect of suppressing the implicit instantiation of the entity
3886ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   to which they refer.
3887234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
3888251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
38891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3890afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
3891afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
3892afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
3893afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
3894afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3895afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3896afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
3897afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3898afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
3899d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3900afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
3901afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
3902025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
3903d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // If we already have a definition, we're done.
3904d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (VarDecl *Def = Var->getDefinition()) {
3905d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // We may be explicitly instantiating something we've already implicitly
3906d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // instantiated.
3907d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3908d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                       PointOfInstantiation);
3909d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    return;
391095e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
3911f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
39127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3913d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
39147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
39151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
39177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
39187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
39195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
392062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
392165173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling  SavePendingLocalImplicitInstantiationsRAII
392265173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling      SavedPendingLocalImplicitInstantiations(*this);
39238155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
39248155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
392562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
39268155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
39271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
39297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3930ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  ContextRAII PreviousContext(*this, Var->getDeclContext());
39317bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
3932ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
39331028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3934ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec)
3935ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3936d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                          TemplateArgs));
3937d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  else if (Var->isStaticDataMember() &&
3938d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith           Var->getLexicalDeclContext()->isRecord()) {
3939d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // We need to instantiate the definition of a static data member template,
3940d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // and all we have is the in-class declaration of it. Instantiate a separate
3941d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // declaration of the definition.
3942d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(),
3943d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                          TemplateArgs);
3944d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl(
39456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        VarSpec->getSpecializedTemplate(), Def, nullptr,
3946d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray()));
3947d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    if (Var) {
3948d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      llvm::PointerUnion<VarTemplateDecl *,
3949d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                         VarTemplatePartialSpecializationDecl *> PatternPtr =
3950d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          VarSpec->getSpecializedTemplateOrPartial();
3951d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (VarTemplatePartialSpecializationDecl *Partial =
3952d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>())
3953d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf(
3954d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith            Partial, &VarSpec->getTemplateInstantiationArgs());
3955d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3956d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Merge the definition with the declaration.
3957d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      LookupResult R(*this, Var->getDeclName(), Var->getLocation(),
3958d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                     LookupOrdinaryName, ForRedeclaration);
3959d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      R.addDecl(OldVar);
3960d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      MergeVarDecl(Var, R);
3961d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3962d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Attach the initializer.
3963d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiateVariableInitializer(Var, Def, TemplateArgs);
3964d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    }
3965d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  } else
3966d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Complete the existing variable's definition with an appropriately
3967d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // substituted type and initializer.
3968d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs);
3969f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3970ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PreviousContext.pop();
39717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
39727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3973ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PassToConsumerRAII.Var = Var;
3974d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(),
3975d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                       OldVar->getPointOfInstantiation());
39767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3977ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3978ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // This variable may have local implicit instantiations that need to be
3979ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiated within this scope.
3980ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PerformPendingInstantiations(/*LocalOnly=*/true);
3981ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
39827bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
39837bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
39847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
39858155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
39868155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
39878155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
39887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
39891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
399062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
39911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39928155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
39938155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
3994ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "VTableUses should be empty before it is discarded.");
39958155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
39968155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
39977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
39988155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
3999ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "PendingInstantiations should be empty before it is discarded.");
400062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
40011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
4002a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
4003815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4004090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
4005090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
4006090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
4007090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
40081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400990ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
401054b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
4011a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4012090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
4013651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (const auto *Init : Tmpl->inits()) {
4014030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
4015030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
4016030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
4017030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
4018030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
40193fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
4020a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
40223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
402376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
402498a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      SmallVector<UnexpandedParameterPack, 4> Unexpanded;
40253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
402698a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
40273fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
4028d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
4029dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
4030a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
40313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
4032a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
4033a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
4034d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
40353fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
40363fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
40373fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
40383fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
40393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
40403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
4041a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4042a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
4043cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
40443fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
40453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
40463fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
40475b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
40485b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
40495b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
40503fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
40513fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
40523fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
40533fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
40543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
405576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
4056a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
4057a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
40583fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
40593fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
40603fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
40613fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
40623fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
40633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
40643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
40656df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
4066ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                                     BaseTInfo, TempInit.get(),
40673fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
40683fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
40693fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
40703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
40713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
40723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
4073a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40743fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
40753fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
4076a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
40783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
40793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
40806b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
40815b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
40825b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
40835b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
40846b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
40856b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
4086090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
4087a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4088090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
408976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
409076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
409176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
409276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
409376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
409476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
40959db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
4096802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
4097802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
4098802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
40996df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
410076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
4101ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(),
410276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
410376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
4104ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(),
410576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
4106090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
4107b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
410800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
410900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
411000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
4111b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
4112b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
4113b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
4114b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
4115b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
41161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4117ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      NewInit = BuildMemberInitializer(Member, TempInit.get(),
41186df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
411900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
412000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
4121b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
412200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
412300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
412400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
4125b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
4126b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
4127b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
41286df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
4129b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
41306df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
4131ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(),
41326df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
4133090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
4134090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
41359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
41369db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
4137090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
41389db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
413990ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
4140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
4141090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
41421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4143090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
4144d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
4145090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
4146090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
414793c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
41489db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
4149090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
4150090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
415152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
415252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
415352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
415452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
415552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
415652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
415752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
415852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
415952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
416052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
416152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
416252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
416352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
416452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
416552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
41660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
41670d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
41680d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
4169a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
41700d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
41710d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
41720d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
41730d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
41740d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
4175a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
41760d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
41770d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
41780d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
4179a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
4180ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
4181ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
4182a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
4183ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
4184ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
4185ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
4186ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
4187ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
4188ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
4189ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
4190ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
4191a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4192ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
4193ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
4194ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
419552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
419652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
419752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
419852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
419952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
420052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
420152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
420252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
420352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
420452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
420552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
420652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
420752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
420852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
420952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
421052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
421152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
421252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
421352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
421452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
421552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
421652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
421752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
421852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
421952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
422052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
422152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
422252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
422352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
422452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
422552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
422652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
422752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
422852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
422952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
423052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
423152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
423252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
423352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4234ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
4235ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
4236ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
4237ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
4238ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
4239ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4240ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
4241ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
4242ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
4243ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
4244ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
4245ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
42467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
42477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
42487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
4249ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
42507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
42517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
42527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
42530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
42540d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
4255ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
42560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
42570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
425852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
425952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
426052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
426152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
426252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
426352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
426452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
426552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
426652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
426752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
426852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
426952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
427052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
427152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
427252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4273ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
4274ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
4275815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
42760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
42777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
42787ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
42797ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
42807ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
42817ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
42827ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
42837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
42847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
42857ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
42860d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
42870d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
42880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
42890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
4290815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
42910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
42920d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
42931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
429552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
42961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
429852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
4299815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
430052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
430152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
4302815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
43037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
430452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
430552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
430652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
430752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
430852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
4309a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
43100d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
43110d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
43120d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
4313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
4314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
4315ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
4316ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
4317ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
4318d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
4319d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
4320d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
43211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
4322d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
4323d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
4324d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
43251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4326ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
4327ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
4328ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4329ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
4330ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
4331ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4332815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
4333815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
4334815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4335815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4336815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
43371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
4338815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
4339815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
4340815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
4341815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
4342815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
4343815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
4344815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
43456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
4346815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4347815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
434802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
434902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
435002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
435102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
43527c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
4353e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
435402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
43557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
435602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
435702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
435802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
435902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
4360ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
4361ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
4362815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4363815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
4364815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
4365815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
4366815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
4367815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4368815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
4369815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
4370815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
4371815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
4372815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
4373815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
4374815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4375815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
4376815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
4377815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4378815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
4379815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
4380815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4381041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4382041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// \p EnumConstantDecl for \p KnownValue (which refers to
4383041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4384041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4385041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// this mapping from within the instantiation of <tt>X<int></tt>.
43867c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
4387e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
4388815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
4389a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // FIXME: Parmeters of pointer to functions (y below) that are themselves
4390a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // parameters (p below) can have their ParentDC set to the translation-unit
4391a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // - thus we can not consistently check if the ParentDC of such a parameter
4392a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // is Dependent or/and a FunctionOrMethod.
4393a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // For e.g. this code, during Template argument deduction tries to
4394a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // find an instantiated decl for (T y) when the ParentDC for y is
4395a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // the translation unit.
4396a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //   e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {}
4397651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  //   float baz(float(*)()) { return 0.0; }
4398a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //   Foo(baz);
4399a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // The better fix here is perhaps to ensure that a ParmVarDecl, by the time
4400a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // it gets here, always has a FunctionOrMethod as its ParentDC??
4401a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // For now:
4402a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //  - as long as we have a ParmVarDecl whose parent is non-dependent and
4403a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //    whose type is not instantiation dependent, do nothing to the decl
4404a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //  - otherwise find its instantiated decl.
4405a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() &&
4406a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali      !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType())
4407a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali    return D;
440880f2b2e693422f84ec3735f16a08614a527b0bc5Rafael Espindola  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
44096d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
44107bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
44117bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
44122bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
44132bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
4414d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4415d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
4416d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
4417a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
441857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
441957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
442057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
4421a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
44229a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
44239a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
442457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
442557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
442657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
4427dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // If we're performing a partial substitution during template argument
4428dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // deduction, we may not have values for template parameters yet. They
4429dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // just map to themselves.
4430dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4431dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov        isa<TemplateTemplateParmDecl>(D))
4432dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov      return D;
4433dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov
443429a46e63490176608efe13f13b293a6ce9862059Serge Pavlov    if (D->isInvalidDecl())
44356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
443629a46e63490176608efe13f13b293a6ce9862059Serge Pavlov
443757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
443857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
443957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
4440a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
444157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
444257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
4443a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
444457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
444557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
44462bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
4447815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4448ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // For variable template specializations, update those that are still
4449ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // type-dependent.
4450ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec =
4451ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          dyn_cast<VarTemplateSpecializationDecl>(D)) {
4452ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
4453ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &VarTemplateArgs =
4454ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getTemplateArgsInfo();
4455ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (TemplateSpecializationType::anyDependentTemplateArguments(
4456ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            VarTemplateArgs, InstantiationDependent))
4457ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      D = cast<NamedDecl>(
4458ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4459ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return D;
4460ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
4461ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4462e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4463e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
4464e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
4465a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
44662c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
44672c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
4468e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
44692c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
44702c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
44712c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
44722c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
44732c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
4474ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
44752c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
44762c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
44772c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
44782c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
44792c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
44802c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
44812c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
44822c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
4483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
44842c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
44852c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
44862c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
44872c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
44882c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
44892c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
44902c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
44912c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
44922c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
4493ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
44942c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
44952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
44962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
4497e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
4498ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
44992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
45002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
45012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
45022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
45032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
45042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
450552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
4506ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
45072c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
450852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
45098b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
4510e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
4511e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
4512e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
451352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4514e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
4515e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
4516a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
45177c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
45181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
45196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
45201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4521815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
4522815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
4523815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
4524815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
45257c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
45263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
45273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
4528eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
45293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
45307c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
45317c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
45323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
45333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
4534eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
4535eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
45363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
45373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
45386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines          return nullptr;
4539a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
4540a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
45417c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
45427c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
45437c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
45446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    NamedDecl *Result = nullptr;
4545815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
454617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
45473bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
4548815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
4549815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
4550815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
4551815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
4552815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4553815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
4554815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
4555815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4556815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
45571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
455817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
455917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
4560815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
45611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4562eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
4563eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
4564eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
4565eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
4566eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
4567eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
4568eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
4569eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
4570a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
4571eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
4572eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
4573eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
4574eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
4575eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
4576eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
4577eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4578eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
45790724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
45800724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
45810724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
45820724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
45830724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
45840724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
45850724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
45860724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
45870724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
45880724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
45890724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
45900724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
45910724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
45920724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
4593eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
4594eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
4595eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
4596eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
4597eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
4598a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4599815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
4600815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
4601815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4602815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
4603815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4604d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
46051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
4606d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
46078155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
460860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
460962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
461060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
461160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
461260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
461362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
461462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
461560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
461660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
461760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
461860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
46191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
46217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
4622f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4623f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
462458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
462558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
462658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
462758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
46287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
46297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
46301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate variable definitions
46327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
4633ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4634ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert((Var->isStaticDataMember() ||
4635ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            isa<VarTemplateSpecializationDecl>(Var)) &&
4636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Not a static data member, nor a variable template"
4637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           " specialization?");
4638c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
4639291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
4640291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
4641ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
4642291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
4643291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4644291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
4645291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
4646ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
4647291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
4648b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
4649291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
4650291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
465158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
465258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
465358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
465458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
4655ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
4656291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
4657291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
4658291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
4659291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4660ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4661ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        "instantiating variable definition");
466258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
466358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
4664ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate static data member definitions or variable template
4666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // specializations.
4667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                  DefinitionRequired);
4669d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
4670d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
46710c01d18094100db92d38daa923c95661512db203John McCall
46720c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
46730c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
4674651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto DD : Pattern->ddiags()) {
46750c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
46760c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
46770c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
46780c01d18094100db92d38daa923c95661512db203John McCall      break;
46790c01d18094100db92d38daa923c95661512db203John McCall    }
46800c01d18094100db92d38daa923c95661512db203John McCall  }
46810c01d18094100db92d38daa923c95661512db203John McCall}
4682