SemaTemplateInstantiateDecl.cpp revision a41c97a5d1912ffd184381d269fd8e5a25ee5e59
18dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
28dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
38dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//                     The LLVM Compiler Infrastructure
48dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
58dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// This file is distributed under the University of Illinois Open Source
68dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor// License. See LICENSE.TXT for details.
78dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
88dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
98dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//  This file implements C++ template instantiation for declarations.
108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//
118dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor//===----------------------------------------------------------------------===/
122d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
13aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor#include "clang/AST/ASTConsumer.h"
148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/ASTContext.h"
158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclTemplate.h"
168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/DeclVisitor.h"
170c01d18094100db92d38daa923c95661512db203John McCall#include "clang/AST/DependentDiagnostic.h"
188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor#include "clang/AST/Expr.h"
19a88cfbfac9bbcbb9858f048d6d73a48711d8e93dDouglas Gregor#include "clang/AST/ExprCXX.h"
2021ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall#include "clang/AST/TypeLoc.h"
2183ddad3ab513f5d73698cf9fbeb4ed3f011bc3b9Douglas Gregor#include "clang/Lex/Preprocessor.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Lookup.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/PrettyDeclStackTrace.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Sema/Template.h"
258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregorusing namespace clang;
278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
28b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
29b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              DeclaratorDecl *NewDecl) {
30c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
31c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
32a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
33c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
34a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
35c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                          TemplateArgs);
36a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
38b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
39a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
41b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
42b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
43b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
44b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCallbool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
45b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall                                              TagDecl *NewDecl) {
46c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!OldDecl->getQualifierLoc())
47c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    return false;
48a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
49c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc NewQualifierLoc
50a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
51c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                        TemplateArgs);
52a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
53c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (!NewQualifierLoc)
54b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return true;
55a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NewDecl->setQualifierInfo(NewQualifierLoc);
57b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  return false;
58b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall}
59b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
607b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins// Include attribute instantiation code.
617b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins#include "clang/Sema/AttrTemplateInstantiate.inc"
627b9ff0c09025dcbe48ec7db71330e2066d1e1863DeLesley Hutchins
63f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
64f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
65f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) {
66f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr()) {
67f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    // The alignment expression is a constant expression.
68f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
69f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
70f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (!Result.isInvalid())
71f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(),
72f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
73f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
74f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(),
75f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         TemplateArgs, Aligned->getLocation(),
76f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                         DeclarationName());
77f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Result)
78f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      S.AddAlignedAttr(Aligned->getLocation(), New, Result,
79f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                       Aligned->getSpellingListIndex(), IsPackExpansion);
80f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
81f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
82f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
83f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smithstatic void instantiateDependentAlignedAttr(
84f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
85f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned, Decl *New) {
86f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Aligned->isPackExpansion()) {
87f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
88f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
89f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
90f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
91f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
92f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (Aligned->isAlignmentExpr())
93f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
94f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
95f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  else
96f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
97f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                      Unexpanded);
98f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
99f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
100f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // Determine whether we can expand this attribute pack yet.
101f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  bool Expand = true, RetainExpansion = false;
102f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  Optional<unsigned> NumExpansions;
103f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  // FIXME: Use the actual location of the ellipsis.
104f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  SourceLocation EllipsisLoc = Aligned->getLocation();
105f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
106f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        Unexpanded, TemplateArgs, Expand,
107f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith                                        RetainExpansion, NumExpansions))
108f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    return;
109f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
110f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  if (!Expand) {
111f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
112f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true);
113f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  } else {
114f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    for (unsigned I = 0; I != *NumExpansions; ++I) {
115f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I);
116f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
117f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    }
118f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith  }
119f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith}
120f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith
1211d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCallvoid Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
12223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            const Decl *Tmpl, Decl *New,
12323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LateInstantiatedAttrVec *LateAttrs,
12423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                            LocalInstantiationScope *OuterMostScope) {
125cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
126cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt       i != e; ++i) {
127cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr *TmplAttr = *i;
12823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins
1294ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    // FIXME: This should be generalized to more than just the AlignedAttr.
130f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
131f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    if (Aligned && Aligned->isAlignmentDependent()) {
132f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
133f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith      continue;
1344ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth    }
1354ced79f0971592e6e7122037de69ee9ae534ce72Chandler Carruth
136f6565a9f7318b1ca6ea9510003dde7b89696daabRichard Smith    assert(!TmplAttr->isPackExpansion());
13723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    if (TmplAttr->isLateParsed() && LateAttrs) {
13823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // Late parsed attributes must be instantiated and attached after the
13923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      // enclosing class has been instantiated.  See Sema::InstantiateClass.
14023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LocalInstantiationScope *Saved = 0;
14123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      if (CurrentInstantiationScope)
14223323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins        Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
14323323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins      LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
14423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    } else {
145cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      // Allow 'this' within late-parsed attributes.
146cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      NamedDecl *ND = dyn_cast<NamedDecl>(New);
147cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXRecordDecl *ThisContext =
148cafeb948e6067b8dc897c441da522367917b06f9Richard Smith          dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
149cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
150cafeb948e6067b8dc897c441da522367917b06f9Richard Smith                                 ND && ND->isCXXInstanceMember());
151cafeb948e6067b8dc897c441da522367917b06f9Richard Smith
1525bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
1535bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
15431c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
15531c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
15623323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
157d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
158d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
159d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1604f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1614f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
162b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1634f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1644f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1654f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
16657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
16757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
16857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
16957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
17057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
17157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
17257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
17357ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1744f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
175b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1764f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1774f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1783dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1840cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1850cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1863dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1873dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1883dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1893dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1903dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1913dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
195a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
196561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
197836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
198ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
199ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
200ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
2018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
202a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
2038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
204b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
205b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
2068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // HACK: g++ has a bug where it gets the value kind of ?: wrong.
209b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // libstdc++ relies upon this bug in its implementation of common_type.
210b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // If we happen to be processing that implementation, fake up the g++ ?:
211b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // semantics. See LWG issue 2141 for more information on the bug.
212b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
213b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
214b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
215b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DT->isReferenceType() &&
216b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
217b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
218b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      D->getIdentifier() && D->getIdentifier()->isStr("type") &&
219b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
220b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    // Fold it to the (non-reference) type which g++ would have produced.
221b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    DI = SemaRef.Context.getTrivialTypeSourceInfo(
222b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DI->getType().getNonReferenceType());
223b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith
2248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
225162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
226162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
227162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
228162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
229162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
230162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
231162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
2328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
235cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
236cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
237cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
238cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
239c61361b102fcb9be7b64cc493fb797ea551eb8e7Douglas Gregor    if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
240cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
24183972f128e9218c051692bf96361327a701aeb79John McCall      assert(!newTag->hasNameForLinkage());
242162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
243cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
244d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
245a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
246ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
2477c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2487c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
249b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
250b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
251a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2525df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
2535df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2545df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
2555df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
2565df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2575df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    Typedef->setPreviousDeclaration(InstPrevTypedef);
2585126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2595126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2601d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
261d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
26246460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2658dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
267162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
271162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
272162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
273162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
293ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
2943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2953bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
2963bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
2973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
3013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
3023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
3033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
3043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
3063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
3083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
3093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
3103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
3123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
3143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
315a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
3173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
319162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
320162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo// FIXME: Revise for static member templates.
3223d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
323567f917df048d42732997a479b2b257403fc88efLarisse Voufo  return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false);
324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
326567f917df048d42732997a479b2b257403fc88efLarisse VoufoDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D,
327567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                             bool InstantiatingVarTemplate) {
328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3299901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3309901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3319901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3329901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3339901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3349901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3359901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
336ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
337a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3380a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3390a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3400a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3410a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3423d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
344c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
345c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
346c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
347c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
348c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
349a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
350a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  DeclContext *DC = Owner;
351a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
352a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
353a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration.
355a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
3563d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                 DI->getType(), DI, D->getStorageClass());
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3599aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3619aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3629aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3639aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
365ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
367bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
368a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
369567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                     StartingScope, InstantiatingVarTemplate);
3703d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
3713d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
3723d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
3736206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
3746206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
3756206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
3766206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
3776206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
3786206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
3796206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
3806206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
3818dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
3828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
383a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
384561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
385836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
38607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
38707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
38807fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
389a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
39007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
39107fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
3928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
3938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
3948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
3958dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
3968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
3978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
3988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
39907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
402b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
403b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
410f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
411f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
412f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
415ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
420e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
42307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
42407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
429ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
430703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
433663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
434663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
435f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
436663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
439a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
440be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
441be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
442be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
443f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
444f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
447f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
448f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
449a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
4509901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
4519901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
4527a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
4539901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
4548dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
456f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
45746460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
458f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
46376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCallDecl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
46476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  bool Invalid = false;
46576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
46676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
46776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (DI->getType()->isVariablyModifiedType()) {
46876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
46976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    << D->getName();
47076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Invalid = true;
47176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else if (DI->getType()->isInstantiationDependentType())  {
47276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
47376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           D->getLocation(), D->getDeclName());
47476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    if (!DI) {
47576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      DI = D->getTypeSourceInfo();
47676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
47776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    } else if (DI->getType()->isFunctionType()) {
47876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      // C++ [temp.arg.type]p3:
47976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   If a declaration acquires a function type through a type
48076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   dependent on a template-parameter and this causes a
48176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   declaration that does not use the syntactic form of a
48276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   function declarator to have function type, the program is
48376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      //   ill-formed.
48476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
48576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      << DI->getType();
48676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      Invalid = true;
48776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    }
48876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  } else {
48976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
49076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  }
49176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
49276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  MSPropertyDecl *Property = new (SemaRef.Context)
49376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall      MSPropertyDecl(Owner, D->getLocation(),
49476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getDeclName(), DI->getType(), DI,
49576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getLocStart(),
49676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                     D->getGetterId(), D->getSetterId());
49776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
49876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
49976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall                           StartingScope);
50076da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50176da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  if (Invalid)
50276da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall    Property->setInvalidDecl();
50376da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50476da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Property->setAccess(D->getAccess());
50576da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  Owner->addDecl(Property);
50676da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
50776da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall  return Property;
50876da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall}
50976da55d3a49e1805f51b1ced7c5da5bcd7f759d8John McCall
51087c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
51187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
51287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
51387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
51487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
51587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
51687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
517b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
518a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
519b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
520b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
521b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
522a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
523b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
524b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
52587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
52640e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
52787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
52887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
52940e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
53087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
53187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
53487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
53587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
53687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
53787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
53887c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
54002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
54106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
54232f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5434fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5454fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5464fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5474fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5484fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5494fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5504fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5514fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5524fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5534fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5544fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
55506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
556fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
557d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
5580216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
55906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
561a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56206245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5639a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
56406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
56506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
566a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
567a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
56806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
56906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
57032f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
571af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
572af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
573af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
574af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
575af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
57606245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
579a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
58006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
5815fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
5829a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
58302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
58402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
585fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
586fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
5878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
5888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
590f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
591f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
592f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
595ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
5968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
5978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
5988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
599e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
6009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
601e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
602e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
603e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
6048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
60738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
60838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
60938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
61038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
61138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
61238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
61338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
61438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
61538f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
616ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
61838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
619a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6201274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
621f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6221274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6231274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6241274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6251274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
626f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
627f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
628f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6291274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
630f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
631f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
632f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6331274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6341274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6351274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6361274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6371274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6381274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6395b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6405b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
641f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
64206c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
643b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
64417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
645f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6464ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
6474ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
6484ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
6494ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
6504ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
6514ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
6524ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6534ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
6544ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
6554ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
6564ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
6574ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
6584ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
6594ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
6608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
66196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
66296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
663a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
664f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
665f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
666f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
667f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: There appears to be no wording for what happens for an enum defined
66838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // within a block scope, but we treat that much like a member template. Only
66938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // instantiate the definition when visiting the definition in that case, since
67038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // we will visit all redeclarations.
67138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (!Enum->isScoped() && Def &&
67238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith      (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
6734ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
674f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
675f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
676f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
677f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
678f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
679f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
680f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
681f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6821af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
6831af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
6841af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
6855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
6868dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
688f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
689f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
6908dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
6918dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
69260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
693ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
694f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
696f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
6971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
698ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
699ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
7008dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7018dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
7028dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
7038dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
7048dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
7058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7128dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7138dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7148dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7188dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
720581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7215b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7223b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
72317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
724d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
726a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
727f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
728f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
72996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
73096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
731581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
73296084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
736f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
737f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
738f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
7399ff2b421f352fe0a0769c0a2a75af922c147b878Dmitri Gribenko                        Enumerators,
740fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
7418dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7428dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7436477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
744b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7456477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7466477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
747e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
74893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
74993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
750550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
751550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7522a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
753e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
754ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
756d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
757e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
758e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
75993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
76093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
76193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
76293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
763c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
764c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
765c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
766c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
767c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
768c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
76993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
77093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
77193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
77293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
77393ba8579c341d5329175f1413cdc3b35a36592d2John McCall
774ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
77537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
7763bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
7773bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
77837574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
77937574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
78037574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
78137574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
78237574f55cd637340f651330f5cfda69742880d36Nick Lewycky
78393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
78493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
78593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
78693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
78793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
789c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
791c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
79393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
79493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
79593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
79693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
79793ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
79893ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
79993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
80193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
80893ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
80993ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
81093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall
812c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8141eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
815c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
81893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
819c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
821c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
822c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
823c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
824c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
825c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
826c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
827c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
828c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
829a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
830c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
831c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
832c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
833c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
834c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
835c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
836c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
837c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
838c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
839c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
840c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
841c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
842c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
843c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
844c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
845c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
846c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
84793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
84893ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
84993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
85093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
85193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
852a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
853c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
854c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
855c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
856c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
857c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
858c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
859c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
86093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
86193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
86293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
863c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
864c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
86593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
86693ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
86793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
86893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
86993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
870e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
87193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
872ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
873ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
874f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
875e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
876c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
877c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
878b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
879e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
88093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
88193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
88293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
883e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
884ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
88593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
886ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
887ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
888ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
889ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
890ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
89122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Inst->setObjectOfFriendDecl();
89293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
89393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
89493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
895e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
89637574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
89737574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
89893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
900f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
9013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
90224bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
903ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
904259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
90593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9061b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9074c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9084c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
909e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
910259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
911a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9124c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9134c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9144c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9154c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9164c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
917e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
918d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
919d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
920d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
921d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
922d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
924d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
925d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
926d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
927d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
928d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
929d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
930e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
931e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
932e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
933d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9347974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9357974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
936ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
937a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
938ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
939ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
941ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
9423bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
943ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
944a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
945ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
9463bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
947ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
948ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
949a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
950d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
951d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
952d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
953d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
954d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9557974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9567974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
957ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
958ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->getTemplatedDecl()->isStaticDataMember() &&
959ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
960ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
961ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template, which
962ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will contain the instantiations of the template parameters.
963ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
964ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = D->getTemplateParameters();
965ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
966ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
967ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return NULL;
968ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
969ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *Pattern = D->getTemplatedDecl();
970ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *PrevVarTemplate = 0;
971ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
972ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Pattern->getPreviousDecl()) {
973ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
974ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Found.empty())
975ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
976ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
977ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
978dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  VarDecl *VarInst =
979567f917df048d42732997a479b2b257403fc88efLarisse Voufo      cast_or_null<VarDecl>(VisitVarDecl(Pattern,
980567f917df048d42732997a479b2b257403fc88efLarisse Voufo                                         /*InstantiatingVarTemplate=*/true));
981ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
982ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext *DC = Owner;
983ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
984ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *Inst = VarTemplateDecl::Create(
985ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
986ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarInst, PrevVarTemplate);
987ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarInst->setDescribedVarTemplate(Inst);
988ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
989ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Inst->setAccess(D->getAccess());
990ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate)
991ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setInstantiatedFromMemberTemplate(D);
992ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
993ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (D->isOutOfLine()) {
994ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
995ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
996ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
997ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
998ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Owner->addDecl(Inst);
999ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1000ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!PrevVarTemplate) {
1001ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Queue up any out-of-line partial specializations of this member
1002ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template; the client will force their instantiation once
1003ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the enclosing class has been instantiated.
1004ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1005ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    D->getPartialSpecializations(PartialSpecs);
1006ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1007ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (PartialSpecs[I]->isOutOfLine())
1008ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        OutOfLineVarPartialSpecs.push_back(
1009ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            std::make_pair(Inst, PartialSpecs[I]));
1010ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
1011ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1012ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Inst;
1013ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1014ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1015ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1016ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *D) {
1017ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(D->isStaticDataMember() &&
1018ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Only static data member templates are allowed.");
1019ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1020ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1021ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1022ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Lookup the already-instantiated declaration and return that.
1023ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1024ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(!Found.empty() && "Instantiation found nothing?");
1025ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1026ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1027ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(InstVarTemplate && "Instantiation did not find a variable template?");
1028ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1029ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplatePartialSpecializationDecl *Result =
1030ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1031ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return Result;
1032ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
1033ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1034ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
1035ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
10367974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
1037d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1038550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
1039550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
1040a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
1041550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
10422a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
1043895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
1044d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
1045d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
1047d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
1048a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1049a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
1050a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1051a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1052a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
1053a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
1054a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1055a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
1056a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
1057a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1058a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
1059d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
1060d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1062d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1063a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1064a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
106537d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1066a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1067a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1068e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1069b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1070b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1071e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1072e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1073e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1074b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1075a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1076a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1077b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
10781f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1079a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
10801f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
10811f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
10821f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
10831f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1084b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1085d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1086d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1087d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1088d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1089d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
1090d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1091d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1092ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
10937c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1094ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
10956c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
10966c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
10976c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
10986c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1099d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1100d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1102ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1103ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1104b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1105b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1106b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1107b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1108b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1109d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1110eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1111eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1112eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1113eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1114eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1115d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1116f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1117d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
111802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
111902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
112022050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith  if (D->getFriendObjectKind())
112122050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Record->setObjectOfFriendDecl();
112202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
11239901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
11249901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
11259901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
11267a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
11279901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
11289901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1129d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
113017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1131d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1132d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1133d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
113471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
113571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
113671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
113771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
113871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
113971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
114071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
114171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
114271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1143bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1144bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1145bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1146bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1147bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1148bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1149bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1150bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1151bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1152bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  return Context.getFunctionType(NewFunc->getResultType(),
11530567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                 NewFunc->getArgTypes(), NewEPI);
115471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
115571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
115602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
115702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
115802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
115902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
116002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
11617557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1162a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1163127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1164127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1165127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1166b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1167c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11691e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
11702c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1171c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(),
11722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1174127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
11752c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
11762c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1177127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1179b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1180b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1181b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1182b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1183b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1184b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
118579c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1186b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1187a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
118879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
11892a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
119264b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
119321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11942dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
119571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1196fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1197c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1198c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1199c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1200c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1201c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1202c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1203d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1204d325daa506338ab86f9dd468b48fd010673f49a6John McCall
120568b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
1206a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // in the enclosing namespace; otherwise we need to find the instantiated
1207a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // context.
120868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
1209a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl()) {
121068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1211a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    SemaRef.adjustContextForLocalExternDecl(DC);
1212a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (isFriend && QualifierLoc) {
1213d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1214c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1215d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1216d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1217d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1218a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
12197c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1220d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
122168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
122202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1223ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1224635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
1225459ef03126f9f0420efb3355e3b2ed3c1fdfb38aRafael Espindola                           D->getCanonicalDecl()->getStorageClass(),
1226af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
122786c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1228de9ed71c696bee936a21323f61548164de0eda13Enea Zaffanella  Function->setRangeEnd(D->getSourceRange().getEnd());
1229b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1230d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1231d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1232d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1233c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1234c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1235b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1236a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (D->isLocalExternDecl())
1237a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    Function->setLocalExternDecl();
1238a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
1239b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1240a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
1241b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1242b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1243b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1244b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1245b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
12461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1247e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
1248c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  for (unsigned P = 0; P < Params.size(); ++P)
1249c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (Params[P])
1250c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      Params[P]->setOwningFunction(Function);
12514278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
125202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1253ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1254a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1255a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1256a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1257a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1258a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1259a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1260a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1261a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1262a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1263a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1264a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1265a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1266a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1267a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1268a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1269d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1270a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1271a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1272a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1273a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1274b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1275b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1276d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1277d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1278d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1279d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1280d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1281d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1282d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
128366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
128466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1285c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1286838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1287910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
1288c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.begin(),
1289c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                             Innermost.size()),
12901e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
129180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
129280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
129380f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
129480f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
129580f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
129680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
129780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1298d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
129902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1300a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1301e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1302e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1304af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1305a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1306a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
1307a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      SemaRef, Function->getDeclName(), SourceLocation(),
1308a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
1309a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                             : Sema::LookupOrdinaryName,
1310a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
13116826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1312af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1313af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1314af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1315af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1316af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
131722050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Function->setObjectOfFriendDecl();
1318af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1319af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1320af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1321af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1322e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1323e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1324e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1325af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1326af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1327af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1328af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1329af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1330af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1331af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1332af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1333af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1334af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1335af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1336af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1337af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1338af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1339af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1340a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1341af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1342af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1343af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1344a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1345a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1346a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
13476826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1348a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1349a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1350a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1351a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1352a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13536826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13546826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1355a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1356a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13579f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
13582c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1359e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
136076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
136176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
136276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
136376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1364a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1365a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1366d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
136722050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    PrincipalDecl->setObjectOfFriendDecl();
13681b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1369ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137077535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1371ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
137253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
137353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
137453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
137553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
137653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
137753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
137853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
137953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
138053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
138153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
138280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((!SemaRef.getLangOpts().CPlusPlus11 ||
138353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
138453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
138553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
138653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1387238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1388238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1389238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
139010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1391238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
139253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
139380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                     SemaRef.getLangOpts().CPlusPlus11 ?
139453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
139553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1396238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
139780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        if (!SemaRef.getLangOpts().CPlusPlus11)
139853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1399a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1400238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1401238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1402238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1403238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1404238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
140513a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
140613a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1407ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1408ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
140980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          if (!SemaRef.getLangOpts().CPlusPlus11 &&
141053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1411ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1412ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1413ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1414ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1415ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1416ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1417ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1418ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1419ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1420ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1421ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1422ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1423ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1424238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
14256a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
142610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
142753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
142880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                           SemaRef.getLangOpts().CPlusPlus11 ?
142953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
143053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1431238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
14326a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
143380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith              if (!SemaRef.getLangOpts().CPlusPlus11)
143453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1435238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1436238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1437238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1438238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1439238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1440a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1441a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
1442a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (Function->isLocalExternDecl() && !Function->getPreviousDecl())
1443a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1444a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
144576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
144676d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
144776d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
144876d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1449eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1450e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1451e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14522dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1453d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1454d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1455af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1456af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
14576b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1458d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1461d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1462c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14641e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
14652c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
1466c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith      = FunctionTemplate->findSpecialization(Innermost.begin(),
1467c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                             Innermost.size(),
14682c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14706b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
14712c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
14722c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
14736b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
14746b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1475b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1476b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1477b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1478b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1479b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1480b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
148179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1482a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
148379c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
14842a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
148548dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
14864eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
14875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
14884eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
14894eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
14904eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
14914eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
14924eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
14934eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
14944eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
14954eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
14964eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
14974eab39f0745fb1949dbb40c4145771b927888242John McCall    }
14984eab39f0745fb1949dbb40c4145771b927888242John McCall  }
14994eab39f0745fb1949dbb40c4145771b927888242John McCall
15005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1501dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
150221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
15032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
150471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
15052dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1506c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1507c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1508c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1509b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1510a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1511c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1512b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1513b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1514b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1515b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1516c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1517b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1518c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1519b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1520c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1521c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1522c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1523b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1524b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1525b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1526b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1527b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1528b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1529b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1530b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15312dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1532b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1533dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1535ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15362577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
153817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1540ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
154216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
154386c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
1544b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
15454841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // Claim that the instantiation of a constructor or constructor template
15464841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith    // inherits the same constructor that the template does.
1547b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1548b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            Constructor->getInheritedConstructor())) {
1549b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // If we're instantiating a specialization of a function template, our
1550b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // "inherited constructor" will actually itself be a function template.
1551b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      // Instantiate a declaration of it, too.
1552b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      if (FunctionTemplate) {
1553b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1554b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               !Inh->getParent()->isDependentContext() &&
1555b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith               "inheriting constructor template in dependent context?");
1556b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1557b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith                                         Inh);
1558b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (Inst)
1559b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1560b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1561b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        LocalInstantiationScope LocalScope(SemaRef);
1562b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith
1563b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // Use the same template arguments that we deduced for the inheriting
1564b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        // constructor. There's no way they could be deduced differently.
1565b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        MultiLevelTemplateArgumentList InheritedArgs;
1566b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1567b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        Inh = cast_or_null<CXXConstructorDecl>(
1568b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith            SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1569b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith        if (!Inh)
1570b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith          return 0;
1571b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith      }
15724841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
1573b5eb3f5bf383807103dc1377a124fd96ee21d02aRichard Smith    }
157417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
157517e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1576ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15772577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
157816573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
157965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
158065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1581ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15820130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1583f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
158486c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
15859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1586dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
158772fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola    StorageClass SC = D->isStatic() ? SC_Static : SC_None;
15882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1589ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
159072fdc8947e54be1a8dd36b03e24f112aba1241e1Rafael Espindola                                   SC, D->isInlineSpecified(),
159186c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1592dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15936b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1594d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1595d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1596d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1597c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1598c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1599b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1600d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1601d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1602d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1604d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1605d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1606d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1607d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1608d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1609d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1610d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1611d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1612d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1613d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1614d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1615d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1617d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1618b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1619b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
162022050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith      FunctionTemplate->setObjectOfFriendDecl();
1621b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1623d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
162466724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
162566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1626c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith    ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1627838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1628910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1629c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.begin(),
1630c95d413660756c474bc8f97e5b32edc7ddff3850Richard Smith                                                          Innermost.size()),
16311e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1632b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
163366724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
16342db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
163566724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1636a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
16387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
16397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1640b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
16414eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
16424eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
16434eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
16444eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
16454eab39f0745fb1949dbb40c4145771b927888242John McCall
1646b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
164722050f25e34ba0cd21ee2dc3d765951c48e27cdeRichard Smith    Method->setObjectOfFriendDecl();
1648b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
16497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16515545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
16525545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
16535545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
16544278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16555545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16565545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16575545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
16582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
16592577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
16602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1662b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1663b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1665dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1666dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1667dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1668dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
16696826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
16706826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1671dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16722dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1673af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
16742c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
167565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
16764ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
16774ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
16784ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
16791f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
16801f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
16811f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
16821f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
16831f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
16841f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
16851f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
16861f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
16871f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
168846460a68f6508775e98c19b4bb8454bb471aac24John McCall
16899eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
16909eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
16913bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1692ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1693ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
16943bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1695ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
16963bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
16971f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1698b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
16991f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17001f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17011f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1702b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
17031f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
17041f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17051f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
17061f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
17071f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
17081f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
17091f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
17101f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
17111f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17121f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
17131f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
17141f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
17151f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
17161f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
17171f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
17181f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1719b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1720eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
17212dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
17222dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17232dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1724615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1725dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1726615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1727615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
172803b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
172917e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
173003b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
173103b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1732bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
173365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1734bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1735bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
1736ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
173766874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
173866874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
17392dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
17402dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1741e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1742e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1743e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
17444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
17451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1746e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1747344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1748344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
17494fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
17504fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1751e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1752e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
17539a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1754a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17559d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
17569d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TypeSourceInfo *InstantiatedDefaultArg =
17579d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs,
17589d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                          D->getDefaultArgumentLoc(), D->getDeclName());
17599d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (InstantiatedDefaultArg)
17609d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Inst->setDefaultArgument(InstantiatedDefaultArg, false);
17619d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1762e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1763a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1764550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1765550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1766a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1767e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1768e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1769e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
177033642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
177133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
177233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
17736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
17745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
17755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
17766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1777a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
177833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
177933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
17806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
17816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1782a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
17836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
17846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
17856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
17866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
17876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
17886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1789a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
17906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
17916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
17926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1793a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
17966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
17986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
17996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
18006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1801a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
18036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
18046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
18056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
18066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
18076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
18086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
180939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
18106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
18115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
18126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1813a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
18166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
18176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1818dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
18196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1820dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
18216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
18226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1823a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
18246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1825a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
18266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1828a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
18306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
18316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
18326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1833a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
18346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
18356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1837a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
18396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
18406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
18416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
18426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
18436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
18446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1846a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
18486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
18496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
18516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
18526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
18546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
18556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
18566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
18576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1858a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
18596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
18606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
18616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1862a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
18646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
18656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
18666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1867a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
18706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
18716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1872a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
18736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
18746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
18756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1876a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1878a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
18796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
18806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
18816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
18826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
18836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
188433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1885a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
18876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1888a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1889ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1890ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1891a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1892a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
18936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
18946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
18956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
18966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
18976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
18986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1900ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
19016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1902a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1903a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1904a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
19056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1906a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19079a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
190833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
190933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1910a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19119d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
19129d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs);
19139d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!Value.isInvalid())
19149d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(Value.get(), false);
19159d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
1916a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1917a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1918550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1919550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
192033642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
192133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
192233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
19236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
19246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
19256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
19266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
19276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
19286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
19296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
19306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
19316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
19326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
19336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
19346964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
19356964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
19366964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
19376964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
19386964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
19396964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19400dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
19419106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
19429106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
19439106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
19449106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
19459106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
19466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
19476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
19496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19506964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
19516964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
19526964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
19536964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
19546964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
19556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
19566964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
19576964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19586964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
19596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
19606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
19616964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19626964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
19636964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19646964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
19666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
19706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
19716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
19746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
19766964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
19776964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
19786964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
1979dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
19806964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
19816964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
19826964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
19836964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
19846964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
19856964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
19866964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
19876964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
19896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
19906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
19916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
19926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
19936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
19946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith          return 0;
19956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
19966964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
19976964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19986964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
19996964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
20006964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
20016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
20026964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
20036964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
20046964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
20056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
20066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
20076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
20086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
20096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
20106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
20116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
20126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
20136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
20149106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
20159106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
20162a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
20179106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
20189106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
20196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
2020a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
2021a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20229106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
20236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
20246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
20256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
20276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
20286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
20306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
20316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
20326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
20336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
2034a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
20356964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
20366964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
20376964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
20389d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  if (D->hasDefaultArgument()) {
20399d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    NestedNameSpecifierLoc QualifierLoc =
20409d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateQualifierLoc();
20419d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    QualifierLoc =
20429d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
20439d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    TemplateName TName = SemaRef.SubstTemplateName(
20449d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
20459d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer        D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
20469d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer    if (!TName.isNull())
20479d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer      Param->setDefaultArgument(
20489d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          TemplateArgumentLoc(TemplateArgument(TName),
20499d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateQualifierLoc(),
20509d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer                              D->getDefaultArgument().getTemplateNameLoc()),
20519d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer          false);
20529d57b8dea3b139dc2e2976ffccef50c74ac03873David Majnemer  }
20539a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
2054a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2055a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
20569106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
20579106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2058a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
20599106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
20609106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
20619106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
206248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2063db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
2064db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
2065a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
206648c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
206748c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2068a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
2069db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
2070a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
2071a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
207248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
2073536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
2074536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
2075536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
2076536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
2077536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2078536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
207948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
208048c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
208148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2082ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
20831b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20841b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
20851b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
20861b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
20871b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
20881b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
20891b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
20901b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
20911b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
20925149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
20935149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
20945149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
20955149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
2096dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
20971b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20981b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
20991b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2100ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2101ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21029f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
21039f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
21049f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
21059f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
21069f54ad4381370c6b771424b53d219e661d6d6706John McCall
2107ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2108ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
21099f54ad4381370c6b771424b53d219e661d6d6706John McCall
2110ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
21118d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->getUsingLoc(),
21125149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2113ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
21148d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                       D->hasTypename());
2115ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21165149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
21175149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21189f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
21199f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
21209f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
21219f54ad4381370c6b771424b53d219e661d6d6706John McCall
21229f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
21238d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
21248d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella                                            D->hasTypename(), SS,
21259f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
21269f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
21279f54ad4381370c6b771424b53d219e661d6d6706John McCall
21289f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
21299f54ad4381370c6b771424b53d219e661d6d6706John McCall
21309f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
21318d030c7a6f36438f6c7dd977f8be0de0cc781ad5Enea Zaffanella      SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS,
2132ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2133ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
21349f54ad4381370c6b771424b53d219e661d6d6706John McCall
2135ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2136ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2137ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2138ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21399f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
21409f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
21419f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
21429f54ad4381370c6b771424b53d219e661d6d6706John McCall
2143c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2144c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2145c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
2146c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2147c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2148c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2149323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2150323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
21519f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
21529f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
21539f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
21549f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
21559f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2156b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2157b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
2158b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
2159b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
2160b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
2161b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
21629f54ad4381370c6b771424b53d219e661d6d6706John McCall
21639f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
21649f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
21659f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
21669f54ad4381370c6b771424b53d219e661d6d6706John McCall
21679f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
21689f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
21699f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2170323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2171323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2172323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
21739f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2174ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2175ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2176ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2177ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2178ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
21799f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
21809f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
2181ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2182ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21837ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
21855149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2186a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21875149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21885149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
21907ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
21925149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21937ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2194ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2195accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2196ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
21977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
21987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2199ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
22007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
22024469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2203ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2204ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
22067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
22077ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
22087ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
22097ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
22105149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
22115149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
22125149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
22130dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
2214a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
22150dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
22165149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
22171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2218ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2219ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2220ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
22211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
22229488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2223ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
22247ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
22257ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
22264469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2227ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2228ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
22290d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
22300dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
22310dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2232af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2233af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2234af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2235af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
22366b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
22376b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
2238af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2239af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2240af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2241af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
22426b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
22436b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
22446b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
22456b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
22466b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
22476b02009359a462ffe633696a4441313b462e6566Nico Weber  }
22486b02009359a462ffe633696a4441313b462e6566Nico Weber
2249af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
22506b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
22516b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2252af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2253af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2254af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2255af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2256af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2257af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2258af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2259af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2260af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2261af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2262af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2263af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2264c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey BataevDecl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2265c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev                                     OMPThreadPrivateDecl *D) {
22666af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  SmallVector<Expr *, 5> Vars;
22676af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev  for (ArrayRef<Expr *>::iterator I = D->varlist_begin(),
22686af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev                                  E = D->varlist_end();
2269c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev       I != E; ++I) {
2270c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take();
2271c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
22726af701f29be43e49a25ab098c79940ae4cbb69c7Alexey Bataev    Vars.push_back(Var);
2273c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  }
2274c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2275c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  OMPThreadPrivateDecl *TD =
2276c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev    SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2277c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2278c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev  return TD;
2279c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev}
2280c640058aa7f224a71ce3b1d2601d84e1b57f82d3Alexey Bataev
2281ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
2282ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitFunctionDecl(D, 0);
2283ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2284ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2285ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
2286ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return VisitCXXMethodDecl(D, 0);
2287ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2288ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2289ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2290ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("There are only CXXRecordDecls in C++");
2291ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2292ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2293ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *
2294ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanTemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2295ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    ClassTemplateSpecializationDecl *D) {
2296ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Only ClassTemplatePartialSpecializationDecls occur"
2297ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                   "inside templates");
2298ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2299ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2300ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2301ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *D) {
2302ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2303ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgumentListInfo VarTemplateArgsInfo;
2304ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2305ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert(VarTemplate &&
2306ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "A template specialization without specialized template?");
2307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2308ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the current template arguments.
2309ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2310ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2311ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2312ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2313ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2314ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
2315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2316ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2317ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this template.
2318ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2319ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  bool ExpansionIntoFixedList = false;
2320ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(
2321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, VarTemplate->getLocStart(),
2322ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2323ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted, &ExpansionIntoFixedList))
2324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2325ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Find the variable template specialization declaration that
2327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // corresponds to these arguments.
2328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2329ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2330ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          Converted.data(), Converted.size(), InsertPos))
2331ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a variable template specialization, return it.
2332ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return VarSpec;
2333ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                            VarTemplateArgsInfo, Converted);
2336ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2337ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2338ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoDecl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
2341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted) {
2342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is the variable for an anonymous struct or union,
2344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiate the anonymous struct/union type first.
2345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        return 0;
2349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
2352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2353ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                        D->getTypeSpecStartLoc(), D->getDeclName());
2354ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2355ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2357ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2358ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << D->isStaticDataMember() << DI->getType();
2360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2362ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2363ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the instantiated declaration
2364ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2365ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Converted.size());
2368ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  Var->setTemplateArgsInfo(TemplateArgsInfo);
2369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplate->AddSpecialization(Var, InsertPos);
2370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2371ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2372ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(D, Var))
2373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
2376a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     Owner, StartingScope);
2377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2378ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return Var;
2379ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2380ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2381ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2382ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("@defs is not supported in Objective-C++");
2383ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2384ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2385ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2386ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  // FIXME: We need to be able to instantiate FriendTemplateDecls.
2387ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2388ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               DiagnosticsEngine::Error,
2389ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman                                               "cannot instantiate %0 yet");
2390ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  SemaRef.Diag(D->getLocation(), DiagID)
2391ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman    << D->getDeclKindName();
2392ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2393ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  return 0;
2394ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2395ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2396ded9979a2997336cee8797deb6bb3194fccc2068Eli FriedmanDecl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2397ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman  llvm_unreachable("Unexpected decl");
2398ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman}
2399ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman
2400ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2401d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
24027e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
24032fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
24042fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
24052fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
24068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
24078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
24088dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2409e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2410e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2411e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2412e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2413e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2414e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2415e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2416ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2417e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2418e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2419e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2420e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
24215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2422e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2423e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2424e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2425e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2426bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2427e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
24289148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2429e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2430e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2431e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2432ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2433e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2434e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2435e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2436e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2437e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2438e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2439e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2441e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2442a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2443ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2444ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2445ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2446ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2447ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2448a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2449ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2450ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2451d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2452d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2453d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2454ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2455ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2456ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2457550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2458550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2459550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
24602a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2461a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2462ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2463ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2464ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2465ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2466ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2467d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2468a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2471c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2472c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2473c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2474c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2475c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2476c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2477e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2478e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2479a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2480ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
24825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2483a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2485a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2487ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2488d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2489ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2490ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2491ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2492ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2493ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2494910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2495910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2496a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2497ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2498ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2499a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2500ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2501910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2502910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2503ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2504ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2505ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2506ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2507ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2510ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
25113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
25123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
25133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
25143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2515d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2517a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2518ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2519ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2520ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2521ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2524ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2525ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2526ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2527ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2528ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2529ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2530ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2533ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2534ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2535d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2536ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2537ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2538d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2539ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2540a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2541a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2542ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2543ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2544a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
254513c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2546a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2547ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2548ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2550a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2551910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2552910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2553d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
25543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
255537fd27dbb941d27f4bd7412e534e7e5089d6781bRichard Smith                                                     0);
2556b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2557b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2558b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2559b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2560ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
25614469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2562a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2563ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2564ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
25651e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2566d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2567ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2568ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2569ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the declaration of a variable template partial
2570ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization.
2571ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2572ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param VarTemplate the (instantiated) variable template that is partially
2573ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialized by the instantiation of \p PartialSpec.
2574ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2575ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \param PartialSpec the (uninstantiated) variable template partial
2576ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// specialization that we are instantiating.
2577ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo///
2578ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \returns The instantiated partial specialization, if successful; otherwise,
2579ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// NULL to indicate an error.
2580ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplatePartialSpecializationDecl *
2581ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoTemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2582ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate,
2583ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplatePartialSpecializationDecl *PartialSpec) {
2584ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create a local instantiation scope for this variable template partial
2585ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization, which will contain the instantiations of the template
2586ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // parameters.
2587ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  LocalInstantiationScope Scope(SemaRef);
2588ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2589ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template parameters of the variable template partial
2590ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2591ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2592ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2593ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!InstParams)
2594ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2595ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2596ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute into the template arguments of the variable template partial
2597ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization.
2598c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  const ASTTemplateArgumentListInfo *TemplArgInfo
2599c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella    = PartialSpec->getTemplateArgsAsWritten();
2600c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2601c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                                            TemplArgInfo->RAngleLoc);
2602c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella  if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2603c1cef0892e049fcd31084f02d1efdd9985d4dfa4Enea Zaffanella                    TemplArgInfo->NumTemplateArgs,
2604ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                    InstTemplateArgs, TemplateArgs))
2605ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2606ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2607ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Check that the template argument list is well-formed for this
2608ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // class template.
2609ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  SmallVector<TemplateArgument, 4> Converted;
2610ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2611ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        InstTemplateArgs, false, Converted))
2612ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2613ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2614ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Figure out where to insert this variable template partial specialization
2615ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // in the member template's set of variable template partial specializations.
2616ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  void *InsertPos = 0;
2617ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *PrevDecl =
2618ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplate->findPartialSpecialization(Converted.data(), Converted.size(),
2619ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                             InsertPos);
2620ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2621ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the canonical type that describes the converted template
2622ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // arguments of the variable template partial specialization.
2623ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2624ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), Converted.data(), Converted.size());
2625ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2626ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Build the fully-sugared type for this variable template
2627ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specialization as the user wrote in the specialization
2628ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // itself. This means that we'll pretty-print the type retrieved
2629ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // from the specialization's declaration the way that the user
2630ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // actually wrote the specialization, rather than formatting the
2631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // name based on the "canonical" representation used to store the
2632ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // template arguments in the specialization.
2633ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2634ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2635ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CanonType);
2636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (PrevDecl) {
2638ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We've already seen a partial specialization with the same template
2639ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // parameters and template arguments. This can happen, for example, when
2640ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // substituting the outer template arguments ends up causing two
2641ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // variable template partial specializations of a member variable template
2642ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // to have identical forms, e.g.,
2643ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2644ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   template<typename T, typename U>
2645ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   struct Outer {
2646ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename X, typename Y> pair<X,Y> p;
2647ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<T, Y> p;
2648ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //     template<typename Y> pair<U, Y> p;
2649ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   };
2650ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //
2651ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //   Outer<int, int> outer; // error: the partial specializations of Inner
2652ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    //                          // have the same signature.
2653ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2654ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_var_partial_spec_redeclared)
2655ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << WrittenTy->getType();
2656ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PrevDecl->getLocation(),
2657ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::note_var_prev_partial_spec_here);
2658ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2659ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2660ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2661ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
2662ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI = SemaRef.SubstType(
2663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSourceInfo(), TemplateArgs,
2664ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
2666ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (DI->getType()->isFunctionType()) {
2669ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SemaRef.Diag(PartialSpec->getLocation(),
2670ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                 diag::err_variable_instantiates_to_function)
2671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        << PartialSpec->isStaticDataMember() << DI->getType();
2672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2673ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
2674ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2675ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Create the variable template partial specialization declaration.
2676ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplatePartialSpecializationDecl *InstPartialSpec =
2677ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      VarTemplatePartialSpecializationDecl::Create(
2678ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2679ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2680ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          DI, PartialSpec->getStorageClass(), Converted.data(),
268137fd27dbb941d27f4bd7412e534e7e5089d6781bRichard Smith          Converted.size(), InstTemplateArgs);
2682ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2683ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Substitute the nested name specifier, if any.
2684ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (SubstQualifier(PartialSpec, InstPartialSpec))
2685ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
2686ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2687ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2688ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstPartialSpec->setTypeAsWritten(WrittenTy);
2689ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
2690ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Add this partial specialization to the set of variable template partial
2691ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // specializations. The instantiation of the initializer is not necessary.
2692ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
269304592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
269404592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  // Set the initializer, to use as pattern for initialization.
269504592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  if (VarDecl *Def = PartialSpec->getDefinition(SemaRef.getASTContext()))
269604592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo    PartialSpec = cast<VarTemplatePartialSpecializationDecl>(Def);
269704592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs,
2698a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                     LateAttrs, Owner, StartingScope);
269904592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  InstPartialSpec->setInit(PartialSpec->getInit());
270004592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo
2701ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return InstPartialSpec;
2702ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
2703ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
270421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
270521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
27065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
270721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
270821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
270921ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2710cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2711cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2712cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2713cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2714cafeb948e6067b8dc897c441da522367917b06f9Richard Smith    ThisContext = cast<CXXRecordDecl>(Owner);
2715cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2716cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2717cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
27186cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
27196cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
27206cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2721cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2722cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
272321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
272421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
27255545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2726c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2727c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2728c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (NewTInfo != OldTInfo) {
2729c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // Get parameters from the new type info.
2730140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
273139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2732500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
273339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
273412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
273539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
2736500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2737500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2738dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2739500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2740500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2741500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2742500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2743500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2744a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
274512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
274639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
274712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2748500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2749500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2750500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2751500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2752500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
275339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
2754500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2755500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2756500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
275712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
27586920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2759c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    } else {
2760c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // The function type itself was not dependent and therefore no
2761c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // substitution occurred. However, we still need to instantiate
2762c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      // the function parameters themselves.
2763c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      const FunctionProtoType *OldProto =
2764c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          cast<FunctionProtoType>(OldProtoLoc.getType());
276539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
2766c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        ParmVarDecl *OldParam = OldProtoLoc.getArg(i);
2767c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        if (!OldParam) {
2768c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
2769c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner              D, D->getLocation(), OldProto->getArgType(i)));
2770c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner          continue;
2771c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner        }
2772c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
2773ded9979a2997336cee8797deb6bb3194fccc2068Eli Friedman        ParmVarDecl *Parm =
2774c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner            cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
27756920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
27766920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
27776920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
27786920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2779cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2780c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner  } else {
2781c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // If the type of this function, after ignoring parentheses, is not
2782c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // *directly* a function type, then we're instantiating a function that
2783c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // was declared via a typedef or with attributes, e.g.,
2784c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2785c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   typedef int functype(int, int);
2786c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   functype func;
2787c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //   int __cdecl meth(int, int);
2788c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    //
2789c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // In this case, we'll just go instantiate the ParmVarDecls that we
2790c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    // synthesized in the method declaration.
2791c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    SmallVector<QualType, 4> ParamTypes;
2792c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
2793c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               D->getNumParams(), TemplateArgs, ParamTypes,
2794c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner                               &Params))
2795c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner      return 0;
2796cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
2797c66e7e99d5acc560de5cea50909fcea22ef12ca5Reid Kleckner
279821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
27995545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
28005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2801e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2802e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2803e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2804e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2805e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2806e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2807e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2808e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2809e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2810e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2811e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2812e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2813e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2814e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2815e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2816e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2817e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2818e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2819e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2820e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2821e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2822e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2823dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
2824e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2825500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2826500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2827500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2828e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2829e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2830e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2831e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2832e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2833e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2834e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2835e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2836e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2837e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2838e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
283913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
284013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2841e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2842e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2843e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2844e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2845e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2846e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2847e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2848e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2849e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2850e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2851e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2852e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2853e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
285480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
2855e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2856e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2857e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2858e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2859e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2860e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2861e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2862e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2863e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2864e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2865e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2866e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2867e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2868e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2869e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2870e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2871e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2872cafeb948e6067b8dc897c441da522367917b06f9Richard Smith      Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions();
2873e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2874e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2875e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2876e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2877e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2878e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2879e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2880e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2881e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2882e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2883e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2884e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2885e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2886e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2887e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2888e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2889e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2890e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2891e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2892e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2893e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2894e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2895e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2896e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2897e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2898e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2899e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2900e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2901e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2902e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2903e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2904e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2905e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2906e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2907e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2908e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2909e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2910e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2911e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2912e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2913e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2914e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2915e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2916e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2917e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2918e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2919e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2920e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2921e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2922e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2923e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2924e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2925e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2926e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2927e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2928e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2929e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2930e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2931e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2932e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2933e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2934e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2935e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2936e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2937e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2938e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2939e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2940e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2941ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2942ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2943ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2944ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2945e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2946e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2947e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2948e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2949e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2950e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2951e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2952e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2953e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2954e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2955e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2956e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2957e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2958e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2959e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
29600567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                               NewProto->getArgTypes(), EPI));
2961e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2962e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2963e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2964e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
296513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
296613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2967e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2968e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2969e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2970e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2971b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (Inst) {
2972b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
2973b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
2974b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2975b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
2976b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Decl->setType(Context.getFunctionType(Proto->getResultType(),
29770567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner                                          Proto->getArgTypes(), EPI));
2978e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2979b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
2980e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2981e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
2982e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
2983e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
2984e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
2985e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2986e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
2987e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2988e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
298913bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
299013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
2991e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
299213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
299313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
299413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
2995e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2996e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
29971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2998e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2999e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
3000e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
30011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
30021eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
3003e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
300485f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
300510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
30061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3007cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
3008cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
3009cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
30101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
30111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
3012cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
3013cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
3014cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
3015cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
3016cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
3017cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
3018cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
30191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
30204a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
30211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
3022cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
3023bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
3024cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
30254a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
3026cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
3027cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
30281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30290ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
30300ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
30310ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
303260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
3033e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
3034e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
3035e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
3036e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
303780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
3038e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
3039e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
3040e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
304113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
304213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
304313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
30444841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      ExceptionSpecificationType NewEST = EST_Uninstantiated;
30454841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      if (EPI.ExceptionSpecType == EST_Unevaluated)
30464841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith        NewEST = EST_Unevaluated;
304713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
3048e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
3049e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
3050e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
3051e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
3052e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
30534841ca5f83bf970f910ac7d154cdd71d2a3cf481Richard Smith      EPI.ExceptionSpecType = NewEST;
3054e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
305513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
30560567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner      New->setType(SemaRef.Context.getFunctionType(
30570567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner          NewProto->getResultType(), NewProto->getArgTypes(), EPI));
3058e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
3059e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
3060e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
30610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
30620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
306319f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
3064e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
306519f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
306619f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
306723323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
306823323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
30697cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
3070e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
3071e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3072e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
30735545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
30745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
30755545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
30765545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
30775545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
30781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
30791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
30805545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
3081e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
3082e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
30831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30845545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
3085e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
308685606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
30875545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
30885545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
30895545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
30905545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
3091a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3092a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
3093a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3094a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
3095b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
3096b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
3097b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
3098b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3099a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
3100b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
3101b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
3102b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
3103b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
3104b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
3105e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3106e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3107e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
3108e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
3109f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
3110b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
3111e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
3112e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
311310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
311454dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
311554dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
3116af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
3117af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
3118af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3119af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
3120251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
31216cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
31221eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
31233b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
3124f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
3125f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
3126f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3127f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
3128f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
3129f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
3130f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
3131dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
3132f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
31331eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
31348387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
3135f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
31368a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
31378387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
31388387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
31398387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
31408387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31418387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3142360d23ef628bf891514e77c519d1d77305ca1743David Majnemer  // Call the LateTemplateParser callback if there is a need to late parse
3143a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
3144f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
31458387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
3146ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    // FIXME: Optimize to allow individual templates to be deserialized.
3147ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    if (PatternDecl->isFromASTFile())
3148ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith      ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3149ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith
3150ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3151ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    assert(LPT && "missing LateParsedTemplate");
3152ac32d9044b9c1e7492cef929a322d23ce899d276Richard Smith    LateTemplateParser(OpaqueParser, *LPT);
31538387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
31548387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
31558387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
3156f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
3157e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3158e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
3159a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3160e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
3161e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
3162e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
3163a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
3164e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
3165e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
3166a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3167e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
3168a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
3169e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
3170cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
317158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
317258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
317362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
317458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
3175e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
317658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
31771eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
3178e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
31791eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
318060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // C++1y [temp.explicit]p10:
318160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   Except for inline functions, declarations with types deduced from their
318260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   initializer or return value, and class template specializations, other
318360e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   explicit instantiation declarations have the effect of suppressing the
318460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  //   implicit instantiation of the entity to which they refer.
31851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
3186d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
318760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      !PatternDecl->isInlined() &&
318837e849ad80731ac1b2ad1c64e73bced27802bd8bRichard Smith      !PatternDecl->getResultType()->getContainedAutoType())
3189d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
31901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3191d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (PatternDecl->isInlined())
3192d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
3193d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
3194f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3195f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
3196a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
3197a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3198e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
3199e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3200e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
3201b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
3202b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
3203b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
32045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
320562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
320686648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  std::deque<PendingImplicitInstantiation>
320786648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                              SavedPendingLocalImplicitInstantiations;
320886648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  SavedPendingLocalImplicitInstantiations.swap(
320986648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                                  PendingLocalImplicitInstantiations);
32102a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
32112a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
321262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
32132a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
32141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3215a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
3216f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
3217e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
321854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
321960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
322060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
322160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
322260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
322360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
322460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
322560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
322660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
32271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32281d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
32291d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
32301d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
32311d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    ActOnStartOfFunctionDef(0, Function);
32327c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32331d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
32341d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
32351d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
32367c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
32371d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
32381d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith      getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
323954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
32401d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
32411d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
32421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3243cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
3244cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
3245cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3246cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3247cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
3248cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
3249cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3250cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
3251cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3252cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
3253cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
3254cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
3255a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3256cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
3257cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
3258b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
32591d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
32600c01d18094100db92d38daa923c95661512db203John McCall
32611d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
32621d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
3263aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
3264aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
3265aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
32661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
326760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
326860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
326962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
327060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
327160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
3272b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
32732a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
32742a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
32752a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3276b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
32771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
327862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
32791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32802a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
32818155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
32828155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
32832a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
32842a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
3285b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
32868155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
32878155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
328862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
3289b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
329086648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali  SavedPendingLocalImplicitInstantiations.swap(
329186648b13eb01c0565de7264b27799ba81c7ad060Faisal Vali                            PendingLocalImplicitInstantiations);
3292a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3293a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
3294ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3295ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3296ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentList &TemplateArgList,
3297ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &TemplateArgsInfo,
3298ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SmallVectorImpl<TemplateArgument> &Converted,
3299ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    SourceLocation PointOfInstantiation, void *InsertPos,
3300ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LateInstantiatedAttrVec *LateAttrs,
3301ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LocalInstantiationScope *StartingScope) {
3302ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (FromVar->isInvalidDecl())
3303ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3304ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3305ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3306ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Inst)
3307ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3308ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3309ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  MultiLevelTemplateArgumentList TemplateArgLists;
3310ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3311ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3312ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TemplateDeclInstantiator Instantiator(
3313ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      *this, VarTemplate->getDeclContext(),
3314ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      MultiLevelTemplateArgumentList(TemplateArgList));
3315ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3316ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // TODO: Set LateAttrs and StartingScope ...
3317ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3318ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return cast_or_null<VarTemplateSpecializationDecl>(
3319ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Instantiator.VisitVarTemplateSpecializationDecl(
3320ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3321ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3322ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3323ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiates a variable template specialization by completing it
3324ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// with appropriate type information and initializer.
3325ef4579cda09b73e3d4d98af48201da25adc29326Larisse VoufoVarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3326ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3327ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3328ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3329ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Do substitution on the type of the declaration
333004592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  MultiLevelTemplateArgumentList Innermost;
333104592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo  Innermost.addOuterTemplateArguments(TemplateArgs.getInnermost());
3332ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  TypeSourceInfo *DI =
333304592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo      SubstType(PatternDecl->getTypeSourceInfo(), Innermost,
3334ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3335ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!DI)
3336ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return 0;
3337ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3338ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Update the type of this variable template specialization.
3339ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarSpec->setType(DI->getType());
3340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3341ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Instantiate the initializer.
3342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  return VarSpec;
3345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3347ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// BuildVariableInstantiation - Used after a new variable has been created.
3348ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// Sets basic variable data and decides whether to postpone the
3349ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// variable instantiation.
3350ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::BuildVariableInstantiation(
3351ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *NewVar, VarDecl *OldVar,
3352ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs,
3353a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner,
3354a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    LocalInstantiationScope *StartingScope,
3355567f917df048d42732997a479b2b257403fc88efLarisse Voufo    bool InstantiatingVarTemplate) {
3356ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3357a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // If we are instantiating a local extern declaration, the
3358a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  // instantiation belongs lexically to the containing function.
3359ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If we are instantiating a static data member defined
3360ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line, the instantiation will have the same lexical
3361ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // context (which will be a namespace scope) as the template.
3362a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (OldVar->isLocalExternDecl()) {
3363a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLocalExternDecl();
3364a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->setLexicalDeclContext(Owner);
3365a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  } else if (OldVar->isOutOfLine())
3366ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3367ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setTSCSpec(OldVar->getTSCSpec());
3368ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setInitStyle(OldVar->getInitStyle());
3369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setConstexpr(OldVar->isConstexpr());
3371dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  NewVar->setPreviousDeclInSameBlockScope(
3372dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      OldVar->isPreviousDeclInSameBlockScope());
3373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  NewVar->setAccess(OldVar->getAccess());
3374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!OldVar->isStaticDataMember()) {
337686164e8f51fa89a3ec904607c3848dc4a21b12cfEli Friedman    NewVar->setIsUsed(OldVar->isUsed(false));
3377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setReferenced(OldVar->isReferenced());
3378ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3379ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3380aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // See if the old variable had a type-specifier that defined an anonymous tag.
3381aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // If it did, mark the new variable as being the declarator for the new
3382aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  // anonymous tag.
3383aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) {
3384aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    TagDecl *OldTag = OldTagType->getDecl();
3385aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    if (OldTag->getDeclaratorForAnonDecl() == OldVar) {
3386aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl();
3387aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      assert(!NewTag->hasNameForLinkage() &&
3388aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer             !NewTag->hasDeclaratorForAnonDecl());
3389aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer      NewTag->setDeclaratorForAnonDecl(NewVar);
3390aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer    }
3391aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer  }
3392aa82461f696ced36b0cd817dfa492b6c93d15447David Majnemer
3393ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3394ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3395ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (NewVar->hasAttrs())
3396ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    CheckAlignasUnderalignment(NewVar);
3397ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3398a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  LookupResult Previous(
3399a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      *this, NewVar->getDeclName(), NewVar->getLocation(),
3400a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
3401a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith                                  : Sema::LookupOrdinaryName,
3402a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith      Sema::ForRedeclaration);
3403ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3404a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl()) {
3405dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // We have a previous declaration. Use that one, so we merge with the
3406dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    // right type.
3407dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith    if (NamedDecl *NewPrev = FindInstantiatedDecl(
3408dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith            NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
3409dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith      Previous.addDecl(NewPrev);
3410dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith  } else if (!isa<VarTemplateSpecializationDecl>(NewVar) &&
3411dd9459f8869f66409f7ea429053b453e33f6499cRichard Smith             OldVar->hasLinkage())
3412ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
3413567f917df048d42732997a479b2b257403fc88efLarisse Voufo  CheckVariableDeclaration(NewVar, Previous);
3414ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3415a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!InstantiatingVarTemplate) {
3416a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
3417a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith    if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
3418ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
3419a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  }
3420a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith
3421a41c97a5d1912ffd184381d269fd8e5a25ee5e59Richard Smith  if (!OldVar->isOutOfLine()) {
3422ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (NewVar->getDeclContext()->isFunctionOrMethod())
3423ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3424ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3425ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3426ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Link instantiations of static data members back to the template from
3427ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // which they were instantiated.
3428567f917df048d42732997a479b2b257403fc88efLarisse Voufo  if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate)
3429ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    NewVar->setInstantiationOfStaticDataMember(OldVar,
3430ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                               TSK_ImplicitInstantiation);
3431ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3432ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (isa<VarTemplateSpecializationDecl>(NewVar)) {
3433ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Do not instantiate the variable just yet.
3434567f917df048d42732997a479b2b257403fc88efLarisse Voufo  } else if (InstantiatingVarTemplate) {
343504592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo    assert(!NewVar->getInit() &&
343604592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo           "A variable should not have an initializer if it is templated"
343704592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo           " and we are instantiating its template");
343804592e7c1260a6a671a24d91dab16f5d5a024fe0Larisse Voufo    NewVar->setInit(OldVar->getInit());
3439ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else
3440ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3441ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3442ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Diagnose unused local variables with dependent types, where the diagnostic
3443ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // will have been deferred.
3444ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!NewVar->isInvalidDecl() &&
3445ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() &&
3446ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      OldVar->getType()->isDependentType())
3447ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    DiagnoseUnusedDecl(NewVar);
3448ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3449ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3450ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo/// \brief Instantiate the initializer of a variable.
3451ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableInitializer(
3452ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    VarDecl *Var, VarDecl *OldVar,
3453ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const MultiLevelTemplateArgumentList &TemplateArgs) {
3454ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3455ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->getAnyInitializer())
3456ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // We already have an initializer in the class.
3457ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return;
3458ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3459ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (OldVar->getInit()) {
3460ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3461ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3462ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    else
3463ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3464ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3465ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate the initializer.
3466ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ExprResult Init =
3467ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        SubstInitializer(OldVar->getInit(), TemplateArgs,
3468ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                         OldVar->getInitStyle() == VarDecl::CallInit);
3469ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!Init.isInvalid()) {
3470ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      bool TypeMayContainAuto = true;
3471ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (Init.get()) {
3472ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        bool DirectInit = OldVar->isDirectInit();
3473ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        AddInitializerToDecl(Var, Init.take(), DirectInit, TypeMayContainAuto);
3474ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else
3475ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        ActOnUninitializedDecl(Var, TypeMayContainAuto);
3476ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    } else {
3477ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // FIXME: Not too happy about invalidating the declaration
3478ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // because of a bogus initializer.
3479ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setInvalidDecl();
3480ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
3481ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3482ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PopExpressionEvaluationContext();
3483ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3484ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             !Var->isCXXForRangeDecl())
3485ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    ActOnUninitializedDecl(Var, false);
3486ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3487ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3488a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
3489a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
3490a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
34917caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
34927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
34937caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
34947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
34957caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
34967caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
34977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
34987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
34997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
3500e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
3501e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
3502e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
3503e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
35047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
35057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
35067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
3507e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
3508e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
3509ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3510ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                DefinitionRequired);
3511ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo}
3512ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3513ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufovoid Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3514ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         VarDecl *Var, bool Recursive,
3515ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         bool DefinitionRequired) {
3516ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
35177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
35187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
35191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3520ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarTemplateSpecializationDecl *VarSpec =
3521ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      dyn_cast<VarTemplateSpecializationDecl>(Var);
3522ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  assert((VarSpec || Var->isStaticDataMember()) &&
3523ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo         "Not a static data member, nor a variable template specialization?");
3524ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  VarDecl *PatternDecl = 0;
3525ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3526ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is a variable template specialization, make sure that it is
3527ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // non-dependent, then find its instantiation pattern.
3528ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarSpec) {
3529ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
3530ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3531ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo               VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3532ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Only instantiate variable template specializations that are "
3533ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "not type-dependent");
35343151b7c6dd49947b0a91b3e22c31f4864629e355Larisse Voufo    (void)InstantiationDependent;
3535ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3536ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Find the variable initialization that we'll be substituting.
3537ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(VarSpec->getSpecializedTemplate() &&
3538ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Specialization without specialized template?");
3539ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    llvm::PointerUnion<VarTemplateDecl *,
3540ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                       VarTemplatePartialSpecializationDecl *> PatternPtr =
3541ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getSpecializedTemplateOrPartial();
3542439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) {
3543ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      PatternDecl = cast<VarDecl>(
3544ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          PatternPtr.get<VarTemplatePartialSpecializationDecl *>());
3545439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3546439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      // Find actual definition
3547439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      if (VarDecl *Def = PatternDecl->getDefinition(getASTContext()))
3548439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo        PatternDecl = Def;
3549439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    } else {
3550439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      VarTemplateDecl *PatternTemplate = PatternPtr.get<VarTemplateDecl *>();
3551439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3552439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      // Find actual definition
3553439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      if (VarTemplateDecl *Def = PatternTemplate->getDefinition())
3554439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo        PatternTemplate = Def;
3555439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo
3556439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo      PatternDecl = PatternTemplate->getTemplatedDecl();
3557439d665f4d1066ee5ebd8dd0938d85be83d490c4Larisse Voufo    }
3558ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(PatternDecl && "instantiating a non-template");
3559ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3560ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3561ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If this is a static data member, find its out-of-line definition.
35627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
3563ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (Var->isStaticDataMember()) {
3564ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(Def && "This data member was not instantiated from a template?");
3565ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert(Def->isStaticDataMember() && "Not a static data member?");
3566ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Def = Def->getOutOfLineDefinition();
3567ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
3568ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3569ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // If the instantiation pattern does not have an initializer, or if an
3570ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // out-of-line definition is not found, we won't perform any instantiation.
3571ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // Rather, we rely on the user to instantiate this definition (or provide
3572ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // a specialization for it) in another translation unit.
3573ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if ((VarSpec && !PatternDecl->getInit()) ||
3574ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      (!VarSpec && Var->isStaticDataMember() && !Def)) {
3575e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
3576ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (!Var->isStaticDataMember()) {
3577ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3578ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_var_template)
3579ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            << PatternDecl;
3580ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PatternDecl->getLocation(),
3581ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::note_explicit_instantiation_here);
3582ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      } else {
3583ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Def = Var->getInstantiatedFromStaticDataMember();
3584ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(PointOfInstantiation,
3585ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo             diag::err_explicit_instantiation_undefined_member)
3586ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            << 3 << Var->getDeclName() << Var->getDeclContext();
3587ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
3588ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
3589ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (VarSpec)
3590ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Var->setInvalidDecl();
359158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
359258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
359362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
359458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
359558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
359658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
35977caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
35987caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
35997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
3600234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3601234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
3602251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
3603234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
3604251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
3605a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3606251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
3607251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
3608251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
3609251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
3610ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //
3611ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // C++11 [temp.explicit]p10:
3612ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   Except for inline functions, [...] explicit instantiation declarations
3613ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   have the effect of suppressing the implicit instantiation of the entity
3614ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  //   to which they refer.
3615234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
3616251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
36171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3618afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
3619afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
3620afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
3621afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
3622afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3623afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3624afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
3625afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
3626afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
3627ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      if (Var->isStaticDataMember())
3628ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3629ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      else {
3630ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        DeclGroupRef DG(Var);
3631ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        Consumer.HandleTopLevelDecl(DG);
3632ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      }
3633afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
3634afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
3635025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
3636ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec) {
3637ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // If we already have a definition, we're done.
3638ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (VarDecl *Def = Var->getDefinition()) {
3639ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // We may be explicitly instantiating something we've already implicitly
3640ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      // instantiated.
3641ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3642ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                         PointOfInstantiation);
3643ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      return;
3644ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    }
364595e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
3646f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
36477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
36487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
36497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
36501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
36527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
36537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
36545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
365562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
36568155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
36578155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
365862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
36598155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
36601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
36627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3663ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  ContextRAII PreviousContext(*this, Var->getDeclContext());
36647bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
3665ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
36661028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3667ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (!VarSpec)
3668ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3669ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                          getTemplateInstantiationArgs(Var)));
3670ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  else
3671ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Construct a VarTemplateSpecializationDecl to avoid name clashing with
3672ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // the primary template. (Note that unlike function declarations, variable
3673ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // declarations cannot be overloaded.)
3674ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // In fact, there is no need to construct a new declaration from scratch.
3675ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Thus, simply complete its definition with an appropriately substituted
3676ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // type and initializer.
3677ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    Var = CompleteVarTemplateSpecializationDecl(
3678ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec, PatternDecl, getTemplateInstantiationArgs(Var));
3679f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3680ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PreviousContext.pop();
36817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
36827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3683583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
3684ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (!VarSpec)
3685ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      assert(MSInfo && "Missing member specialization information?");
3686ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3687ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PassToConsumerRAII.Var = Var;
3688ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (MSInfo)
3689ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      Var->setTemplateSpecializationKind(
3690ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          MSInfo->getTemplateSpecializationKind(),
3691ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          MSInfo->getPointOfInstantiation());
36927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
3693ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
3694ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // This variable may have local implicit instantiations that need to be
3695ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // instantiated within this scope.
3696ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  PerformPendingInstantiations(/*LocalOnly=*/true);
3697ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
36987bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
36997bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
37007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
37018155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
37028155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
37038155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
37047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
37051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
370662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
37071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
37098155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
3710ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "VTableUses should be empty before it is discarded.");
37118155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
37128155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
37137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
37148155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
3715ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "PendingInstantiations should be empty before it is discarded.");
371662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
37171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3718a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3719815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3720090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
3721090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
3722090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
3723090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
37241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372590ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
372654b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
3727a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3728090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
3729090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
373072f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
373172f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
3732cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
3733090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
3734030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
3735030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
3736030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
3737030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
3738030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
37393fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
3740a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37413fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
37423fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
374376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
374498a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      SmallVector<UnexpandedParameterPack, 4> Unexpanded;
37453fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
374698a75581e155a7dac853a69b0151960f8e2aacbdNick Lewycky      collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
37473fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
3748d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
3749dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
3750a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
37513fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
3752a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
3753a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
3754d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
37553fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
37563fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
37573fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
37583fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
37593fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
37603fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
3761a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3762a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
3763cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
37643fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
37653fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37663fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
37675b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
37685b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
37695b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
37703fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37713fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37723fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
37733fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37743fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
377576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
3776a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
3777a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
37783fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
37793fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
37803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37823fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
37833fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
37843fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
37856df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
37865b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
37873fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
37883fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
37893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
37903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
37913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
37923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
3793a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37943fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
37953fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
3796a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
37973fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
37983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
37993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
38006b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
38015b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
38025b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
38035b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
38046b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
38056b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
3806090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3807a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3808090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
380976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
381076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
381176852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
381276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
381376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
381476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
38159db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
3816802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
3817802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
3818802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
38196df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
382076852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
38215b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
382276852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
382376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
38245b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
382576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
3826090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
3827b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
382800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
382900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
383000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
3831b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
3832b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3833b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
3834b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
3835b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
38361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38375b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
38386df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
383900eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
384000eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
3841b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
384200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
384300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
384400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
3845b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
3846b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3847b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
38486df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
3849b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
38506df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
38515b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
38526df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
3853090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3854090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
38559db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
38569db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
3857090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
38589db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
385990ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
3860090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3861090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
38621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3863090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
3864d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
3865090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
3866090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
386793c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
38689db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
3869090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
3870090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
387152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
387252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
387352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
387452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
387552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
387652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
387752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
387852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
387952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
388052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
388152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
388252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
388352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
388452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
388552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
38860d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
38870d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
38880d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3889a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38900d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
38910d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
38920d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
38930d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
38940d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3895a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
38960d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
38970d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
38980d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3899a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3900ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3901ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3902a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3904ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3905ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3906ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3907ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3908ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3909ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3910ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
3911a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3912ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
3913ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
3914ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
391552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
391652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
391752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
391852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
391952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
392052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
392152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
392252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
392352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
392452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
392552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
392652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
392752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
392852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
392952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
393052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
393152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
393252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
393352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
393452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
393552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
393652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
393752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
393852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
393952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
394052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
394152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
394252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
394352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
394452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
394552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
394652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
394752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
394852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
394952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
395052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
395152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
395252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
395352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3954ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3955ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3956ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3957ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3958ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3959ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3960ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3961ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3962ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3963ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3964ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3965ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
39667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
39677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
39687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3969ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
39707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
39717ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
39727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
39730d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
39740d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3975ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
39760d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
39770d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
397852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
397952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
398052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
398152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
398252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
398352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
398452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
398552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
398652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
398752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
398852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
398952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
399052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
399152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
399252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3993ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3994ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3995815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
39960d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
39977ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
39987ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
39997ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
40007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
40017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
40027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
40037ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
40047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
40057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
40060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
40070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
40080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
40090d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
4010815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
40110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
40120d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
40131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
401552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
40161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
401852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
4019815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
402052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
402152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
4022815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
40237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
402452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
402552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
402652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
402752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
402852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
4029a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
40300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
40310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
40320d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
4033ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
4034ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
4035ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
4036ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
4037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
4038d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
4039d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
4040d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
40411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
4042d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
4043d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
4044d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
40451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4046ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
4047ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
4048ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4049ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
4050ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
4051ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
4052815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
4053815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
4054815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4055815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4056815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
40571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
4058815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
4059815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
4060815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
4061815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
4062815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
4063815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
4064815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4065815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
4066815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4067815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
406802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
406902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
407002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
407102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
40727c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
4073e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
407402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
40757c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
407602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
407702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
407802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
407902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
4080ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
4081ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
4082815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4083815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
4084815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
4085815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
4086815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
4087815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4088815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
4089815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
4090815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
4091815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
4092815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
4093815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
4094815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4095815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
4096815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
4097815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4098815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
4099815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
4100815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
4101041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4102041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// \p EnumConstantDecl for \p KnownValue (which refers to
4103041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4104041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4105041d10caff48859c0de6d001559a73185f5e0601Serge Pavlov/// this mapping from within the instantiation of <tt>X<int></tt>.
41067c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
4107e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
4108815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
4109550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
41106d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
41117bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
41127bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
41132bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
41142bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
4115d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4116d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
4117d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
4118a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
411957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
412057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
412157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
4122a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
41239a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
41249a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
412557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
412657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
412757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
4128dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // If we're performing a partial substitution during template argument
4129dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // deduction, we may not have values for template parameters yet. They
4130dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    // just map to themselves.
4131dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov    if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4132dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov        isa<TemplateTemplateParmDecl>(D))
4133dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov      return D;
4134dc49d523db70a1c9005b7c09de80b22ccb1ed6a4Serge Pavlov
413529a46e63490176608efe13f13b293a6ce9862059Serge Pavlov    if (D->isInvalidDecl())
413629a46e63490176608efe13f13b293a6ce9862059Serge Pavlov      return 0;
413729a46e63490176608efe13f13b293a6ce9862059Serge Pavlov
413857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
413957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
414057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
4141a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
414257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
414357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
4144a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
414557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
414657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
41472bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
4148815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4149ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // For variable template specializations, update those that are still
4150ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  // type-dependent.
4151ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  if (VarTemplateSpecializationDecl *VarSpec =
4152ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          dyn_cast<VarTemplateSpecializationDecl>(D)) {
4153ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    bool InstantiationDependent = false;
4154ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    const TemplateArgumentListInfo &VarTemplateArgs =
4155ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo        VarSpec->getTemplateArgsInfo();
4156ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    if (TemplateSpecializationType::anyDependentTemplateArguments(
4157ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            VarTemplateArgs, InstantiationDependent))
4158ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo      D = cast<NamedDecl>(
4159ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo          SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4160ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    return D;
4161ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo  }
4162ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4163e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4164e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
4165e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
4166a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
41672c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
41682c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
4169e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
41702c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
41712c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
41722c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
41732c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
41742c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
4175ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41762c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
41772c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
41782c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
41792c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
41802c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
41812c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
41822c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
41832c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
4184ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41852c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
41862c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
41872c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
41882c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
41892c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
41902c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
41912c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
41922c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
41932c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
4194ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
41952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
41962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
41972c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
4198e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
4199ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
42002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
42012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
42022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
42032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
42042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
42052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
420652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
4207ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
42082c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
420952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
42108b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
4211e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
4212e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
4213e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
421452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
4215e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
4216e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
4217a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
42187c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
42191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
422044c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
42211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4222815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
4223815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
4224815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
4225815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
42267c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
42273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
42283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
4229eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
42303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
42317c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
42327c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
42333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
42343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
4235eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
4236eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
42373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
42383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
42393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
4240a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
4241a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
42427c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
42437c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
42447c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
4245815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
4246815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
424717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
42483bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
4249815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
4250815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
4251815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
4252815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
4253815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4254815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
4255815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
4256815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
4257815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
42581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
425917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
426017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
4261815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
42621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4263eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
4264eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
4265eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
4266eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
4267eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
4268eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
4269eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
4270eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
4271a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
4272eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
4273eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
4274eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
4275eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
4276eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
4277eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
4278eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4279eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
42800724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
42810724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
42820724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
42830724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
42840724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
42850724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
42860724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
42870724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
42880724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
42890724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
42900724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
42910724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
42920724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
42930724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
4294eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
4295eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
4296eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
4297eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
4298eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
4299a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4300815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
4301815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
4302815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
4303815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
4304815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
4305d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
43061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
4307d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
43088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
43096e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
43106e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
4311b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    SmallVector<PendingImplicitInstantiation, 4> Pending;
43126e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
43136e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
43146e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
43156e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
4316a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
431760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
431862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
431960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
432060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
432160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
432262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
432362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
432460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
432560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
432660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
432760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
43281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
43307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
4331f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4332f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
433358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
433458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
433558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
433658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
43377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
43387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
43391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4340ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate variable definitions
43417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
4342ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4343ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    assert((Var->isStaticDataMember() ||
4344ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo            isa<VarTemplateSpecializationDecl>(Var)) &&
4345ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           "Not a static data member, nor a variable template"
4346ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo           " specialization?");
4347c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
4348291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
4349291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
4350ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
4351291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
4352291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4353291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
4354291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
4355ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
4356291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
4357b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
4358291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
4359291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
436058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
436158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
436258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
436358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
4364ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
4365291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
4366291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
4367291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
4368291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
4369ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4370ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                        "instantiating variable definition");
437158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
437258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
4373ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo
4374ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // Instantiate static data member definitions or variable template
4375ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    // specializations.
4376ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo    InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4377ef4579cda09b73e3d4d98af48201da25adc29326Larisse Voufo                                  DefinitionRequired);
4378d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
4379d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
43800c01d18094100db92d38daa923c95661512db203John McCall
43810c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
43820c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
43830c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
43840c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
43850c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
43860c01d18094100db92d38daa923c95661512db203John McCall
43870c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
43880c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
43890c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
43900c01d18094100db92d38daa923c95661512db203John McCall      break;
43910c01d18094100db92d38daa923c95661512db203John McCall    }
43920c01d18094100db92d38daa923c95661512db203John McCall  }
43930c01d18094100db92d38daa923c95661512db203John McCall}
4394