SemaTemplateInstantiateDecl.cpp revision 65173e04eacb68ff89a58fbff14979eb318896c9
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
170c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
19a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Lookup.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/PrettyDeclStackTrace.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
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())
82f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<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
1321d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
13323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            const Decl *Tmpl, Decl *New,
13423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LateInstantiatedAttrVec *LateAttrs,
13523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LocalInstantiationScope *OuterMostScope) {
136cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
137cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
138cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
13923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
1404ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
141f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
142f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Aligned && Aligned->isAlignmentDependent()) {
143f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
144f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      continue;
1454ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1464ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
147f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    assert(!TmplAttr->isPackExpansion());
14823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
14923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
15023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
15123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *Saved = 0;
15223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
15323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
15423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
15523323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
156cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      // Allow 'this' within late-parsed attributes.
157cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      NamedDecl *ND = dyn_cast<NamedDecl>(New);
158cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXRecordDecl *ThisContext =
159cafeb948e6067b8dc897c441da522367917b06f9Richard Smith          dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
160cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
161cafeb948e6067b8dc897c441da522367917b06f9Richard Smith                                 ND && ND->isCXXInstanceMember());
162cafeb948e6067b8dc897c441da522367917b06f9Richard Smith
1635bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
1645bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
16531c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
16631c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
16723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
168d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
169d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
170d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1714f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1724f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
173b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1744f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1754f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1764f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
17757ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
17857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
17957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
18057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
18157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
18257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
18357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
18457ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1854f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
186b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1874f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1884f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1923dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1933dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1943dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1950cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1960cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1973dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1983dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1993dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
2003dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
2013dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
2023dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
2033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
2043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
2058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
206a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
207561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
208836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
209ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
210ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
211ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
213a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
215b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
216b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
219b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // HACK: g++ has a bug where it gets the value kind of ?: wrong.
220b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // libstdc++ relies upon this bug in its implementation of common_type.
221b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // If we happen to be processing that implementation, fake up the g++ ?:
222b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // semantics. See LWG issue 2141 for more information on the bug.
223b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
224b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
225b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
226b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DT->isReferenceType() &&
227b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
228b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
229b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      D->getIdentifier() && D->getIdentifier()->isStr("type") &&
230b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
231b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    // Fold it to the (non-reference) type which g++ would have produced.
232b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    DI = SemaRef.Context.getTrivialTypeSourceInfo(
233b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DI->getType().getNonReferenceType());
234b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith
2358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
236162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
237162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
238162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
239162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
240162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
241162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
242162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
2438dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2458dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
246cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
247cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
248cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
249cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
250c61361b102fcb9be7b64cc493fb797ea551eb8e7Douglas Gregor    if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
251cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
25283972f128e9218c051692bf96361327a701aeb79John McCall      assert(!newTag->hasNameForLinkage());
253162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
254cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
255d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
256a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
257ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
2587c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2597c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
260b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
261b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
262a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2635df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
2645df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2655df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
2665df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
2675df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
268bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola    Typedef->setPreviousDecl(InstPrevTypedef);
2695126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2705126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2711d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
272d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
27346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2768dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2778dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
278162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
282162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
283162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
284162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
3003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
3023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
304ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
3053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
3063bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
3073bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
3083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
3093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
3103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
3123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
3133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
3143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
3153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
3173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
3193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
320bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola    Inst->setPreviousDecl(PrevAliasTemplate);
3213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
3233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
3253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
326a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
3283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
330162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
331162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
3323d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
333567f917df048d42732997a479b2b257403fc88efLarisse Voufo  return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false);
334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
336567f917df048d42732997a479b2b257403fc88efLarisse VoufoDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D,
337567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                             bool InstantiatingVarTemplate) {
338ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3399901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3409901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3419901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3429901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3439901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3449901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3459901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
346ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
347a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3480a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3490a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3500a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3510a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3523d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3533d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
354c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
355c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
356c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
357c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
358c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
359a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
360a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  DeclContext *DC = Owner;
361a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
362a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
363a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration.
365a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
3663d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                 DI->getType(), DI, D->getStorageClass());
3681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3699aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3719aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3729aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3739aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
376ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
377bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
378a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
379567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                     StartingScope, InstantiatingVarTemplate);
3803d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3813d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3823d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3836206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3846206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3856206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3866206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3876206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3886206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3896206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3906206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
393a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
394561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
395836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
39607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
39707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
39807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
399a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
40007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
40107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
40907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
412b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
413b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
420f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
421f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
422f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
425ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
430e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
43307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
43407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
439ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
440703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
443663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
444663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
445f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
446663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
449a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
450be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
451be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
452be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
453f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
454f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
456f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
457f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
458f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
459a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
4609901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4619901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4627a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4639901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
466f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
46746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
468f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4698dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
47376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCallDecl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
47476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  bool Invalid = false;
47576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
47676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
47776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (DI->getType()->isVariablyModifiedType()) {
47876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
47976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    << D->getName();
48076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Invalid = true;
48176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else if (DI->getType()->isInstantiationDependentType())  {
48276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
48376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           D->getLocation(), D->getDeclName());
48476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    if (!DI) {
48576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      DI = D->getTypeSourceInfo();
48676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
48776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    } else if (DI->getType()->isFunctionType()) {
48876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      // C++ [temp.arg.type]p3:
48976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   If a declaration acquires a function type through a type
49076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   dependent on a template-parameter and this causes a
49176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   declaration that does not use the syntactic form of a
49276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   function declarator to have function type, the program is
49376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   ill-formed.
49476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
49576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      << DI->getType();
49676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
49776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    }
49876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else {
49976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
50076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  }
50176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  MSPropertyDecl *Property = new (SemaRef.Context)
50376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      MSPropertyDecl(Owner, D->getLocation(),
50476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getDeclName(), DI->getType(), DI,
50576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getLocStart(),
50676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getGetterId(), D->getSetterId());
50776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
50976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           StartingScope);
51076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
51176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (Invalid)
51276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Property->setInvalidDecl();
51376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
51476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Property->setAccess(D->getAccess());
51576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Owner->addDecl(Property);
51676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
51776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  return Property;
51876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall}
51976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
52087c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
52187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
52287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
52387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
52587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
52687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
527b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
528a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
529b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
530b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
531b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
532a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
533b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
534b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
53587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53640e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
53787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
53887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
53940e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
54087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
54187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
54287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
54387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
54487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
54587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
54687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
54787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
54887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
54902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
55002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
55106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
55232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5534fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5544fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5554fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5564fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5574fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5584fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5594fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5604fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5614fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5624fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5634fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5644fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
56506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
566fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
567d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
5680216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
56906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
57006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
571a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
57206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5739a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
57406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
57506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
576a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
577a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
57806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
57906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
58032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
581af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
582af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
583af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
584af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
585af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
58606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
589a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
59006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5915fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5929a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
59302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
59402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
595fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
596fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
600f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
601f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
602f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
605ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
6078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
6088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
609e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
6109ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
611e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
612e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
613e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
6148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
61738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
61838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
61938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
62038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
62138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
62238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
62338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
62438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
62538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
626ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
62838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
629a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6301274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
631f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6321274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
636f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
637f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
638f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
640f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
641f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
642f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6441274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6451274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6461274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6471274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6495b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6505b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
651f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
65206c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
653b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
65417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
655f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6564ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
6574ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
6584ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
6594ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
6604ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
6614ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
6624ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6634ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
6644ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
6654ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
6664ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
6674ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
6684ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
6694ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
6708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
671f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
672f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
673f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
67457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  //
67557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // DR1484 clarifies that enumeration definitions inside of a template
67657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // declaration aren't considered entities that can be separately instantiated
67757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // from the rest of the entity they are declared inside of.
67857907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) {
67957907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
6804ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
68157907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  }
682f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
683f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
684f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
685f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
686f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
687f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
688f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
689f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6901af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
6911af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
6921af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
6935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
696f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
697f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
6988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
70060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
701ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
702f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
704f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
706ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
707ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
7088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
7108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
7118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
7128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
7138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7199ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
728581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7295b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7303b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
73117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
732d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
734a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
735f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
736f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
73796084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
73896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
739581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
74096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
744f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
745f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
746f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
7479ff2b421f352fe0a0769c0a2a75af922c147b878Dmitri Gribenko                        Enumerators,
748fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
7498dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7508dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7516477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
752b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7536477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7546477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
755e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
75693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
75793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
758550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
759550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7602a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
761e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
762ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
764d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
765e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
766e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
771c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
772c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
773c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
774c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
775c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
776c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
77793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
782ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
78337574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
7843bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
7853bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
78637574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
78737574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
78837574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
78937574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
79037574f55cd637340f651330f5cfda69742880d36Nick Lewycky
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
79393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
79493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
79593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
797c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
79893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
799c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
80993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
81093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
81593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
81893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
820c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8221eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
823c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
82693ba8579c341d5329175f1413cdc3b35a36592d2John McCall
827c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
82893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
829c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
830c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
831c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
832c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
833c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
834c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
835c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
836c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
837a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
838c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
839c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
840c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
841c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
842c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
843c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
844c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
845c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
846c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
847c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
848c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
849c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
850c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
851c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
852c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
853c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
854c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
85593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
85693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
85793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
85893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
85993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
860a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
861c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
862c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
863c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
864c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
865c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
866c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
867c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
86893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
86993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
87093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
871c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
872c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
87393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
87493ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
87593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
87693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
87793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
878e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
87993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
880ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
881ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
882f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
883e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
884c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
885c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
886b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
887e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
88893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
88993ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
89093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
891e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
892ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
89393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
894ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
895ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
896ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
897ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
898ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
89922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Inst->setObjectOfFriendDecl();
90093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
90193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
90293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
903e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
90437574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
90537574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
90693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
907a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
908f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
9093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
91024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
911ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
912259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
91393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9141b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9154c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9164c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
917e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
918259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
919a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9204c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9214c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9224c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9234c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9244c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
925e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
926d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
927d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
928d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
929d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
930d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
932d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
933d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
934bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola      if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
935d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
936d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
937d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
938e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
939e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
940e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
941d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9427974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9437974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
944ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
945a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
946ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
947ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
948ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
949ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
9503bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
951ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
952a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
953ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
9543bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
957a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
958d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
959d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
960d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
961d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
962d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9637974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9647974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
965ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
966ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->getTemplatedDecl()->isStaticDataMember() &&
967ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
968ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
969ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template, which
970ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will contain the instantiations of the template parameters.
971ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
972ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = D->getTemplateParameters();
973ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
974ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
975ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return NULL;
976ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
977ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *Pattern = D->getTemplatedDecl();
978ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *PrevVarTemplate = 0;
979ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
980ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Pattern->getPreviousDecl()) {
981ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
982ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Found.empty())
983ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
984ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
985ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
986dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  VarDecl *VarInst =
987567f917df048d42732997a479b2b257403fc88efLarisse Voufo      cast_or_null<VarDecl>(VisitVarDecl(Pattern,
988567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                         /*InstantiatingVarTemplate=*/true));
989ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
990ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext *DC = Owner;
991ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
992ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *Inst = VarTemplateDecl::Create(
993ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
994ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarInst, PrevVarTemplate);
995ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarInst->setDescribedVarTemplate(Inst);
996ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
997ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Inst->setAccess(D->getAccess());
998ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate)
999ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setInstantiatedFromMemberTemplate(D);
1000ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1001ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (D->isOutOfLine()) {
1002ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1003ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
1004ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1005ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1006ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Owner->addDecl(Inst);
1007ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1008ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate) {
1009ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Queue up any out-of-line partial specializations of this member
1010ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template; the client will force their instantiation once
1011ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the enclosing class has been instantiated.
1012ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1013ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    D->getPartialSpecializations(PartialSpecs);
1014ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1015bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola      if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1016ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        OutOfLineVarPartialSpecs.push_back(
1017ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            std::make_pair(Inst, PartialSpecs[I]));
1018ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1019ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1020ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Inst;
1021ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1022ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1023ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1024ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *D) {
1025ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->isStaticDataMember() &&
1026ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
1027ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1028ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1029ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1030ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Lookup the already-instantiated declaration and return that.
1031ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1032ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(!Found.empty() && "Instantiation found nothing?");
1033ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1034ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1035ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(InstVarTemplate && "Instantiation did not find a variable template?");
1036ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1037ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplatePartialSpecializationDecl *Result =
1038ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1039ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return Result;
1040ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1041ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1042ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1043ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
10447974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
1045d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1046550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
1047550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
1048a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
1049550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
10502a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1051895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
1052d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1053d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
1055d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
1056a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1057a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1059a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1060a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
1061a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
1062a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1063a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
1065a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1066a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
1067d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
1068d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
10691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1070d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1071a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1072a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
107337d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1074a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1075a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1076e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1077b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1078b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1079e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1080e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1081e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1082b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1083a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1084a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1085b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
10861f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1087a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
10881f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
10891f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
10901f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
10911f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1092b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1093d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1094d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1095d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1096d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1097d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
1098d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1099d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1100ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
11017c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1102ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
11036c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
11046c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
11056c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
11066c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1107d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1108d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1110ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1111ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1112b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1113b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1114b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1115b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1116b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1117d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1118eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1119eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1120eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1121eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1122eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1123d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1124f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1125d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
112602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
112702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
112822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  if (D->getFriendObjectKind())
112922050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Record->setObjectOfFriendDecl();
113002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
11319901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
113257907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isAnonymousStructOrUnion())
11339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
113457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
113557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isLocalClass())
113657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1137d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
113817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
113957907e56191adea0fa870c052054eb0fe0c4681fBill Wendling
114057907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // DR1484 clarifies that the members of a local class are instantiated as part
114157907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  // of the instantiation of their enclosing entity.
114257907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  if (D->isCompleteDefinition() && D->isLocalClass()) {
114357907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    if (SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
114457907e56191adea0fa870c052054eb0fe0c4681fBill Wendling                                 TSK_ImplicitInstantiation,
114557907e56191adea0fa870c052054eb0fe0c4681fBill Wendling                                 /*Complain=*/true)) {
114657907e56191adea0fa870c052054eb0fe0c4681fBill Wendling      llvm_unreachable("InstantiateClass shouldn't fail here!");
114757907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    } else {
114857907e56191adea0fa870c052054eb0fe0c4681fBill Wendling      SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
114957907e56191adea0fa870c052054eb0fe0c4681fBill Wendling                                      TSK_ImplicitInstantiation);
115057907e56191adea0fa870c052054eb0fe0c4681fBill Wendling    }
115157907e56191adea0fa870c052054eb0fe0c4681fBill Wendling  }
1152d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1153d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1154d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
115571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
115671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
115771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
115871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
115971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
116071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
116171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
116271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
116371074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1164bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1165bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1166bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1167bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1168bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1169bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1170bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1171bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1172bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1173bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  return Context.getFunctionType(NewFunc->getResultType(),
11740567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                 NewFunc->getArgTypes(), NewEPI);
117571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
117671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
117702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
117802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
117902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
118002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
118102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
11827557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1183a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1184127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1185127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1186127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1187b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1188c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11901e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
11912c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1192c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(),
11932c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1195127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
11962c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
11972c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1198127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1200b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1201b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1202b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1203b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1204b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1205b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
120679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1207b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1208a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
120979c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
12102a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
121364b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
121421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
12152dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
121671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1217fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1218c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1219c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1220c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1221c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1222c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1223c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1224d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1225d325daa506338ab86f9dd468b48fd010673f49a6John McCall
122668b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
1227a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // in the enclosing namespace; otherwise we need to find the instantiated
1228a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // context.
122968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
1230a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl()) {
123168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1232a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
1233a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (isFriend && QualifierLoc) {
1234d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1235c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1236d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1237d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1238d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1239a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
12407c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1241d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
124268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
124302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1244ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1245635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
1246459ef03126f9f0420efb3355e3b2ed3c1fdfb38aRafael Espindola                           D->getCanonicalDecl()->getStorageClass(),
1247af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
124886c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1249de9ed71c696bee936a21323f61548164de0eda13Enea Zaffanella  Function->setRangeEnd(D->getSourceRange().getEnd());
1250b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1251d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1252d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1253d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1254c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1255c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1256b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1257a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
1258a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    Function->setLocalExternDecl();
1259a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
1260b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1261a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
1262b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1263b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1264b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1265b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1266b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1268e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1269c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  for (unsigned P = 0; P < Params.size(); ++P)
1270c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (Params[P])
1271c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      Params[P]->setOwningFunction(Function);
12724278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
127302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1274ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1275a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1276a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1277a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1278a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1279a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1280a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1281a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1282a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1283a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1284a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1285a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1286a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1287a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1288a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1289a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1290d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1291a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1292a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1293a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1294a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1295b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1296b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1297d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1298d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1299d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1300d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1301d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1302d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1303d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
130466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
130566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1306c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1307838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1308910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
1309c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.begin(),
1310c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.size()),
13111e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
131280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
131380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
131480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
131580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
131680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
131780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
131880f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1319d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
132002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1321a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1322e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1323e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1325af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1326a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1327a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
1328a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      SemaRef, Function->getDeclName(), SourceLocation(),
1329a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
1330a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                             : Sema::LookupOrdinaryName,
1331a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
13326826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1333af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1334af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1335af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1336af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1337af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
133822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Function->setObjectOfFriendDecl();
1339af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1340af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1341af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1342af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1343e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1344e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1345e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1346af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1347af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1348af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1349af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1350af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1351af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1352af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1353af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1354af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1355af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1356af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1357af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1358af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1359af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1360af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1361a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1362af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1363af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1364af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1365a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1366a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1367a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
13686826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1369a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1370a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1371a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1372a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1373a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13746826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13756826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1376a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1377a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13789f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
13792c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1380e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
138176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
138276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
138376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
138476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1385a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1386a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1387d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
138822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    PrincipalDecl->setObjectOfFriendDecl();
13891b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1390ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
139177535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1392ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
139353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
139453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
139553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
139653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
139753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
139853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
139953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
140053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
140153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
140253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
140380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((!SemaRef.getLangOpts().CPlusPlus11 ||
140453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
140553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
140653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
140753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1408238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1409238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1410238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
141110620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1412238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
141353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
141480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                     SemaRef.getLangOpts().CPlusPlus11 ?
141553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
141653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1417238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
141880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        if (!SemaRef.getLangOpts().CPlusPlus11)
141953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1420a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1421238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1422238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1423238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1424238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1425238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
142613a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
142713a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1428ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1429ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
143080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          if (!SemaRef.getLangOpts().CPlusPlus11 &&
143153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1432ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1433ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1434ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1435ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1436ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1437ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1438ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1439ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1440ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1441ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1442ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1443ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1444ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1445238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
14466a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
144710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
144853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
144980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                           SemaRef.getLangOpts().CPlusPlus11 ?
145053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
145153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1452238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
14536a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
145480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith              if (!SemaRef.getLangOpts().CPlusPlus11)
145553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1456238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1457238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1458238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1459238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1460238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1461a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1462a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1463a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (Function->isLocalExternDecl() && !Function->getPreviousDecl())
1464a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1465a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
146676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
146776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
146876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
146976d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1470eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1471e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1472e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14732dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1474d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1475d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1476af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1477af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
14786b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1479d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1482d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1483c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14851e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
14862c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1487c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(),
1488c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                             Innermost.size(),
14892c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14916b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
14922c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
14932c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
14946b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
14956b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1496b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1497b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1498b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1499b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1500b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1501b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
150279c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1503a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
150479c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
15052a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
150648dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
15074eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
15085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
15094eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
15104eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
15114eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
15124eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
15134eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
15144eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
15154eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
15164eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
15174eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
15184eab39f0745fb1949dbb40c4145771b927888242John McCall    }
15194eab39f0745fb1949dbb40c4145771b927888242John McCall  }
15204eab39f0745fb1949dbb40c4145771b927888242John McCall
15215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1522dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
152321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
15242dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
152571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
15262dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1527c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1528c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1529c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1530b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1531a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1532c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1533b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1534b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1535b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1536b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1537c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1538b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1539c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1540b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1541c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1542c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1543c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1544b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1545b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1546b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1547b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1548b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1549b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1550b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1551b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1553b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1554dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1556ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15572577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15582577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
155917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1561ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
156316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
156486c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
1565b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
15664841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // Claim that the instantiation of a constructor or constructor template
15674841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // inherits the same constructor that the template does.
1568b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1569b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            Constructor->getInheritedConstructor())) {
1570b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // If we're instantiating a specialization of a function template, our
1571b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // "inherited constructor" will actually itself be a function template.
1572b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // Instantiate a declaration of it, too.
1573b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      if (FunctionTemplate) {
1574b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1575b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               !Inh->getParent()->isDependentContext() &&
1576b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               "inheriting constructor template in dependent context?");
1577b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1578b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith                                         Inh);
1579d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker        if (Inst.isInvalid())
1580b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1581b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1582b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        LocalInstantiationScope LocalScope(SemaRef);
1583b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
1584b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // Use the same template arguments that we deduced for the inheriting
1585b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // constructor. There's no way they could be deduced differently.
1586b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        MultiLevelTemplateArgumentList InheritedArgs;
1587b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1588b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Inh = cast_or_null<CXXConstructorDecl>(
1589b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1590b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (!Inh)
1591b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1592b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      }
15934841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
1594b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    }
159517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
159617e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1597ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15982577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
159916573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
160065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
160165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1602ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
16030130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1604f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
160586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
16069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1607dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
160872fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola    StorageClass SC = D->isStatic() ? SC_Static : SC_None;
16092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1610ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
161172fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola                                   SC, D->isInlineSpecified(),
161286c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1613dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16146b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1615d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1616d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1617d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1618c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1619c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1620b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1621d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1622d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1623d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1625d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1626d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1627d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1628d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1629d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1630d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1631d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1632d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1633d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1634d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1635d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1636d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1638d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1639b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1640b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
164122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith      FunctionTemplate->setObjectOfFriendDecl();
1642b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
16431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1644d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
164566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
164666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1647c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1648838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1649910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1650c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.begin(),
1651c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.size()),
16521e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1653b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
165466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
16552db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
165666724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1657a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
16597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
16607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1661b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
16624eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
16634eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
16644eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
16654eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
16664eab39f0745fb1949dbb40c4145771b927888242John McCall
1667b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
166822050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Method->setObjectOfFriendDecl();
1669b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
16707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16725545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
16735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
16745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
16754278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16785545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
16792dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
16802577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
16812577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
16821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1683b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1684b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1686dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1687dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1688dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1689dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
16906826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
16916826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1692dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16932dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1694af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
16952c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
169665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
16974ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
16984ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
16994ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
17001f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
17011f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
17021f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
17031f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
17041f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
17051f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
17061f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
17071f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
17081f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
170946460a68f6508775e98c19b4bb8454bb471aac24John McCall
17109eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
17119eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
17123bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1713ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1714ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
17153bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1716ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
17173bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
17181f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1719b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
17201f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17211f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17221f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1723b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
17241f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17251f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17261f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
17271f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
17281f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
17291f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
17301f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
17311f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
17321f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17331f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
17341f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17351f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
17361f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
17371f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
17381f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
17391f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1740b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1741eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
17422dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
17432dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17442dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1745615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1746dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1747615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1748615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
174903b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
175017e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
175103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
175203b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1753bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
175465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1755bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1756bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
1757ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
175866874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
175966874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
17602dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17612dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1762e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1763e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1764e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
17654fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
17661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1767e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1768344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1769344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
17704fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
17714fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1773e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
17749a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1775a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17769d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
17779d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TypeSourceInfo *InstantiatedDefaultArg =
17789d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs,
17799d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                          D->getDefaultArgumentLoc(), D->getDeclName());
17809d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (InstantiatedDefaultArg)
17819d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Inst->setDefaultArgument(InstantiatedDefaultArg, false);
17829d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1783e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1784a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1785550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1786550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1787a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1788e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1789e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1790e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
179133642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
179233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
179333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
17946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
17955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
17965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
17976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1798a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
179933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
180033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
18016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
18026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1803a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
18046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
18056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
18066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
18076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
18086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
18096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1810a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
18116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
18126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
18136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1814a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
18176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
18196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
18206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
18216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1822a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
18246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
18256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
18266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
18286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
18296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
183039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
18316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
18325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
18336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1834a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
18376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1839dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
18406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1841dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
18426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
18436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1844a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1846a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
18476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
18486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1849a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
18516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
18526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
18536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1854a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
18556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
18566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
18576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1858a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
18616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
18626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
18646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
18656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
18666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1867a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
18696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
18706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
18716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
18726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
18736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
18756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
18766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
18776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
18786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1879a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
18806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
18816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
18826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1883a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
18856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
18866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
18876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
18916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
18926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1893a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
18946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
18956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
18966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1897a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
19006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
19016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
19026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
19036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
19046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
190533642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1906a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
19086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1909a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1910ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1911ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1912a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1913a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
19146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
19156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
19166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
19176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
19186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
19196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1920a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1921ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
19226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1923a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1924a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1925a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
19266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1927a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19289a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
192933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
193033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1931a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19329d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
19339d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs);
19349d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!Value.isInvalid())
19359d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(Value.get(), false);
19369d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1937a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1938a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1939550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1940550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
194133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
194233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
194333642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
19446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
19456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
19466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
19476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
19486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
19496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
19506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
19516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
19526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
19536964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
19546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
19556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
19566964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
19576964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
19586964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
19596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
19606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19610dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
19629106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
19639106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
19649106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
19659106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
19669106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
19706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
19746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
19756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
19766964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
19776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
19786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19796964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
19806964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
19816964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
19826964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19836964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
19846964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19856964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19866964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
19876964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
19886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
19896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
19906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
19916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
19926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
19936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
19946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
19956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19966964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
19976964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
19986964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
19996964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
2000dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
20016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
20026964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
20036964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
20046964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
20056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
20066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
20076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
20086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
20106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
20116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
20126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
20136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
20146964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
20156964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith          return 0;
20166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
20176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
20186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
20206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
20216964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
20226964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
20236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
20246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
20256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
20266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
20276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
20286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
20306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
20316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
20326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
20336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
20346964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
20359106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
20369106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
20372a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
20389106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
20399106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
20406964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
2041a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
2042a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
20446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
20456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
20466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
20486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
20496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
20516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
20526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
20536964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
2055a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
20566964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20576964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
20586964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
20599d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
20609d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    NestedNameSpecifierLoc QualifierLoc =
20619d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateQualifierLoc();
20629d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    QualifierLoc =
20639d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
20649d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TemplateName TName = SemaRef.SubstTemplateName(
20659d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
20669d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
20679d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!TName.isNull())
20689d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(
20699d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          TemplateArgumentLoc(TemplateArgument(TName),
20709d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateQualifierLoc(),
20719d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateNameLoc()),
20729d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          false);
20739d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
20749a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
2075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2076a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
20779106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
20789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2079a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
20819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
20829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
208348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2084db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
2085db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
2086a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
208748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
208848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2089a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
2090db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
2091a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
2092a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
209348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
2094536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
2095536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
2096536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
2097536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
2098536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2099536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
210048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
210148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
210248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2103ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
21041b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
21051b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
21061b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
21071b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
21081b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
21091b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
21101b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
21111b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
21121b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
21135149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
21145149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21155149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21165149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
2117dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
21181b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
21191b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
21201b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2121ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2122ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21239f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
21249f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
21259f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
21269f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
21279f54ad4381370c6b771424b53d219e661d6d6706John McCall
2128ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2129ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
21309f54ad4381370c6b771424b53d219e661d6d6706John McCall
2131ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
21328d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->getUsingLoc(),
21335149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2134ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
21358d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->hasTypename());
2136ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21375149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
21385149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21399f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
21409f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
21419f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
21429f54ad4381370c6b771424b53d219e661d6d6706John McCall
21439f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
21448d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
21458d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                            D->hasTypename(), SS,
21469f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
21479f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
21489f54ad4381370c6b771424b53d219e661d6d6706John McCall
21499f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
21509f54ad4381370c6b771424b53d219e661d6d6706John McCall
21519f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
21528d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella      SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS,
2153ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2154ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
21559f54ad4381370c6b771424b53d219e661d6d6706John McCall
2156ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2157ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2158ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2159ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21609f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
21619f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
21629f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
21639f54ad4381370c6b771424b53d219e661d6d6706John McCall
2164c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2165c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2166c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
2167c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2168c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2169c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2170323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2171323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
21729f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
21739f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
21749f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
21759f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
21769f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2177f06a2893bc9778857295c64ee32b4a899a338480Richard Smith        cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2178f06a2893bc9778857295c64ee32b4a899a338480Richard Smith            Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs));
2179b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
2180b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
21819f54ad4381370c6b771424b53d219e661d6d6706John McCall
2182f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    UsingShadowDecl *PrevDecl = 0;
2183f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    if (CheckRedeclaration) {
2184f06a2893bc9778857295c64ee32b4a899a338480Richard Smith      if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl))
2185f06a2893bc9778857295c64ee32b4a899a338480Richard Smith        continue;
2186f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    } else if (UsingShadowDecl *OldPrev = Shadow->getPreviousDecl()) {
2187f06a2893bc9778857295c64ee32b4a899a338480Richard Smith      PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl(
2188f06a2893bc9778857295c64ee32b4a899a338480Richard Smith          Shadow->getLocation(), OldPrev, TemplateArgs));
2189f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    }
21909f54ad4381370c6b771424b53d219e661d6d6706John McCall
2191f06a2893bc9778857295c64ee32b4a899a338480Richard Smith    UsingShadowDecl *InstShadow =
2192f06a2893bc9778857295c64ee32b4a899a338480Richard Smith        SemaRef.BuildUsingShadowDecl(/*Scope*/0, NewUD, InstTarget, PrevDecl);
21939f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2194323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2195323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2196323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
21979f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2198ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2199ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2200ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2201ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2202ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
22039f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
22049f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
2205ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2206ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
22087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
22095149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2210a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
22115149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
22125149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
22137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
22147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
22165149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
22177ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2218ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2219accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2220ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
22217ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
22227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2223ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
22247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
22264469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2227ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2228ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
22307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
22317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
22337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
22345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
22355149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
22365149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
22370dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
2238a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
22390dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
22405149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2242ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2243ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2244ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
22451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
22469488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2247ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
22487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
22504469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2251ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2252ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
22540dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
22550dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2256af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2257af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2258af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2259af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
22606b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
22616b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
2262af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2263af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2264af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2265af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
22666b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
22676b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
22686b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
22696b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
22706b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
22716b02009359a462ffe633696a4441313b462e6566Nico Weber  }
22726b02009359a462ffe633696a4441313b462e6566Nico Weber
2273af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
22746b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
22756b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2276af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2277af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2278af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2279af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2280af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2281af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2282af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2283af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2284af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2285af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2286af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2287af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2288c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey BataevDecl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2289c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                     OMPThreadPrivateDecl *D) {
22906af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  SmallVector<Expr *, 5> Vars;
22916af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  for (ArrayRef<Expr *>::iterator I = D->varlist_begin(),
22926af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev                                  E = D->varlist_end();
2293c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev       I != E; ++I) {
2294c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2295c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
22966af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev    Vars.push_back(Var);
2297c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  }
2298c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2299c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OMPThreadPrivateDecl *TD =
2300c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2301c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2302c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  return TD;
2303c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev}
2304c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2305ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
2306ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitFunctionDecl(D, 0);
2307ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2308ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2309ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
2310ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitCXXMethodDecl(D, 0);
2311ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2312ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2313ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2314ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("There are only CXXRecordDecls in C++");
2315ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2316ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2317ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *
2318ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanTemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2319ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    ClassTemplateSpecializationDecl *D) {
2320ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Only ClassTemplatePartialSpecializationDecls occur"
2321ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                   "inside templates");
2322ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2323ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2324ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *D) {
2326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgumentListInfo VarTemplateArgsInfo;
2328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2329ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(VarTemplate &&
2330ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "A template specialization without specialized template?");
2331ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2332ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the current template arguments.
2333ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2336ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2337ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2338ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
2339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this template.
2342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  bool ExpansionIntoFixedList = false;
2344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(
2345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, VarTemplate->getLocStart(),
2346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted, &ExpansionIntoFixedList))
2348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Find the variable template specialization declaration that
2351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // corresponds to these arguments.
2352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2353ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted.data(), Converted.size(), InsertPos))
2355ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a variable template specialization, return it.
2356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return VarSpec;
2357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2358ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                            VarTemplateArgsInfo, Converted);
2360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2362ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2363ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
2365d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    llvm::ArrayRef<TemplateArgument> Converted) {
2366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is the variable for an anonymous struct or union,
2368ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiate the anonymous struct/union type first.
2369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2371ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2372ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        return 0;
2373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
2376ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                        D->getTypeSpecStartLoc(), D->getDeclName());
2378ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2379ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2380ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2381ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2382ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2383ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << D->isStaticDataMember() << DI->getType();
2384ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2385ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2386ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2387ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration
2388ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2389ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2390ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2391ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Converted.size());
2392ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Var->setTemplateArgsInfo(TemplateArgsInfo);
2393d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (InsertPos)
2394d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    VarTemplate->AddSpecialization(Var, InsertPos);
2395ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2396ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2397ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
2398ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2399ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2400ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
2401a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     Owner, StartingScope);
2402ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2403ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Var;
2404ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2405ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2406ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2407ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("@defs is not supported in Objective-C++");
2408ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2409ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2410ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2411ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  // FIXME: We need to be able to instantiate FriendTemplateDecls.
2412ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2413ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               DiagnosticsEngine::Error,
2414ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               "cannot instantiate %0 yet");
2415ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  SemaRef.Diag(D->getLocation(), DiagID)
2416ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    << D->getDeclKindName();
2417ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2418ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return 0;
2419ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2420ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2421ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2422ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Unexpected decl");
2423ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2424ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2425ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2426d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
24277e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
24282fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
24292fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
24302fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
24318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
24328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
24338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2434e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2435e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2436e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2437e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2438e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2439e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2440e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2441ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2442e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2443e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2444e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2445e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
24465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2447e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2448e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2449e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2450e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2451bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2452e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
24539148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2454e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2455e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2456e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2457ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2458e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2459e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2460e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2461e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2462e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2463e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2464e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2466e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2467a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2468ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2473a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2474ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2476d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2477d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2478d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2479ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2480ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2482550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2483550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2484550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
24852a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2486a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2487ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2489ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2490ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2491ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2492d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2493a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2494ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2495ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2496c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2497c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2498c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2499c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2500c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2501c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2502e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2503e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2504a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2505ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2506ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
25075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2508a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2510a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2511ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2512ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2513d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2514ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2515ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2517ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2518ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2519910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2520910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2521a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2524a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2525ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2526910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2527910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2528ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2529ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2530ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2533ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2534ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2535ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
25363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
25373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
25383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
25393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2540d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2541ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2542a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2543ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2544ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2545ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2546ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2547ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2548ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2550ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2551ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2552ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2553ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2554ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2555ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2557ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2558ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2559ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2560d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2561ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2562ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2563d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2564ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2565a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2566a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2567ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2568ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2569a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
257013c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2571a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2572ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2573ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2574ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2575a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2576910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2577910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2578d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
25793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
258037fd27dbb941d27f4bd7412e534e7e5089d6781bRichard Smith                                                     0);
2581b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2582b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2583b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2584b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2585ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
25864469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2587a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2588ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2589ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
25901e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2591d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2592ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2593ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2594ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the declaration of a variable template partial
2595ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization.
2596ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2597ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param VarTemplate the (instantiated) variable template that is partially
2598ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialized by the instantiation of \p PartialSpec.
2599ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2600ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param PartialSpec the (uninstantiated) variable template partial
2601ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization that we are instantiating.
2602ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2603ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \returns The instantiated partial specialization, if successful; otherwise,
2604ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// NULL to indicate an error.
2605ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplatePartialSpecializationDecl *
2606ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoTemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2607ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate,
2608ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *PartialSpec) {
2609ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template partial
2610ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization, which will contain the instantiations of the template
2611ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // parameters.
2612ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
2613ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2614ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template parameters of the variable template partial
2615ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2616ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2617ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2618ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
2619ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2620ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2621ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template arguments of the variable template partial
2622ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2623c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2624c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2625c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2626c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2627c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2628c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2629ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    InstTemplateArgs, TemplateArgs))
2630ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2632ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this
2633ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // class template.
2634ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2635ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        InstTemplateArgs, false, Converted))
2637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2638ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2639ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Figure out where to insert this variable template partial specialization
2640ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // in the member template's set of variable template partial specializations.
2641ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2642ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *PrevDecl =
2643ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate->findPartialSpecialization(Converted.data(), Converted.size(),
2644ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                             InsertPos);
2645ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2646ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the canonical type that describes the converted template
2647ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // arguments of the variable template partial specialization.
2648ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2649ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), Converted.data(), Converted.size());
2650ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2651ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the fully-sugared type for this variable template
2652ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization as the user wrote in the specialization
2653ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // itself. This means that we'll pretty-print the type retrieved
2654ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // from the specialization's declaration the way that the user
2655ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // actually wrote the specialization, rather than formatting the
2656ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // name based on the "canonical" representation used to store the
2657ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // template arguments in the specialization.
2658ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2659ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2660ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CanonType);
2661ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2662ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (PrevDecl) {
2663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We've already seen a partial specialization with the same template
2664ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // parameters and template arguments. This can happen, for example, when
2665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // substituting the outer template arguments ends up causing two
2666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template partial specializations of a member variable template
2667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // to have identical forms, e.g.,
2668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2669ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   template<typename T, typename U>
2670ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   struct Outer {
2671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename X, typename Y> pair<X,Y> p;
2672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<T, Y> p;
2673ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<U, Y> p;
2674ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   };
2675ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2676ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   Outer<int, int> outer; // error: the partial specializations of Inner
2677ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //                          // have the same signature.
2678ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2679ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_var_partial_spec_redeclared)
2680ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << WrittenTy->getType();
2681ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PrevDecl->getLocation(),
2682ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::note_var_prev_partial_spec_here);
2683ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2684ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2685ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2686ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2687ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI = SemaRef.SubstType(
2688ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSourceInfo(), TemplateArgs,
2689ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2690ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2691ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2692ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2693ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2694ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2695ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_variable_instantiates_to_function)
2696ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << PartialSpec->isStaticDataMember() << DI->getType();
2697ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2698ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2699ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2700ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create the variable template partial specialization declaration.
2701ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplatePartialSpecializationDecl *InstPartialSpec =
2702ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplatePartialSpecializationDecl::Create(
2703ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2704ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2705ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          DI, PartialSpec->getStorageClass(), Converted.data(),
270637fd27dbb941d27f4bd7412e534e7e5089d6781bRichard Smith          Converted.size(), InstTemplateArgs);
2707ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2708ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2709ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(PartialSpec, InstPartialSpec))
2710ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2711ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2712ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2713ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setTypeAsWritten(WrittenTy);
2714ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2715ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Add this partial specialization to the set of variable template partial
2716ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specializations. The instantiation of the initializer is not necessary.
2717ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
271804592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
271904592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs,
2720a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     LateAttrs, Owner, StartingScope);
272104592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
2722ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstPartialSpec;
2723ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2724ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
272521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
272621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
27275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
272821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
272921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
273021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2731cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2732cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2733cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2734cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2735cafeb948e6067b8dc897c441da522367917b06f9Richard Smith    ThisContext = cast<CXXRecordDecl>(Owner);
2736cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2737cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2738cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
27396cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
27406cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
27416cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2742cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2743cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
274421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
274521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
27465545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2747c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2748c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2749c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (NewTInfo != OldTInfo) {
2750c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // Get parameters from the new type info.
2751140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
275239e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2753500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
275439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
275512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
275639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
2757500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2758500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2759dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2760500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2761500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2762500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2763500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2764500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2765a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
276612c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
276739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
276812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2769500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2770500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2771500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2772500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2773500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
277439e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
2775500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2776500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2777500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
277812c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
27796920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2780c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    } else {
2781c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // The function type itself was not dependent and therefore no
2782c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // substitution occurred. However, we still need to instantiate
2783c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // the function parameters themselves.
2784c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      const FunctionProtoType *OldProto =
2785c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          cast<FunctionProtoType>(OldProtoLoc.getType());
278639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2787c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        ParmVarDecl *OldParam = OldProtoLoc.getArg(i);
2788c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        if (!OldParam) {
2789c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
2790c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner              D, D->getLocation(), OldProto->getArgType(i)));
2791c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          continue;
2792c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        }
2793c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
2794ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman        ParmVarDecl *Parm =
2795c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner            cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
27966920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
27976920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
27986920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
27996920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2800cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2801c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  } else {
2802c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // If the type of this function, after ignoring parentheses, is not
2803c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // *directly* a function type, then we're instantiating a function that
2804c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // was declared via a typedef or with attributes, e.g.,
2805c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2806c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   typedef int functype(int, int);
2807c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   functype func;
2808c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   int __cdecl meth(int, int);
2809c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2810c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // In this case, we'll just go instantiate the ParmVarDecls that we
2811c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // synthesized in the method declaration.
2812c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    SmallVector<QualType, 4> ParamTypes;
2813c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
2814c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               D->getNumParams(), TemplateArgs, ParamTypes,
2815c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               &Params))
2816c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      return 0;
2817cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
2818c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
281921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
28205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
28215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2822e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2823e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2824e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2825e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2826e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2827e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2828e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2829e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2830e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2831e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2832e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2833e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2834e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2835e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2836e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2837e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2838e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2839e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2840e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2841e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2842e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2843e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2844dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
2845e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2846500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2847500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2848500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2849e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2850e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2851e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2852e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2853e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2854e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2855e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2856e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2857e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2858e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2859e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
286013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
286113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2862e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2863e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2864e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2865e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2866e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2867e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2868e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2869e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2870e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2871e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2872e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2873e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2874e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
287580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
2876e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2877e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2878e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2879e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2880e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2881e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2882e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2883e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2884e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2885e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2886e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2887e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2888e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2889e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2890e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2891e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2892e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2893cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
2894e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2895e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2896e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2897e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2898e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2899e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2900e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2901e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2902e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2903e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2904e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2905e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2906e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2907e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2908e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2909e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2910e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2911e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2912e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2913e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2914e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2915e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2916e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2917e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2918e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2919e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2920e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2921e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2922e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2923e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2924e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2925e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2926e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2927e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2928e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2929e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2930e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2931e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2932e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2933e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2934e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2935e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2936e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2937e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2938e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2939e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2940e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2941e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2942e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2943e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2944e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2945e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2946e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2947e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2948e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2949e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2950e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2951e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2952e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2953e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2954e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2955e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2956e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2957e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2958e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2959e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2960e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2961e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2962ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2963ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2964ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2965ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2966e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2967e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2968e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2969e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2970e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2971e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2972e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2973e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2974e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2975e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2976e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2977e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2978e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2979e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2980e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
29810567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                               NewProto->getArgTypes(), EPI));
2982e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2983e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2984e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2985e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
298613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
298713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2988e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2989e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2990e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2991e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2992d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid()) {
2993b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
2994b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
2995b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2996b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
2997b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Decl->setType(Context.getFunctionType(Proto->getResultType(),
29980567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                          Proto->getArgTypes(), EPI));
2999e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
3000b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
3001e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3002e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
3003e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
3004e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
3005e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
3006e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
3007e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
3008e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
3009e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
301013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
301113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
3012e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
301313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
301413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
301513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
3016e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
3017e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
30181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
3019e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
3020e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
3021e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
30221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
30231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
3024e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
302585f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
302610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
30271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3028cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
3029cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
3030cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
30311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
30321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
3033cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
3034cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
3035cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
3036cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
3037cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
3038cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
3039cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
30401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
30414a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
30421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
3043cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
3044bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
3045cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
30464a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
3047cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
3048cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
30491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30500ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
30510ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
30520ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
305360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
3054e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
3055e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3056e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
3057e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
305880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
3059e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
3060e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
3061e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
306213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
306313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
306413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
30654841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      ExceptionSpecificationType NewEST = EST_Uninstantiated;
30664841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      if (EPI.ExceptionSpecType == EST_Unevaluated)
30674841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith        NewEST = EST_Unevaluated;
306813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
3069e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
3070e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
3071e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
3072e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
3073e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
30744841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      EPI.ExceptionSpecType = NewEST;
3075e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
307613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
30770567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      New->setType(SemaRef.Context.getFunctionType(
30780567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner          NewProto->getResultType(), NewProto->getArgTypes(), EPI));
3079e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
3080e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
3081e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
30820ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
30830ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
308419f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
3085e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
308619f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
308719f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
308823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
308923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
30907cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
3091e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
3092e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3093e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
30945545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
30955545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
30965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
30975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
30985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
30991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
31001eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
31015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
3102e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
3103e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
31041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31055545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
3106e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
310785606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
31085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
31095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
31105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
31115545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
3112a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3113a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
3114a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3115a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
3116b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
3117b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
3118b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
3119b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3120a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
3121b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
3122b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
3123b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3124b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
3125b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
3126e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3127e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3128e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
3129e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
3130f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
3131b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
3132e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
3133e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
313410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
313554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
313654dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
3137af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
3138af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
3139af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3140af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
3141251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
31426cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
31431eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
31443b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
3145f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
3146f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
3147f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3148f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
3149f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
3150f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
3151f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
3152dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
3153f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
31541eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
31558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
3156f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
31578a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
31588387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
31598387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
31608387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
31618387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31628387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3163360d23ef628bf891514e77c519d1d77305ca1743David Majnemer  // Call the LateTemplateParser callback if there is a need to late parse
3164a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
3165f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
31668387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
3167ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    // FIXME: Optimize to allow individual templates to be deserialized.
3168ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    if (PatternDecl->isFromASTFile())
3169ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith      ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3170ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith
3171ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3172ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    assert(LPT && "missing LateParsedTemplate");
3173ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateTemplateParser(OpaqueParser, *LPT);
31748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
31758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3177f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
3178e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3179e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
3180a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3181e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
3182e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
3183e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
3184a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3185e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
3186e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
3187a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3188e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
3189a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
3190e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
3191cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
319258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
319358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
319462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
319558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
3196e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
319758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
31981eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
3199e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
32001eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
320160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // C++1y [temp.explicit]p10:
320260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   Except for inline functions, declarations with types deduced from their
320360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   initializer or return value, and class template specializations, other
320460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   explicit instantiation declarations have the effect of suppressing the
320560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   implicit instantiation of the entity to which they refer.
32061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
3207d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
320860e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->isInlined() &&
320937e849ad80731ac1b2ad1c64e73bced27802bd8bRichard Smith      !PatternDecl->getResultType()->getContainedAutoType())
3210d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
32111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3212d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (PatternDecl->isInlined())
3213d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
3214d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
3215f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3216d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
3217a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
3218a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3219e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
3220e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3221e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
3222b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
3223b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
3224b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
32255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
322662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
322765173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling  SavePendingLocalImplicitInstantiationsRAII
322865173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling      SavedPendingLocalImplicitInstantiations(*this);
32292a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
32302a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
323162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
32322a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
32331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3234a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
3235f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
3236e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
323754dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
323860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
323960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
324060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
324160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
324260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
324360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
324460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
324560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
32461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32471d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
32481d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
32491d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
32501d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    ActOnStartOfFunctionDef(0, Function);
32517c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32521d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
32531d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
32541d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
32557c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32561d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
32571d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith      getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
325854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
32591d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
32601d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
32611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3262cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
3263cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
3264cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3265cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3266cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
3267cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
3268cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3269cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
3270cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3271cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3272cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
3273cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
3274a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3275cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
3276cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
3277b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
32781d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
32790c01d18094100db92d38daa923c95661512db203John McCall
32801d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
32811d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
3282aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
3283aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
3284aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
32851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
328660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
328760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
328862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
328960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
329060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
3291b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
32922a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
32932a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
32942a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3295b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
32961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
329762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
32981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32992a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
33008155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
33018155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
33022a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
33032a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3304b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
33058155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
33068155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
330762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
3308b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
3309a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3310a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3311ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3312ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3313ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentList &TemplateArgList,
3314ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
3315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted,
3316ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SourceLocation PointOfInstantiation, void *InsertPos,
3317ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LateInstantiatedAttrVec *LateAttrs,
3318ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LocalInstantiationScope *StartingScope) {
3319ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (FromVar->isInvalidDecl())
3320ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3322ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3323d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
3324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  MultiLevelTemplateArgumentList TemplateArgLists;
3327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3329d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // Instantiate the first declaration of the variable template: for a partial
3330d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // specialization of a static data member template, the first declaration may
3331d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // or may not be the declaration in the class; if it's in the class, we want
3332d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // to instantiate a member in the class (a declaration), and if it's outside,
3333d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // we want to instantiate a definition.
3334bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola  FromVar = FromVar->getFirstDecl();
3335d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
33362e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek  MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList);
33372e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek  TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(),
33382e563c28c456b48f43f38f5a92a4bc292d5cda91Manuel Klimek                                        MultiLevelList);
3339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // TODO: Set LateAttrs and StartingScope ...
3341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return cast_or_null<VarTemplateSpecializationDecl>(
3343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Instantiator.VisitVarTemplateSpecializationDecl(
3344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiates a variable template specialization by completing it
3348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// with appropriate type information and initializer.
3349ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3353ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
3354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
3355d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs,
3356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
3358ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Update the type of this variable template specialization.
3361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarSpec->setType(DI->getType());
3362ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3363ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Instantiate the initializer.
3364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3365ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VarSpec;
3367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3368ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// BuildVariableInstantiation - Used after a new variable has been created.
3370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// Sets basic variable data and decides whether to postpone the
3371ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// variable instantiation.
3372ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::BuildVariableInstantiation(
3373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *NewVar, VarDecl *OldVar,
3374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs,
3375a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner,
3376a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LocalInstantiationScope *StartingScope,
3377567f917df048d42732997a479b2b257403fc88efLarisse Voufo    bool InstantiatingVarTemplate) {
3378ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3379a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // If we are instantiating a local extern declaration, the
3380a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // instantiation belongs lexically to the containing function.
3381ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If we are instantiating a static data member defined
3382ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line, the instantiation will have the same lexical
3383ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // context (which will be a namespace scope) as the template.
3384a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (OldVar->isLocalExternDecl()) {
3385a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLocalExternDecl();
3386a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLexicalDeclContext(Owner);
3387a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (OldVar->isOutOfLine())
3388ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3389ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setTSCSpec(OldVar->getTSCSpec());
3390ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setInitStyle(OldVar->getInitStyle());
3391ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3392ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setConstexpr(OldVar->isConstexpr());
339304fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith  NewVar->setInitCapture(OldVar->isInitCapture());
3394dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  NewVar->setPreviousDeclInSameBlockScope(
3395dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      OldVar->isPreviousDeclInSameBlockScope());
3396ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setAccess(OldVar->getAccess());
3397ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
339890618ff0b53f063a880c4933d44c70d6c1cb8dc0Richard Smith  if (!OldVar->isStaticDataMember()) {
3399e7bd89af8aa96a779c0031baf1a21e960a51d0f0Rafael Espindola    if (OldVar->isUsed(false))
3400e7bd89af8aa96a779c0031baf1a21e960a51d0f0Rafael Espindola      NewVar->setIsUsed();
3401ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setReferenced(OldVar->isReferenced());
3402ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3403ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3404aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // See if the old variable had a type-specifier that defined an anonymous tag.
3405aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // If it did, mark the new variable as being the declarator for the new
3406aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // anonymous tag.
3407aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) {
3408aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    TagDecl *OldTag = OldTagType->getDecl();
3409aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    if (OldTag->getDeclaratorForAnonDecl() == OldVar) {
3410aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl();
3411aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      assert(!NewTag->hasNameForLinkage() &&
3412aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer             !NewTag->hasDeclaratorForAnonDecl());
3413aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      NewTag->setDeclaratorForAnonDecl(NewVar);
3414aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    }
3415aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  }
3416aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer
3417ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3418ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3419ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (NewVar->hasAttrs())
3420ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    CheckAlignasUnderalignment(NewVar);
3421ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3422a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
3423a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      *this, NewVar->getDeclName(), NewVar->getLocation(),
3424a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
3425a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                  : Sema::LookupOrdinaryName,
3426a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
3427ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3428a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl()) {
3429dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // We have a previous declaration. Use that one, so we merge with the
3430dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // right type.
3431dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    if (NamedDecl *NewPrev = FindInstantiatedDecl(
3432dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith            NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
3433dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      Previous.addDecl(NewPrev);
3434dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  } else if (!isa<VarTemplateSpecializationDecl>(NewVar) &&
3435dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith             OldVar->hasLinkage())
3436ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
3437567f917df048d42732997a479b2b257403fc88efLarisse Voufo  CheckVariableDeclaration(NewVar, Previous);
3438ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3439a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!InstantiatingVarTemplate) {
3440a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
3441a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
3442ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
3443a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  }
3444a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
3445a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!OldVar->isOutOfLine()) {
3446ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (NewVar->getDeclContext()->isFunctionOrMethod())
3447ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3448ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3449ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3450ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Link instantiations of static data members back to the template from
3451ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // which they were instantiated.
3452567f917df048d42732997a479b2b257403fc88efLarisse Voufo  if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate)
3453ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setInstantiationOfStaticDataMember(OldVar,
3454ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                               TSK_ImplicitInstantiation);
3455ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3456d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // Delay instantiation of the initializer for variable templates until a
3457d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // definition of the variable is needed.
3458d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (!isa<VarTemplateSpecializationDecl>(NewVar) && !InstantiatingVarTemplate)
3459ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3460ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3461ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Diagnose unused local variables with dependent types, where the diagnostic
3462ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will have been deferred.
3463ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!NewVar->isInvalidDecl() &&
3464ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() &&
3465ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      OldVar->getType()->isDependentType())
3466ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DiagnoseUnusedDecl(NewVar);
3467ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3468ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3469ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the initializer of a variable.
3470ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableInitializer(
3471ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *Var, VarDecl *OldVar,
3472ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3473ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3474ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->getAnyInitializer())
3475ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We already have an initializer in the class.
3476ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return;
3477ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3478ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->getInit()) {
3479ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3480ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3481ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    else
3482ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3484ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate the initializer.
3485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ExprResult Init =
3486ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        SubstInitializer(OldVar->getInit(), TemplateArgs,
3487ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                         OldVar->getInitStyle() == VarDecl::CallInit);
3488ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Init.isInvalid()) {
3489ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      bool TypeMayContainAuto = true;
3490ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (Init.get()) {
3491ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        bool DirectInit = OldVar->isDirectInit();
3492ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        AddInitializerToDecl(Var, Init.take(), DirectInit, TypeMayContainAuto);
3493ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else
3494ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        ActOnUninitializedDecl(Var, TypeMayContainAuto);
3495ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    } else {
3496ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // FIXME: Not too happy about invalidating the declaration
3497ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // because of a bogus initializer.
3498ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setInvalidDecl();
3499ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
3500ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3501ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PopExpressionEvaluationContext();
3502ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3503ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             !Var->isCXXForRangeDecl())
3504ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ActOnUninitializedDecl(Var, false);
3505ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3506ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3507a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
3508a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3509a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
35107caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
35117caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
35127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
35137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
35147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
35157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
35167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
35177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
35187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
3519e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3520e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3521e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
3522e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
35237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
35247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
35257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
3526e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
3527e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
3528ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3529ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                DefinitionRequired);
3530ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3531ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3532ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3533ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         VarDecl *Var, bool Recursive,
3534ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         bool DefinitionRequired) {
35357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
35367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
35371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3538ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *VarSpec =
3539ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      dyn_cast<VarTemplateSpecializationDecl>(Var);
3540d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  VarDecl *PatternDecl = 0, *Def = 0;
3541d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
3542d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      getTemplateInstantiationArgs(Var);
3543ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3544ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarSpec) {
3545d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a variable template specialization, make sure that it is
3546d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // non-dependent, then find its instantiation pattern.
3547ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
3548ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3549ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo               VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3550ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Only instantiate variable template specializations that are "
3551ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "not type-dependent");
35523151b7c6dd49947b0a91b3e22c31f4864629e355Larisse Voufo    (void)InstantiationDependent;
3553ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3554d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Find the variable initialization that we'll be substituting. If the
3555d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // pattern was instantiated from a member template, look back further to
3556d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // find the real pattern.
3557ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(VarSpec->getSpecializedTemplate() &&
3558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Specialization without specialized template?");
3559ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    llvm::PointerUnion<VarTemplateDecl *,
3560ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                       VarTemplatePartialSpecializationDecl *> PatternPtr =
3561ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getSpecializedTemplateOrPartial();
3562439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) {
3563d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      VarTemplatePartialSpecializationDecl *Tmpl =
3564d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          PatternPtr.get<VarTemplatePartialSpecializationDecl *>();
3565d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      while (VarTemplatePartialSpecializationDecl *From =
3566d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                 Tmpl->getInstantiatedFromMember()) {
3567d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        if (Tmpl->isMemberSpecialization())
3568d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          break;
3569439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3570d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        Tmpl = From;
3571d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3572d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PatternDecl = Tmpl;
3573439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    } else {
3574d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>();
3575d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      while (VarTemplateDecl *From =
3576d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                 Tmpl->getInstantiatedFromMemberTemplate()) {
3577d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        if (Tmpl->isMemberSpecialization())
3578d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          break;
3579d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3580d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        Tmpl = From;
3581d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3582d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PatternDecl = Tmpl->getTemplatedDecl();
3583d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    }
3584d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3585d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a static data member template, there might be an
3586d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // uninstantiated initializer on the declaration. If so, instantiate
3587d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // it now.
3588d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    if (PatternDecl->isStaticDataMember() &&
3589bc6509175e1ce5cc1b48d1b97ac8d23d8b74167cRafael Espindola        (PatternDecl = PatternDecl->getFirstDecl())->hasInit() &&
3590d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        !Var->hasInit()) {
3591d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // FIXME: Factor out the duplicated instantiation context setup/tear down
3592d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // code here.
3593d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3594d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker      if (Inst.isInvalid())
3595d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        return;
3596d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3597d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // If we're performing recursive template instantiation, create our own
3598d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // queue of pending implicit instantiations that we will instantiate
3599d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // later, while we're still within our own instantiation context.
3600d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      SmallVector<VTableUse, 16> SavedVTableUses;
3601d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
3602d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (Recursive) {
3603d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VTableUses.swap(SavedVTableUses);
3604d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PendingInstantiations.swap(SavedPendingInstantiations);
3605d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3606d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3607d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      LocalInstantiationScope Local(*this);
3608d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3609d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Enter the scope of this instantiation. We don't use
3610d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // PushDeclContext because we don't have a scope.
3611d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      ContextRAII PreviousContext(*this, Var->getDeclContext());
3612d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs);
3613d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PreviousContext.pop();
3614d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3615d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // FIXME: Need to inform the ASTConsumer that we instantiated the
3616d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // initializer?
3617d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3618d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // This variable may have local implicit instantiations that need to be
3619d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // instantiated within this scope.
3620d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      PerformPendingInstantiations(/*LocalOnly=*/true);
3621d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3622d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Local.Exit();
3623d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3624d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (Recursive) {
3625d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Define any newly required vtables.
3626d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        DefineUsedVTables();
3627d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3628d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Instantiate any pending implicit instantiations found during the
3629d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // instantiation of this template.
3630d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PerformPendingInstantiations();
3631439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3632d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Restore the set of pending vtables.
3633d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        assert(VTableUses.empty() &&
3634d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith               "VTableUses should be empty before it is discarded.");
3635d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VTableUses.swap(SavedVTableUses);
3636439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3637d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        // Restore the set of pending implicit instantiations.
3638d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        assert(PendingInstantiations.empty() &&
3639d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith               "PendingInstantiations should be empty before it is discarded.");
3640d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        PendingInstantiations.swap(SavedPendingInstantiations);
3641d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      }
3642439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    }
3643ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3644d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Find actual definition
3645d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def = PatternDecl->getDefinition(getASTContext());
3646d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  } else {
3647d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // If this is a static data member, find its out-of-line definition.
3648d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(Var->isStaticDataMember() && "not a static data member?");
3649d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    PatternDecl = Var->getInstantiatedFromStaticDataMember();
3650d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3651d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(PatternDecl && "data member was not instantiated from a template?");
3652d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    assert(PatternDecl->isStaticDataMember() && "not a static data member?");
3653d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def = PatternDecl->getOutOfLineDefinition();
3654ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3655ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3656d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // If we don't have a definition of the variable template, we won't perform
3657d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // any instantiation. Rather, we rely on the user to instantiate this
3658d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // definition (or provide a specialization for it) in another translation
3659d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // unit.
3660d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (!Def) {
3661e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3662d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (VarSpec)
3663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3664d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith             diag::err_explicit_instantiation_undefined_var_template) << Var;
3665d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      else
3666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_member)
3668d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith            << 2 << Var->getDeclName() << Var->getDeclContext();
3669d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Diag(PatternDecl->getLocation(),
3670d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith           diag::note_explicit_instantiation_here);
3671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (VarSpec)
3672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Var->setInvalidDecl();
367358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
367458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
367562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
367658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
367758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
367858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
36797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
36807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
36817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3682234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3683234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
3684251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
3685234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
3686251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
3687a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3688ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // C++11 [temp.explicit]p10:
3689ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   Except for inline functions, [...] explicit instantiation declarations
3690ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   have the effect of suppressing the implicit instantiation of the entity
3691ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   to which they refer.
3692234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
3693251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
36941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3695afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
3696afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
3697afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
3698afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
3699afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3700afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3701afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
3702afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3703afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
3704d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3705afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
3706afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
3707025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
3708d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  // If we already have a definition, we're done.
3709d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  if (VarDecl *Def = Var->getDefinition()) {
3710d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // We may be explicitly instantiating something we've already implicitly
3711d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // instantiated.
3712d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3713d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                       PointOfInstantiation);
3714d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    return;
371595e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
3716f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
37177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3718d69f37b5822420e3c3a1b2e875b122aca8248533Alp Toker  if (Inst.isInvalid())
37197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
37201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
37227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
37237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
37245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
372562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
372665173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling  SavePendingLocalImplicitInstantiationsRAII
372765173e04eacb68ff89a58fbff14979eb318896c9Bill Wendling      SavedPendingLocalImplicitInstantiations(*this);
37288155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
37298155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
373062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
37318155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
37321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
37347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3735ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  ContextRAII PreviousContext(*this, Var->getDeclContext());
37367bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
3737ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
37381028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3739ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec)
3740ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3741d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                          TemplateArgs));
3742d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  else if (Var->isStaticDataMember() &&
3743d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith           Var->getLexicalDeclContext()->isRecord()) {
3744d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // We need to instantiate the definition of a static data member template,
3745d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // and all we have is the in-class declaration of it. Instantiate a separate
3746d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // declaration of the definition.
3747d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(),
3748d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                          TemplateArgs);
3749d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl(
3750d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VarSpec->getSpecializedTemplate(), Def, 0,
3751d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray()));
3752d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    if (Var) {
3753d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      llvm::PointerUnion<VarTemplateDecl *,
3754d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                         VarTemplatePartialSpecializationDecl *> PatternPtr =
3755d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          VarSpec->getSpecializedTemplateOrPartial();
3756d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      if (VarTemplatePartialSpecializationDecl *Partial =
3757d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith          PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>())
3758d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith        cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf(
3759d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith            Partial, &VarSpec->getTemplateInstantiationArgs());
3760d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3761d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Merge the definition with the declaration.
3762d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      LookupResult R(*this, Var->getDeclName(), Var->getLocation(),
3763d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                     LookupOrdinaryName, ForRedeclaration);
3764d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      R.addDecl(OldVar);
3765d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      MergeVarDecl(Var, R);
3766d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
3767d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      // Attach the initializer.
3768d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith      InstantiateVariableInitializer(Var, Def, TemplateArgs);
3769d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    }
3770d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  } else
3771d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // Complete the existing variable's definition with an appropriately
3772d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    // substituted type and initializer.
3773d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs);
3774f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3775ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PreviousContext.pop();
37767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
37777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3778ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PassToConsumerRAII.Var = Var;
3779d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(),
3780d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith                                       OldVar->getPointOfInstantiation());
37817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3782ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3783ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // This variable may have local implicit instantiations that need to be
3784ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiated within this scope.
3785ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PerformPendingInstantiations(/*LocalOnly=*/true);
3786ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
37877bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
37887bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
37897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
37908155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
37918155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
37928155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
37937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
37941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
379562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
37961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37978155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
37988155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
3799ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "VTableUses should be empty before it is discarded.");
38008155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
38018155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
38027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
38038155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
3804ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "PendingInstantiations should be empty before it is discarded.");
380562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
38061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3807a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3808815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3809090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
3810090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
3811090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
3812090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
38131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381490ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
381554b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
3816a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3817090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
3818090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
381972f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
382072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
3821cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
3822090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
3823030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
3824030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
3825030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
3826030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
3827030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
38283fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
3829a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38303fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
38313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
383276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
383398a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      SmallVector<UnexpandedParameterPack, 4> Unexpanded;
38343fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
383598a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
38363fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
3837d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
3838dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
3839a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
38403fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
3841a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
3842a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
3843d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
38443fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
38453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
38463fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
38473fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
38483fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
38493fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
3850a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3851a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
3852cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
38533fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
38543fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
38553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
38565b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
38575b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
38585b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
38593fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
38603fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
38613fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
38623fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
38633fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
386476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
3865a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
3866a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
38673fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
38683fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
38693fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
38703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
38713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
38723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
38733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
38746df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
38755b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
38763fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
38773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
38783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
38793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
38803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
38813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
3882a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
38843fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
3885a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38863fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
38873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
38883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
38896b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
38905b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
38915b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
38925b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
38936b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
38946b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
3895090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3896a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3897090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
389876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
389976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
390076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
390176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
390276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
390376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
39049db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
3905802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
3906802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
3907802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
39086df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
390976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
39105b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
391176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
391276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
39135b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
391476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
3915090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
3916b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
391700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
391800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
391900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
3920b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
3921b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3922b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
3923b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
3924b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
39251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39265b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
39276df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
392800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
392900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
3930b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
393100eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
393200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
393300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
3934b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
3935b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3936b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
39376df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
3938b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
39396df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
39405b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
39416df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
3942090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3943090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
39449db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
39459db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
3946090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
39479db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
394890ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
3949090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3950090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
39511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3952090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
3953d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
3954090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
3955090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
395693c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
39579db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
3958090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
3959090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
396052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
396152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
396252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
396352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
396452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
396552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
396652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
396752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
396852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
396952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
397052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
397152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
397252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
397352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
397452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
39750d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
39760d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
39770d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3978a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
39790d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
39800d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
39810d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
39820d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
39830d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3984a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
39850d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
39860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
39870d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3988a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3989ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3990ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3991a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3992ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3993ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3994ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3995ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3996ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3997ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3998ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3999ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
4000a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4001ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
4002ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
4003ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
400452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
400552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
400652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
400752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
400852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
400952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
401052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
401152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
401252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
401352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
401452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
401552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
401652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
401752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
401852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
401952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
402052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
402152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
402252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
402352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
402452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
402552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
402652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
402752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
402852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
402952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
403052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
403152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
403252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
403352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
403452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
403552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
403652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
403752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
403852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
403952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
404052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
404152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
404252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4043ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
4044ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
4045ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
4046ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
4047ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
4048ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4049ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
4050ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
4051ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
4052ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
4053ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
4054ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
40557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
40567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
40577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
4058ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
40597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
40607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
40617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
40620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
40630d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
4064ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
40650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
40660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
406752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
406852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
406952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
407052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
407152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
407252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
407352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
407452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
407552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
407652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
407752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
407852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
407952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
408052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
408152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4082ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
4083ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
4084815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
40850d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
40867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
40877ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
40887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
40897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
40907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
40917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
40927ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
40937ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
40947ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
40950d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
40960d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
40970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
40980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
4099815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
41000d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
41010d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
41021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
410452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
41051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
410752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
4108815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
410952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
411052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
4111815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
41127caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
411352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
411452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
411552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
411652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
411752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
4118a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
41190d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
41200d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
41210d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
4122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
4123ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
4124ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
4125ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
4126ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
4127d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
4128d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
4129d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
41301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
4131d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
4132d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
4133d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
41341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4135ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
4136ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
4137ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4138ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
4139ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
4140ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4141815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
4142815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
4143815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4144815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4145815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
41461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
4147815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
4148815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
4149815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
4150815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
4151815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
4152815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
4153815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4154815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
4155815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4156815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
415702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
415802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
415902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
416002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
41617c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
4162e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
416302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
41647c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
416502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
416602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
416702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
416802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
4169ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
4170ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
4171815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4172815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
4173815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
4174815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
4175815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
4176815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4177815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
4178815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
4179815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
4180815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
4181815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
4182815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
4183815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4184815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
4185815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
4186815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4187815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
4188815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
4189815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4190041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4191041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// \p EnumConstantDecl for \p KnownValue (which refers to
4192041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4193041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4194041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// this mapping from within the instantiation of <tt>X<int></tt>.
41957c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
4196e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
4197815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
4198a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // FIXME: Parmeters of pointer to functions (y below) that are themselves
4199a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // parameters (p below) can have their ParentDC set to the translation-unit
4200a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // - thus we can not consistently check if the ParentDC of such a parameter
4201a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // is Dependent or/and a FunctionOrMethod.
4202a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // For e.g. this code, during Template argument deduction tries to
4203a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // find an instantiated decl for (T y) when the ParentDC for y is
4204a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // the translation unit.
4205a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //   e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {}
4206a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //   float baz(float(*)()) { return 0.0; }
4207a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //   Foo(baz);
4208a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // The better fix here is perhaps to ensure that a ParmVarDecl, by the time
4209a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // it gets here, always has a FunctionOrMethod as its ParentDC??
4210a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  // For now:
4211a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //  - as long as we have a ParmVarDecl whose parent is non-dependent and
4212a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //    whose type is not instantiation dependent, do nothing to the decl
4213a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  //  - otherwise find its instantiated decl.
4214a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali  if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() &&
4215a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali      !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType())
4216a3d311e468bce37defb97ed75105f8d36942b651Faisal Vali    return D;
421780f2b2e693422f84ec3735f16a08614a527b0bc5Rafael Espindola  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
42186d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
42197bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
42207bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
42212bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
42222bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
4223d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4224d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
4225d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
4226a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
422757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
422857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
422957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
4230a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
42319a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
42329a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
423357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
423457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
423557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
4236dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // If we're performing a partial substitution during template argument
4237dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // deduction, we may not have values for template parameters yet. They
4238dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // just map to themselves.
4239dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4240dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov        isa<TemplateTemplateParmDecl>(D))
4241dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov      return D;
4242dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov
424329a46e63490176608efe13f13b293a6ce9862059Serge Pavlov    if (D->isInvalidDecl())
424429a46e63490176608efe13f13b293a6ce9862059Serge Pavlov      return 0;
424529a46e63490176608efe13f13b293a6ce9862059Serge Pavlov
424657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
424757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
424857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
4249a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
425057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
425157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
4252a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
425357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
425457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
42552bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
4256815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4257ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // For variable template specializations, update those that are still
4258ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // type-dependent.
4259ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec =
4260ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          dyn_cast<VarTemplateSpecializationDecl>(D)) {
4261ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
4262ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &VarTemplateArgs =
4263ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getTemplateArgsInfo();
4264ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (TemplateSpecializationType::anyDependentTemplateArguments(
4265ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            VarTemplateArgs, InstantiationDependent))
4266ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      D = cast<NamedDecl>(
4267ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4268ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return D;
4269ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
4270ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4271e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4272e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
4273e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
4274a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
42752c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
42762c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
4277e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
42782c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
42792c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
42802c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
42812c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
42822c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
4283ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
42842c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
42852c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
42862c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
42872c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
42882c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
42892c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
42902c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
42912c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
4292ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
42932c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
42942c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
42952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
42962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
42972c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
42982c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
42992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
43002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
43012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
4302ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
43032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
43042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
43052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
4306e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
4307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
43082c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
43092c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
43102c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
43112c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
43122c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
43132c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
431452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
4315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
43162c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
431752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
43188b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
4319e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
4320e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
4321e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
432252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4323e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
4324e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
4325a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
43267c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
43271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
432844c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
43291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4330815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
4331815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
4332815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
4333815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
43347c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
43353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
43363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
4337eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
43383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
43397c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
43407c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
43413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
43423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
4343eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
4344eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
43453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
43463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
43473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
4348a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
4349a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
43507c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
43517c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
43527c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
4353815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
4354815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
435517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
43563bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
4357815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
4358815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
4359815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
4360815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
4361815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4362815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
4363815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
4364815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4365815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
43661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
436717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
436817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
4369815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
43701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4371eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
4372eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
4373eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
4374eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
4375eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
4376eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
4377eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
4378eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
4379a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
4380eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
4381eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
4382eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
4383eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
4384eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
4385eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
4386eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4387eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
43880724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
43890724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
43900724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
43910724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
43920724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
43930724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
43940724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
43950724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
43960724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
43970724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
43980724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
43990724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
44000724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
44010724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
4402eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
4403eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
4404eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
4405eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
4406eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
4407a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4408815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
4409815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
4410815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4411815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
4412815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4413d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
44141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
4415d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
44168155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
44176e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
44186e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
4419b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    SmallVector<PendingImplicitInstantiation, 4> Pending;
44206e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
44216e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
44226e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
44236e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
4424a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
442560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
442662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
442760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
442860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
442960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
443062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
443162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
443260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
443360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
443460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
443560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
44361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
44387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
4439f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4440f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
444158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
444258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
444358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
444458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
44457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
44467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
44471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4448ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate variable definitions
44497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
4450ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4451ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert((Var->isStaticDataMember() ||
4452ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            isa<VarTemplateSpecializationDecl>(Var)) &&
4453ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Not a static data member, nor a variable template"
4454ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           " specialization?");
4455c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
4456291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
4457291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
4458ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
4459291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
4460291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4461291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
4462291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
4463ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
4464291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
4465b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
4466291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
4467291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
446858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
446958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
447058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
447158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
4472ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
4473291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
4474291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
4475291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
4476291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4477ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4478ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        "instantiating variable definition");
447958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
448058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
4481ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4482ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate static data member definitions or variable template
4483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // specializations.
4484ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                  DefinitionRequired);
4486d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
4487d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
44880c01d18094100db92d38daa923c95661512db203John McCall
44890c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
44900c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
44910c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
44920c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
44930c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
44940c01d18094100db92d38daa923c95661512db203John McCall
44950c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
44960c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
44970c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
44980c01d18094100db92d38daa923c95661512db203John McCall      break;
44990c01d18094100db92d38daa923c95661512db203John McCall    }
45000c01d18094100db92d38daa923c95661512db203John McCall  }
45010c01d18094100db92d38daa923c95661512db203John McCall}
4502