SemaTemplateInstantiateDecl.cpp revision 83972f128e9218c051692bf96361327a701aeb79
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 {
1455bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer      Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
1465bbc385ad2d8e487edfbc2756eaf4fb0b920cfe4Benjamin Kramer                                                         *this, TemplateArgs);
14731c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola      if (NewAttr)
14831c195ac0f3869e742d42f9d02b6cd33442fb630Rafael Espindola        New->addAttr(NewAttr);
14923323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins    }
150d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson  }
151d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson}
152d8fe2d56fb5463c9d109e8c6dab2e98b06bee186Anders Carlsson
1534f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
1544f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
155b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Translation units cannot be instantiated");
1564f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1574f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1584f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorDecl *
15957ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerTemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
16057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
16157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner                                      D->getIdentifier());
16257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  Owner->addDecl(Inst);
16357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner  return Inst;
16457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner}
16557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
16657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerDecl *
1674f722be4587a7a0dece399fb5405dda158971ae1Douglas GregorTemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
168b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Namespaces cannot be instantiated");
1694f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor}
1704f722be4587a7a0dece399fb5405dda158971ae1Douglas Gregor
1713dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallDecl *
1723dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCallTemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1733dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  NamespaceAliasDecl *Inst
1743dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall    = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
1753dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespaceLoc(),
1763dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getAliasLoc(),
1770cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getIdentifier(),
1780cfaf6a270ecd0f5c7e541a8047c87948317548bDouglas Gregor                                 D->getQualifierLoc(),
1793dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getTargetNameLoc(),
1803dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall                                 D->getNamespace());
1813dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  Owner->addDecl(Inst);
1823dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall  return Inst;
1833dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall}
1843dbd3d5c04cd5abd7dfd83b15f51d7c610a3c512John McCall
1853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
1863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                           bool IsTypeAlias) {
1878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
188a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
189561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
190836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType()) {
191ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
192ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall                           D->getLocation(), D->getDeclName());
193ba6a9bd384df475780be636ca45bcef5c5bbd19fJohn McCall    if (!DI) {
1948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
195a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
1968dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
197b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
198b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1998dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // HACK: g++ has a bug where it gets the value kind of ?: wrong.
202b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // libstdc++ relies upon this bug in its implementation of common_type.
203b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // If we happen to be processing that implementation, fake up the g++ ?:
204b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  // semantics. See LWG issue 2141 for more information on the bug.
205b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
206b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
207b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith  if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
208b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DT->isReferenceType() &&
209b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
210b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
211b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      D->getIdentifier() && D->getIdentifier()->isStr("type") &&
212b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
213b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    // Fold it to the (non-reference) type which g++ would have produced.
214b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith    DI = SemaRef.Context.getTrivialTypeSourceInfo(
215b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith      DI->getType().getNonReferenceType());
216b5b37d194dddb960f43f763b3f9c3e17e7be3c2dRichard Smith
2178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  // Create the new typedef
218162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  TypedefNameDecl *Typedef;
219162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (IsTypeAlias)
220162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
221162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                    D->getLocation(), D->getIdentifier(), DI);
222162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  else
223162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
224162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                                  D->getLocation(), D->getIdentifier(), DI);
2258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
2268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    Typedef->setInvalidDecl();
2278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
228cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // If the old typedef was the name for linkage purposes of an anonymous
229cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  // tag decl, re-establish that relationship for the new typedef.
230cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall  if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
231cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    TagDecl *oldTag = oldTagType->getDecl();
232c61361b102fcb9be7b64cc493fb797ea551eb8e7Douglas Gregor    if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
233cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall      TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
23483972f128e9218c051692bf96361327a701aeb79John McCall      assert(!newTag->hasNameForLinkage());
235162e1c1b487352434552147967c3dd296ebee2f7Richard Smith      newTag->setTypedefNameForAnonDecl(Typedef);
236cde5a400dbc9655eddf0f383585d3cf67c11c539John McCall    }
237d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor  }
238a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
239ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (TypedefNameDecl *Prev = D->getPreviousDecl()) {
2407c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
2417c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                                       TemplateArgs);
242b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstPrev)
243b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
244a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2455df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
2465df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2475df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    // If the typedef types are not identical, reject them.
2485df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
2495df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola
2505df37bd0242e838e465f0bd51a70af424d152053Rafael Espindola    Typedef->setPreviousDeclaration(InstPrevTypedef);
2515126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall  }
2525126fd0dd92c4ec211c837ee78d5ce59c68dcbd5John McCall
2531d8d1ccd36888f1120b3a1df9e76f35dc2edb81dJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
254d57a38ee02c285d69d05fed6df0d7406b2517888Douglas Gregor
25546460a68f6508775e98c19b4bb8454bb471aac24John McCall  Typedef->setAccess(D->getAccess());
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2578dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Typedef;
2588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
2598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
260162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
2613e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
2623e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
264162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
265162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
266162e1c1b487352434552147967c3dd296ebee2f7Richard SmithDecl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
2673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
2683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Typedef);
2693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Typedef;
2703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith}
2713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithDecl *
2733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard SmithTemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // Create a local instantiation scope for this type alias template, which
2753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  // will contain the instantiations of the template parameters.
2763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  LocalInstantiationScope Scope(SemaRef);
2773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *TempParams = D->getTemplateParameters();
2793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!InstParams)
2813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *Pattern = D->getTemplatedDecl();
2843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *PrevAliasTemplate = 0;
286ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (Pattern->getPreviousDecl()) {
2873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
2883bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
2893bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
2903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    }
2913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
2943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
2953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!AliasInst)
2963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    return 0;
2973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  TypeAliasTemplateDecl *Inst
2993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3003e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                    D->getDeclName(), InstParams, AliasInst);
3013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (PrevAliasTemplate)
3023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setPreviousDeclaration(PrevAliasTemplate);
3033e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Inst->setAccess(D->getAccess());
3053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!PrevAliasTemplate)
3073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Inst->setInstantiatedFromMemberTemplate(D);
308a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  Owner->addDecl(Inst);
3103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
3113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  return Inst;
312162e1c1b487352434552147967c3dd296ebee2f7Richard Smith}
313162e1c1b487352434552147967c3dd296ebee2f7Richard Smith
3143d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas GregorDecl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
3159901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // If this is the variable for an anonymous struct or union,
3169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // instantiate the anonymous struct/union type first.
3179901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
3189901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (RecordTy->getDecl()->isAnonymousStructOrUnion())
3199901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
3209901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor        return 0;
3219901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor
322ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  // Do substitution on the type of the declaration
323a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
3240a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         TemplateArgs,
3250a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getTypeSpecStartLoc(),
3260a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                         D->getDeclName());
3270a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall  if (!DI)
3283d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor    return 0;
3293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
330c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  if (DI->getType()->isFunctionType()) {
331c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
332c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor      << D->isStaticDataMember() << DI->getType();
333c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor    return 0;
334c6dbc3fa467e2355b678a6b717534928048efcb2Douglas Gregor  }
335a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
336b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor  // Build the instantiated declaration
3373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
338ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                 D->getInnerLocStart(),
3393d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor                                 D->getLocation(), D->getIdentifier(),
3400a5fa06a688e1086ea553a24b42b9915996ed1f7John McCall                                 DI->getType(), DI,
34116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClass(),
34216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                 D->getStorageClassAsWritten());
3433d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  Var->setThreadSpecified(D->isThreadSpecified());
3445b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl  Var->setInitStyle(D->getInitStyle());
345ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  Var->setCXXForRangeDecl(D->isCXXForRangeDecl());
346796c1a1e3e63e459e371383ac878aa5f40b02a8cRichard Smith  Var->setConstexpr(D->isConstexpr());
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
348b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
349b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Var))
350b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
351b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
3521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a static data member defined
3537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
3547caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
3557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine())
3567caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Var->setLexicalDeclContext(D->getLexicalDeclContext());
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35846460a68f6508775e98c19b4bb8454bb471aac24John McCall  Var->setAccess(D->getAccess());
359a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3606b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  if (!D->isStaticDataMember()) {
361c070cc602d6eefea881f71a60de09e05b54c3fddDouglas Gregor    Var->setUsed(D->isUsed(false));
3626b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis    Var->setReferenced(D->isReferenced());
3636b6b42aed07726178f61954ac6e51f47da00275cArgyrios Kyrtzidis  }
364a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
365671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith  SemaRef.InstantiateAttrs(TemplateArgs, D, Var, LateAttrs, StartingScope);
366671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith
367671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith  if (Var->hasAttrs())
368671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith    SemaRef.CheckAlignasUnderalignment(Var);
369671b3219c2be00ef8f26234ec993816c3ba56a4fRichard Smith
370390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // FIXME: In theory, we could have a previous declaration for variables that
371390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump  // are not static data members.
3726826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  // FIXME: having to fake up a LookupResult is dumb.
3736826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
374449d0a829007ea654912098e6a73134a2c529d61Douglas Gregor                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
37560c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (D->isStaticDataMember())
37660c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    SemaRef.LookupQualifiedName(Previous, Owner, false);
3779aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3789aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor  // In ARC, infer 'retaining' for variables of retainable type.
3794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().ObjCAutoRefCount &&
3809aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor      SemaRef.inferObjCARCLifetime(Var))
3819aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor    Var->setInvalidDecl();
3829aab9c4116bb3ea876d92d4af10bff7f4c451f24Douglas Gregor
3832c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain  SemaRef.CheckVariableDeclaration(Var, Previous);
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (D->isOutOfLine()) {
3863e9ea0b8cd7c4691d62e385245556be5fded58a7Richard Smith    D->getLexicalDeclContext()->addDecl(Var);
3877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->makeDeclVisibleInContext(Var);
3887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  } else {
3897caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Owner->addDecl(Var);
390f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor    if (Owner->isFunctionOrMethod())
391f7d72f5a4a3f0e610d77c6779ca3c21920a14bc7Douglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
3927caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
393be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
394251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Link instantiations of static data members back to the template from
395251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // which they were instantiated.
396251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  if (Var->isStaticDataMember())
397a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
398cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor                                                     TSK_ImplicitInstantiation);
399a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
40060c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  if (Var->getAnyInitializer()) {
40160c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor    // We already have an initializer in the class.
40260c93c9981c467738369702e7aa23fd58c2b6aacDouglas Gregor  } else if (D->getInit()) {
4031f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    if (Var->isStaticDataMember() && !D->isOutOfLine())
404adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith      SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D);
4051f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    else
406adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith      SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D);
4071f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor
4086b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
4095b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs,
4105b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                        D->getInitStyle() == VarDecl::CallInit);
4115b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (!Init.isInvalid()) {
41234b41d939a1328f484511c6002ba2456db879a29Richard Smith      bool TypeMayContainAuto = true;
4135b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      if (Init.get()) {
4145b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        bool DirectInit = D->isDirectInit();
4155b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        SemaRef.AddInitializerToDecl(Var, Init.take(), DirectInit,
4165b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                     TypeMayContainAuto);
4175b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      } else
4186aeaa60217e1ed11a621409acf1b53df0d14b591Eli Friedman        SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto);
4196eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    } else {
4206b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // FIXME: Not too happy about invalidating the declaration
4216b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      // because of a bogus initializer.
4226b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      Var->setInvalidDecl();
4236eef519fc8a97bb7ca6066f23d35e10f06b2c1b5Douglas Gregor    }
424a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
4251f5f3a4d58a1c7c50c331b33329fc14563533c04Douglas Gregor    SemaRef.PopExpressionEvaluationContext();
426ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
427ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith             !Var->isCXXForRangeDecl())
428d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    SemaRef.ActOnUninitializedDecl(Var, false);
4293d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
430e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // Diagnose unused local variables with dependent types, where the diagnostic
431e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  // will have been deferred.
432e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed() &&
433e3499cae8e5323ac553ad56977bf1cd42b9a5a35Richard Smith      D->getType()->isDependentType())
4345764f613e61cb3183f3d7ceeafd23396de96ed16Douglas Gregor    SemaRef.DiagnoseUnusedDecl(Var);
435bbc6454bb98d6a6ecbaafa715222c5db834307f2Argyrios Kyrtzidis
4363d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor  return Var;
4373d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor}
4383d7a12a50558c31d4351e923c15ab57688f4fdf2Douglas Gregor
4396206d53f67613958ae1b023aba337ebb46f11a8bAbramo BagnaraDecl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
4406206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  AccessSpecDecl* AD
4416206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara    = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
4426206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara                             D->getAccessSpecifierLoc(), D->getColonLoc());
4436206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  Owner->addHiddenDecl(AD);
4446206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara  return AD;
4456206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara}
4466206d53f67613958ae1b023aba337ebb46f11a8bAbramo Bagnara
4478dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
4488dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  bool Invalid = false;
449a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = D->getTypeSourceInfo();
450561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (DI->getType()->isInstantiationDependentType() ||
451836adf6771d5170d936599dfcce21687e37e9bbfDouglas Gregor      DI->getType()->isVariablyModifiedType())  {
45207fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    DI = SemaRef.SubstType(DI, TemplateArgs,
45307fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                           D->getLocation(), D->getDeclName());
45407fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    if (!DI) {
455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall      DI = D->getTypeSourceInfo();
45607fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall      Invalid = true;
45707fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall    } else if (DI->getType()->isFunctionType()) {
4588dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      // C++ [temp.arg.type]p3:
4598dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   If a declaration acquires a function type through a type
4608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   dependent on a template-parameter and this causes a
4618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   declaration that does not use the syntactic form of a
4628dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   function declarator to have function type, the program is
4638dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      //   ill-formed.
4648dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
46507fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall        << DI->getType();
4668dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4678dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
468b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor  } else {
469b4eeaff1595b7d0a8fbc2b3c8bec7dc63f48b7fdDouglas Gregor    SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
4708dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4718dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
4728dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *BitWidth = D->getBitWidth();
4738dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (Invalid)
4748dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    BitWidth = 0;
4758dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  else if (BitWidth) {
476f6702a3927147655206ae729a84339c4fda4c651Richard Smith    // The bit-width expression is a constant expression.
477f6702a3927147655206ae729a84339c4fda4c651Richard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
478f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                 Sema::ConstantEvaluated);
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48060d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult InstantiatedBitWidth
481ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      = SemaRef.SubstExpr(BitWidth, TemplateArgs);
4828dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (InstantiatedBitWidth.isInvalid()) {
4838dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Invalid = true;
4848dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      BitWidth = 0;
4858dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    } else
486e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson      BitWidth = InstantiatedBitWidth.takeAs<Expr>();
4878dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
4888dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
48907fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall  FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
49007fb6bef6242c0f2ab47f059583edbdef924823eJohn McCall                                            DI->getType(), DI,
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            cast<RecordDecl>(Owner),
4928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getLocation(),
4938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->isMutable(),
4948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            BitWidth,
495ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                            D->getInClassInitStyle(),
496703b6015176550eefc91f3e2f19cd19beacbc592Richard Smith                                            D->getInnerLocStart(),
4978dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            D->getAccess(),
4988dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                            0);
499663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  if (!Field) {
500663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor    cast<Decl>(Owner)->setInvalidDecl();
501f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    return 0;
502663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  }
5031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50423323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
505a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
506be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith  if (Field->hasAttrs())
507be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith    SemaRef.CheckAlignasUnderalignment(Field);
508be507b6e72df8ab5e7d8c31eb4453e1bdf5fcfafRichard Smith
509f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (Invalid)
510f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    Field->setInvalidDecl();
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
512f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  if (!Field->getDeclName()) {
513f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    // Keep track of where this decl came from.
514f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson    SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
515a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
5169901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
5179901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    if (Parent->isAnonymousStructOrUnion() &&
5187a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl        Parent->getRedeclContext()->isFunctionOrMethod())
5199901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
5208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
522f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Field->setImplicit(D->isImplicit());
52346460a68f6508775e98c19b4bb8454bb471aac24John McCall  Field->setAccess(D->getAccess());
524f4b5f5c6a1487317aab9aa30d97bccfd57c82c98Anders Carlsson  Owner->addDecl(Field);
5258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
5268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Field;
5278dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
5288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
52987c2e121cf0522fc266efe2922b58091cd2e0182Francois PichetDecl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
53087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  NamedDecl **NamedChain =
53187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
53287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
53387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  int i = 0;
53487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  for (IndirectFieldDecl::chain_iterator PI =
53587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet       D->chain_begin(), PE = D->chain_end();
536b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor       PI != PE; ++PI) {
537a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI,
538b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                              TemplateArgs);
539b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!Next)
540b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
541a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
542b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    NamedChain[i++] = Next;
543b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor  }
54487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
54540e17752086c2c497951d64f5ac6ab5039466113Francois Pichet  QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
54687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectFieldDecl* IndirectField
54787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet    = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(),
54840e17752086c2c497951d64f5ac6ab5039466113Francois Pichet                                D->getIdentifier(), T,
54987c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet                                NamedChain, D->getChainingSize());
55087c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
55187c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
55287c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setImplicit(D->isImplicit());
55387c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  IndirectField->setAccess(D->getAccess());
55487c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  Owner->addDecl(IndirectField);
55587c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet  return IndirectField;
55687c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet}
55787c2e121cf0522fc266efe2922b58091cd2e0182Francois Pichet
55802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCallDecl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
55902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // Handle friend type expressions by simply substituting template
56006245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  // parameters into the pattern type and checking the result.
56132f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall  if (TypeSourceInfo *Ty = D->getFriendType()) {
5624fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeSourceInfo *InstTy;
5634fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // If this is an unsupported friend, don't bother substituting template
5644fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // arguments into it. The actual type referred to won't be used by any
5654fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // parts of Clang, and may not be valid for instantiating. Just use the
5664fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    // same info for the instantiated friend.
5674fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (D->isUnsupportedFriend()) {
5684fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = Ty;
5694fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    } else {
5704fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth      InstTy = SemaRef.SubstType(Ty, TemplateArgs,
5714fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getLocation(), DeclarationName());
5724fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    }
5734fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    if (!InstTy)
57406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
575fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
576d6f80daa84164ceeb8900da07f43b6a150edf713Richard Smith    FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
5770216df8fd3ce58f5a68ef2ab141ea34c96c11164Abramo Bagnara                                                 D->getFriendLoc(), InstTy);
57806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    if (!FD)
57906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor      return 0;
580a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
58106245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    FD->setAccess(AS_public);
5829a34edb710917798aa30263374f624f13b594605John McCall    FD->setUnsupportedFriend(D->isUnsupportedFriend());
58306245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    Owner->addDecl(FD);
58406245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor    return FD;
585a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
586a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
58706245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  NamedDecl *ND = D->getFriendDecl();
58806245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  assert(ND && "friend decl must be a decl or a type!");
58932f2fb53d9d7c28c94d8569fd0fcf06cccee0c3dJohn McCall
590af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // All of the Visit implementations for the various potential friend
591af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // declarations have to be carefully written to work for friend
592af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // objects, with the most important detail being that the target
593af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  // decl should almost certainly not be placed in Owner.
594af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  Decl *NewND = Visit(ND);
59506245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor  if (!NewND) return 0;
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59702cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FriendDecl *FD =
598a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
59906245bfb3ae40bb24a8bfb17eafeb266a4daf5caDouglas Gregor                       cast<NamedDecl>(NewND), D->getFriendLoc());
6005fee110ac106370f75592df024001de73edced2aJohn McCall  FD->setAccess(AS_public);
6019a34edb710917798aa30263374f624f13b594605John McCall  FD->setUnsupportedFriend(D->isUnsupportedFriend());
60202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  Owner->addDecl(FD);
60302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  return FD;
604fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall}
605fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
6068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
6078dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  Expr *AssertExpr = D->getAssertExpr();
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
609f6702a3927147655206ae729a84339c4fda4c651Richard Smith  // The expression in a static assertion is a constant expression.
610f6702a3927147655206ae729a84339c4fda4c651Richard Smith  EnterExpressionEvaluationContext Unevaluated(SemaRef,
611f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                               Sema::ConstantEvaluated);
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult InstantiatedAssertExpr
614ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall    = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
6158dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  if (InstantiatedAssertExpr.isInvalid())
6168dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    return 0;
6178dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
618e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith  return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
6199ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                              InstantiatedAssertExpr.get(),
620e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getMessage(),
621e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->getRParenLoc(),
622e3f470a718ec00eb8b546e405fa59bc2df2d7c46Richard Smith                                              D->isFailed());
6238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
6248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
6258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
62638f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  EnumDecl *PrevDecl = 0;
62738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (D->getPreviousDecl()) {
62838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
62938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   D->getPreviousDecl(),
63038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                                   TemplateArgs);
63138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    if (!Prev) return 0;
63238f0df352fadc546c5666079fb22de5ec1819d92Richard Smith    PrevDecl = cast<EnumDecl>(Prev);
63338f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  }
63438f0df352fadc546c5666079fb22de5ec1819d92Richard Smith
635ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
6368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                    D->getLocation(), D->getIdentifier(),
63738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith                                    PrevDecl, D->isScoped(),
638a88cefd266c428be33cc06f7e8b00ff8fc97c1ffAbramo Bagnara                                    D->isScopedUsingClassTag(), D->isFixed());
6391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (D->isFixed()) {
640f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
6411274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // If we have type source information for the underlying type, it means it
6421274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // has been explicitly set by the user. Perform substitution on it before
6431274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      // moving on.
6441274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
645f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
646f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                                                DeclarationName());
647f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
6481274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor        Enum->setIntegerType(SemaRef.Context.IntTy);
649f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      else
650f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith        Enum->setIntegerTypeSourceInfo(NewTI);
651f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    } else {
6521274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      assert(!D->getIntegerType()->isDependentType()
6531274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor             && "Dependent type without type source info");
6541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor      Enum->setIntegerType(D->getIntegerType());
6551274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    }
6561274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  }
6571274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor
6585b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall  SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
6595b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
660f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
66106c0fecd197fef21e265a41bca8dc5022de1f864Douglas Gregor  Enum->setAccess(D->getAccess());
662b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Enum)) return 0;
66317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Enum);
664f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
6654ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  EnumDecl *Def = D->getDefinition();
6664ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  if (Def && Def != D) {
6674ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // If this is an out-of-line definition of an enum member template, check
6684ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // that the underlying types match in the instantiation of both
6694ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    // declarations.
6704ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
6714ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
6724ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      QualType DefnUnderlying =
6734ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith        SemaRef.SubstType(TI->getType(), TemplateArgs,
6744ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                          UnderlyingLoc, DeclarationName());
6754ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith      SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
6764ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith                                     DefnUnderlying, Enum);
6774ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    }
6784ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith  }
6798dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
68096084f171f4824397dc48453146f0a9719cb9247Douglas Gregor  if (D->getDeclContext()->isFunctionOrMethod())
68196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
682a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
683f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // C++11 [temp.inst]p1: The implicit instantiation of a class template
684f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // specialization causes the implicit instantiation of the declarations, but
685f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // not the definitions of scoped member enumerations.
686f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: There appears to be no wording for what happens for an enum defined
68738f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // within a block scope, but we treat that much like a member template. Only
68838f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // instantiate the definition when visiting the definition in that case, since
68938f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  // we will visit all redeclarations.
69038f0df352fadc546c5666079fb22de5ec1819d92Richard Smith  if (!Enum->isScoped() && Def &&
69138f0df352fadc546c5666079fb22de5ec1819d92Richard Smith      (!D->getDeclContext()->isFunctionOrMethod() || D->isCompleteDefinition()))
6924ca93d9978aac02b01814b4f749d6903a1f87ee5Richard Smith    InstantiateEnumDefinition(Enum, Def);
693f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
694f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  return Enum;
695f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith}
696f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
697f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smithvoid TemplateDeclInstantiator::InstantiateEnumDefinition(
698f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith    EnumDecl *Enum, EnumDecl *Pattern) {
699f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  Enum->startDefinition();
700f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith
7011af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  // Update the location to refer to the definition.
7021af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith  Enum->setLocation(Pattern->getLocation());
7031af83c444e5a2f6f50a6e1c15e6ebc618ae18a5fRichard Smith
7045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 4> Enumerators;
7058dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7068dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  EnumConstantDecl *LastEnumConst = 0;
707f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(),
708f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith         ECEnd = Pattern->enumerator_end();
7098dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor       EC != ECEnd; ++EC) {
7108dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // The specified value for the enumerator.
71160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Value = SemaRef.Owned((Expr *)0);
712ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    if (Expr *UninstValue = EC->getInitExpr()) {
713f6702a3927147655206ae729a84339c4fda4c651Richard Smith      // The enumerator's value expression is a constant expression.
7141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      EnterExpressionEvaluationContext Unevaluated(SemaRef,
715f6702a3927147655206ae729a84339c4fda4c651Richard Smith                                                   Sema::ConstantEvaluated);
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
717ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall      Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
718ac7610dad6653bad02dd42de198ca358b6fb1f1dDouglas Gregor    }
7198dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7208dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    // Drop the initial value and continue.
7218dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    bool isInvalid = false;
7228dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (Value.isInvalid()) {
7238dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Value = SemaRef.Owned((Expr *)0);
7248dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      isInvalid = true;
7258dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7268dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EnumConstantDecl *EnumConst
7288dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
7298dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor                                  EC->getLocation(), EC->getIdentifier(),
7309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                  Value.get());
7318dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7328dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (isInvalid) {
7338dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      if (EnumConst)
7348dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor        EnumConst->setInvalidDecl();
7358dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      Enum->setInvalidDecl();
7368dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7378dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7388dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    if (EnumConst) {
739581deb3da481053c4993c7600f97acf7768caac5David Blaikie      SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst);
7405b629aa86c987f276d00453b6c9ab8424f7903feJohn McCall
7413b85ecf2049c8670eba30d0c06f28f64168af9b8John McCall      EnumConst->setAccess(Enum->getAccess());
74217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Enum->addDecl(EnumConst);
743d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Enumerators.push_back(EnumConst);
7448dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor      LastEnumConst = EnumConst;
745a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
746f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith      if (Pattern->getDeclContext()->isFunctionOrMethod() &&
747f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith          !Enum->isScoped()) {
74896084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // If the enumeration is within a function or method, record the enum
74996084f171f4824397dc48453146f0a9719cb9247Douglas Gregor        // constant as a local.
750581deb3da481053c4993c7600f97acf7768caac5David Blaikie        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
75196084f171f4824397dc48453146f0a9719cb9247Douglas Gregor      }
7528dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor    }
7538dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  }
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
755f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  // FIXME: Fixup LBraceLoc
756f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith  SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
757f1c66b40213784a1c4612f04c14cafa2b0e89988Richard Smith                        Enum->getRBraceLoc(), Enum,
758de7a0fcdf9f30cb5a97aab614f3975d93cd9926fEli Friedman                        Enumerators.data(), Enumerators.size(),
759fee13819693c8492f0c364bc704645e844ef737aEdward O'Callaghan                        0, 0);
7608dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
7618dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
7626477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorDecl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
763b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
7646477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor}
7656477b69cc93e0a0ff15036d60d604f3544da0f29Douglas Gregor
766e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
76793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
76893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
769550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template, which
770550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters.
7712a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
772e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *TempParams = D->getTemplateParameters();
773ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
775d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
776e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
777e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *Pattern = D->getTemplatedDecl();
77893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
77993ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // Instantiate the qualifier.  We have to do this first in case
78093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // we're a friend declaration, because if we are then we need to put
78193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the new declaration in the appropriate context.
782c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
783c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
784c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
785c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
786c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
787c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
78893ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
78993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
79093ba8579c341d5329175f1413cdc3b35a36592d2John McCall  CXXRecordDecl *PrevDecl = 0;
79193ba8579c341d5329175f1413cdc3b35a36592d2John McCall  ClassTemplateDecl *PrevClassTemplate = 0;
79293ba8579c341d5329175f1413cdc3b35a36592d2John McCall
793ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (!isFriend && Pattern->getPreviousDecl()) {
79437574f55cd637340f651330f5cfda69742880d36Nick Lewycky    DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
7953bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    if (!Found.empty()) {
7963bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
79737574f55cd637340f651330f5cfda69742880d36Nick Lewycky      if (PrevClassTemplate)
79837574f55cd637340f651330f5cfda69742880d36Nick Lewycky        PrevDecl = PrevClassTemplate->getTemplatedDecl();
79937574f55cd637340f651330f5cfda69742880d36Nick Lewycky    }
80037574f55cd637340f651330f5cfda69742880d36Nick Lewycky  }
80137574f55cd637340f651330f5cfda69742880d36Nick Lewycky
80293ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // If this isn't a friend, then it's a member template, in which
80393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // case we just want to build the instantiation in the
80493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // specialization.  If it is a friend, we want to build it in
80593ba8579c341d5329175f1413cdc3b35a36592d2John McCall  // the appropriate context.
80693ba8579c341d5329175f1413cdc3b35a36592d2John McCall  DeclContext *DC = Owner;
80793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
808c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
80993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      CXXScopeSpec SS;
810c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
81193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.computeDeclContext(SS);
81293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!DC) return 0;
81393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    } else {
81493ba8579c341d5329175f1413cdc3b35a36592d2John McCall      DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
81593ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           Pattern->getDeclContext(),
81693ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                           TemplateArgs);
81793ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
81893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
81993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // Look for a previous declaration of the template in the owning
82093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // context.
82193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
82293ba8579c341d5329175f1413cdc3b35a36592d2John McCall                   Sema::LookupOrdinaryName, Sema::ForRedeclaration);
82393ba8579c341d5329175f1413cdc3b35a36592d2John McCall    SemaRef.LookupQualifiedName(R, DC);
82493ba8579c341d5329175f1413cdc3b35a36592d2John McCall
82593ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (R.isSingleResult()) {
82693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
82793ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (PrevClassTemplate)
82893ba8579c341d5329175f1413cdc3b35a36592d2John McCall        PrevDecl = PrevClassTemplate->getTemplatedDecl();
82993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
83093ba8579c341d5329175f1413cdc3b35a36592d2John McCall
831c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!PrevClassTemplate && QualifierLoc) {
83293ba8579c341d5329175f1413cdc3b35a36592d2John McCall      SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
8331eabb7d0c30f6a876b0fd03ad4656c096c26b8d0Douglas Gregor        << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
834c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor        << QualifierLoc.getSourceRange();
83593ba8579c341d5329175f1413cdc3b35a36592d2John McCall      return 0;
83693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
83793ba8579c341d5329175f1413cdc3b35a36592d2John McCall
838c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor    bool AdoptedPreviousTemplateParams = false;
83993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    if (PrevClassTemplate) {
840c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      bool Complain = true;
841c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
842c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
843c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template for struct std::tr1::__detail::_Map_base, where the
844c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the friend declaration don't match the
845c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // template parameters of the original declaration. In this one
846c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // case, we don't complain about the ill-formed friend
847c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      // declaration.
848a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (isFriend && Pattern->getIdentifier() &&
849c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          Pattern->getIdentifier()->isStr("_Map_base") &&
850c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DC->isNamespace() &&
851c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier() &&
852c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
853c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        DeclContext *DCParent = DC->getParent();
854c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (DCParent->isNamespace() &&
855c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier() &&
856c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
857c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          DeclContext *DCParent2 = DCParent->getParent();
858c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          if (DCParent2->isNamespace() &&
859c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
860c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
861c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor              DCParent2->getParent()->isTranslationUnit())
862c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor            Complain = false;
863c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        }
864c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
865c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
86693ba8579c341d5329175f1413cdc3b35a36592d2John McCall      TemplateParameterList *PrevParams
86793ba8579c341d5329175f1413cdc3b35a36592d2John McCall        = PrevClassTemplate->getTemplateParameters();
86893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
86993ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Make sure the parameter lists match.
87093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
871a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  Complain,
872c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor                                                  Sema::TPL_TemplateMatch)) {
873c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        if (Complain)
874c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          return 0;
875c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor
876c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        AdoptedPreviousTemplateParams = true;
877c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor        InstParams = PrevParams;
878c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      }
87993ba8579c341d5329175f1413cdc3b35a36592d2John McCall
88093ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // Do some additional validation, then merge default arguments
88193ba8579c341d5329175f1413cdc3b35a36592d2John McCall      // from the existing declarations.
882c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor      if (!AdoptedPreviousTemplateParams &&
883c53d0d762010217d02da5aa14be171817d63e3feDouglas Gregor          SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
88493ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                             Sema::TPC_ClassTemplate))
88593ba8579c341d5329175f1413cdc3b35a36592d2John McCall        return 0;
88693ba8579c341d5329175f1413cdc3b35a36592d2John McCall    }
88793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
88893ba8579c341d5329175f1413cdc3b35a36592d2John McCall
889e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  CXXRecordDecl *RecordInst
89093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
891ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getLocStart(), Pattern->getLocation(),
892ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            Pattern->getIdentifier(), PrevDecl,
893f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor                            /*DelayTypeCreation=*/true);
894e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
895c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
896c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    RecordInst->setQualifierInfo(QualifierLoc);
897b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
898e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ClassTemplateDecl *Inst
89993ba8579c341d5329175f1413cdc3b35a36592d2John McCall    = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
90093ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                D->getIdentifier(), InstParams, RecordInst,
90193ba8579c341d5329175f1413cdc3b35a36592d2John McCall                                PrevClassTemplate);
902e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  RecordInst->setDescribedClassTemplate(Inst);
903ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
90493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
905ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    if (PrevClassTemplate)
906ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(PrevClassTemplate->getAccess());
907ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall    else
908ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall      Inst->setAccess(D->getAccess());
909ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
91093ba8579c341d5329175f1413cdc3b35a36592d2John McCall    Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
91193ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // TODO: do we want to track the instantiation progeny of this
91293ba8579c341d5329175f1413cdc3b35a36592d2John McCall    // friend target decl?
91393ba8579c341d5329175f1413cdc3b35a36592d2John McCall  } else {
914e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    Inst->setAccess(D->getAccess());
91537574f55cd637340f651330f5cfda69742880d36Nick Lewycky    if (!PrevClassTemplate)
91637574f55cd637340f651330f5cfda69742880d36Nick Lewycky      Inst->setInstantiatedFromMemberTemplate(D);
91793ba8579c341d5329175f1413cdc3b35a36592d2John McCall  }
918a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
919f0510d49a6985e9284d30cfc36a0df2a6292a638Douglas Gregor  // Trigger creation of the type for the instantiation.
9203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  SemaRef.Context.getInjectedClassNameType(RecordInst,
92124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                    Inst->getInjectedClassNameSpecialization());
922ea7390c7b384ce607bfc8fc13c01f37cfc3776f0John McCall
923259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  // Finish handling of friends.
92493ba8579c341d5329175f1413cdc3b35a36592d2John McCall  if (isFriend) {
9251b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(Inst);
9264c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(Owner);
9274c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(Owner);
928e8c01bdb56549adcecd71ce39160eea54b2c51c8Douglas Gregor    return Inst;
929259571e27e513cfaf691cc7447e09b31a47d5438Douglas Gregor  }
930a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
9314c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  if (D->isOutOfLine()) {
9324c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    Inst->setLexicalDeclContext(D->getLexicalDeclContext());
9334c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara    RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
9344c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara  }
9354c51548271d2f8385127e1d943764ae8939d7794Abramo Bagnara
936e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Owner->addDecl(Inst);
937d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
938d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (!PrevClassTemplate) {
939d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // Queue up any out-of-line partial specializations of this member
940d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // class template; the client will force their instantiation once
941d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    // the enclosing class has been instantiated.
9425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
943d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    D->getPartialSpecializations(PartialSpecs);
944d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
945d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      if (PartialSpecs[I]->isOutOfLine())
946d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
947d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  }
948d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
949e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
950e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
951e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
952d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
9537974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorTemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
9547974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor                                   ClassTemplatePartialSpecializationDecl *D) {
955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
956a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Lookup the already-instantiated declaration in the instantiation
958ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // of the class template and return that.
959ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  DeclContext::lookup_result Found
960ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = Owner->lookup(ClassTemplate->getDeclName());
9613bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie  if (Found.empty())
962ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
963a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
964ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateDecl *InstClassTemplate
9653bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie    = dyn_cast<ClassTemplateDecl>(Found.front());
966ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstClassTemplate)
967ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return 0;
968a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
969d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *Result
970d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor        = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
971d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return Result;
972d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor
973d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
9747974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor}
9757974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas Gregor
9767974c3b7062f85bb7c0ada34526cdefe1d30f89bDouglas GregorDecl *
977d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
978550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this function template, which
979550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // will contain the instantiations of the template parameters and then get
980a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // merged with the local instantiation scope for the function template
981550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // itself.
9822a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
983895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor
984d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
985d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!InstParams)
987d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return NULL;
988a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
989a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  FunctionDecl *Instantiated = 0;
990a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
991a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
992a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                 InstParams));
993a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  else
994a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
995a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                          D->getTemplatedDecl(),
996a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                                InstParams));
997a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
998a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (!Instantiated)
999d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    return 0;
1000d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Link the instantiated function template declaration to the function
1002d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  // template from which it was instantiated.
1003a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  FunctionTemplateDecl *InstTemplate
1004a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    = Instantiated->getDescribedFunctionTemplate();
100537d68185088947322a97eabdc1c0714b0debd929Douglas Gregor  InstTemplate->setAccess(D->getAccess());
1006a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(InstTemplate &&
1007a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor         "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1008e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall
1009b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1010b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1011e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // Link the instantiation back to the pattern *unless* this is a
1012e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  // non-definition friend declaration.
1013e976ffe18ee60b81641423f42ff6feec2f5e3cb7John McCall  if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1014b1a56e767cfb645fcb25027ab728dd5824d92615John McCall      !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1015a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    InstTemplate->setInstantiatedFromMemberTemplate(D);
1016a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1017b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  // Make declarations visible in the appropriate context.
10181f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (!isFriend) {
1019a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Owner->addDecl(InstTemplate);
10201f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (InstTemplate->getDeclContext()->isRecord() &&
10211f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall             !D->getPreviousDecl()) {
10221f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    SemaRef.CheckFriendAccess(InstTemplate);
10231f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  }
1024b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1025d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  return InstTemplate;
1026d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor}
1027d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor
1028d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas GregorDecl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1029d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *PrevDecl = 0;
1030d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (D->isInjectedClassName())
1031d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor    PrevDecl = cast<CXXRecordDecl>(Owner);
1032ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  else if (D->getPreviousDecl()) {
10337c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1034ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor                                                   D->getPreviousDecl(),
10356c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall                                                   TemplateArgs);
10366c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    if (!Prev) return 0;
10376c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall    PrevDecl = cast<CXXRecordDecl>(Prev);
10386c1c1b8c6ca743a5b6b4c81f9ac56392c12c7457John McCall  }
1039d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
1040d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  CXXRecordDecl *Record
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1042ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getLocStart(), D->getLocation(),
1043ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                            D->getIdentifier(), PrevDecl);
1044b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1045b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
1046b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(D, Record))
1047b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
1048b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1049d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  Record->setImplicit(D->isImplicit());
1050eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // FIXME: Check against AS_none is an ugly hack to work around the issue that
1051eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // the tag decls introduced by friend class declarations don't have an access
1052eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  // specifier. Remove once this area of the code gets sorted out.
1053eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman  if (D->getAccess() != AS_none)
1054eaba1af8529c381d7bfd4dd0a8b48d2c01647972Eli Friedman    Record->setAccess(D->getAccess());
1055d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  if (!D->isInjectedClassName())
1056f6b1185f0a8a209c06dfc1efdb6a59cc851e970cDouglas Gregor    Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1057d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
105802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // If the original function was part of a friend declaration,
105902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  // inherit its namespace state.
106002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
106102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
106202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
10639901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  // Make sure that anonymous structs and unions are recorded.
10649901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  if (D->isAnonymousStructOrUnion()) {
10659901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor    Record->setAnonymousStructOrUnion(true);
10667a126a474fdde06382b315b4e3d8ef0a21d4dc31Sebastian Redl    if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod())
10679901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor      SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
10689901c57806f7e36736ed1616e6ab3eebcc99b78cDouglas Gregor  }
1069d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
107017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  Owner->addDecl(Record);
1071d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor  return Record;
1072d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor}
1073d475b8d9e6f5ff0e6ab8d15667ce8a64c7cb9a4dDouglas Gregor
107471074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \brief Adjust the given function type for an instantiation of the
107571074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// given declaration, to cope with modifications to the function's type that
107671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// aren't reflected in the type-source information.
107771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor///
107871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param D The declaration we're instantiating.
107971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor/// \param TInfo The already-instantiated type.
108071074fdf40a8f5b53810712102b58c27efc30759Douglas Gregorstatic QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
108171074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   FunctionDecl *D,
108271074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor                                                   TypeSourceInfo *TInfo) {
1083bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *OrigFunc
1084bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = D->getType()->castAs<FunctionProtoType>();
1085bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  const FunctionProtoType *NewFunc
1086bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    = TInfo->getType()->castAs<FunctionProtoType>();
1087bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1088bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor    return TInfo->getType();
1089bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor
1090bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1091bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  NewEPI.ExtInfo = OrigFunc->getExtInfo();
1092bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor  return Context.getFunctionType(NewFunc->getResultType(),
1093bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                 ArrayRef<QualType>(NewFunc->arg_type_begin(),
1094bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                    NewFunc->getNumArgs()),
1095bed51fef5773f043db2ad13aa2b6d2f8a8bdbdbaDouglas Gregor                                 NewEPI);
109671074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor}
109771074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor
109802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// Normal class members are of more specific types and therefore
109902cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// don't make it here.  This function serves two purposes:
110002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   1) instantiating function templates
110102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///   2) substituting friend declarations
110202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// FIXME: preserve function definitions in case #2
11037557a1348d2821dce126a778aa7acd7a00b814fdDouglas GregorDecl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1104a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                       TemplateParameterList *TemplateParams) {
1105127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // Check whether there is already a function template specialization for
1106127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  // this declaration.
1107127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1108b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate && !TemplateParams) {
1109a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
111024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11121e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
11132c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
11142c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
11152c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1117127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor    // If we already have a function template specialization, return it.
11182c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
11192c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
1120127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1122b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1123b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1124b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1125b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1126b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1127b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
112879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1129b212d9a8e10308cde5b7a1ce07bd59d8df14fa06Douglas Gregor    Owner->isFunctionOrMethod() ||
1130a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
113179c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
11322a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
113564b4b43a23aa8b8009470e3cc451333f623d7d58David Blaikie  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
113621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
11372dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
113871074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1139fd810b1386ed29b250e7d522ea826a65c815e49dJohn McCall
1140c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1141c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1142c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1143c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor                                                       TemplateArgs);
1144c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (!QualifierLoc)
1145c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1146d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
1147d325daa506338ab86f9dd468b48fd010673f49a6John McCall
114868b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // If we're instantiating a local function declaration, put the result
114968b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  // in the owner;  otherwise we need to find the instantiated context.
115068b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  DeclContext *DC;
115168b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall  if (D->getDeclContext()->isFunctionOrMethod())
115268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall    DC = Owner;
1153c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  else if (isFriend && QualifierLoc) {
1154d325daa506338ab86f9dd468b48fd010673f49a6John McCall    CXXScopeSpec SS;
1155c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    SS.Adopt(QualifierLoc);
1156d325daa506338ab86f9dd468b48fd010673f49a6John McCall    DC = SemaRef.computeDeclContext(SS);
1157d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (!DC) return 0;
1158d325daa506338ab86f9dd468b48fd010673f49a6John McCall  } else {
1159a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
11607c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor                                         TemplateArgs);
1161d325daa506338ab86f9dd468b48fd010673f49a6John McCall  }
116268b6b87b6beb7922fc2c8ab923ba2ce125490363John McCall
116302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  FunctionDecl *Function =
1164ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara      FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1165635311f94e8fd4ff153130d91046ff78ffe97b06Abramo Bagnara                           D->getNameInfo(), T, TInfo,
116616573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                           D->getStorageClass(), D->getStorageClassAsWritten(),
1167af1fc7af351758b0ea0d285bdfe5640128109a4eRichard Smith                           D->isInlineSpecified(), D->hasWrittenPrototype(),
116886c3ae46250cdcc57778c27826060779a92f3815Richard Smith                           D->isConstexpr());
1169b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1170d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1171d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
1172d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1173c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1174c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Function->setQualifierInfo(QualifierLoc);
1175b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1176b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  DeclContext *LexicalDC = Owner;
1177b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  if (!isFriend && D->isOutOfLine()) {
1178b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    assert(D->getDeclContext()->isFileContext());
1179b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    LexicalDC = D->getDeclContext();
1180b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  }
1181b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1182b1a56e767cfb645fcb25027ab728dd5824d92615John McCall  Function->setLexicalDeclContext(LexicalDC);
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1184e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // Attach the parameters
11855cbe101b502e06d16bc77df45a27ce8bc13f33c8Douglas Gregor  if (isa<FunctionProtoType>(Function->getType().IgnoreParens())) {
11861d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Adopt the already-instantiated parameters into our own context.
11871d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (unsigned P = 0; P < Params.size(); ++P)
11881d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      if (Params[P])
11891d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        Params[P]->setOwningFunction(Function);
11901d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  } else {
11911d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // Since we were instantiated via a typedef of a function type, create
11921d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    // new parameters.
11931d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    const FunctionProtoType *Proto
11941d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      = Function->getType()->getAs<FunctionProtoType>();
11951d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    assert(Proto && "No function prototype in template instantiation?");
11961d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
11971d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor         AE = Proto->arg_type_end(); AI != AE; ++AI) {
11981d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      ParmVarDecl *Param
11991d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
12001d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor                                             *AI);
12011d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Param->setScopeInfo(0, Params.size());
12021d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor      Params.push_back(Param);
12031d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor    }
12041d441ee69aaf3b3afa4521d05df934c5c7ea5f62Douglas Gregor  }
12054278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Function->setParams(Params);
120602cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
1207ac7c2c8a9d47df7d652364af3043c41816a18fa4Douglas Gregor  SourceLocation InstantiateAtPOI;
1208a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  if (TemplateParams) {
1209a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1210a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // are substituting only the outer template parameters. For example, given
1211a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1212a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   template<typename T>
1213a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   struct X {
1214a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //     template<typename U> friend void f(T, U);
1215a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   };
1216a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1217a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //   X<int> x;
1218a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    //
1219a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // We are instantiating the friend function template "f" within X<int>,
1220a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // which means substituting int for T, but leaving "f" as a friend function
1221a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // template.
1222a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // Build the function template itself.
1223d325daa506338ab86f9dd468b48fd010673f49a6John McCall    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1224a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getLocation(),
1225a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    Function->getDeclName(),
1226a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor                                                    TemplateParams, Function);
1227a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    Function->setDescribedFunctionTemplate(FunctionTemplate);
1228b1a56e767cfb645fcb25027ab728dd5824d92615John McCall
1229b1a56e767cfb645fcb25027ab728dd5824d92615John McCall    FunctionTemplate->setLexicalDeclContext(LexicalDC);
1230d325daa506338ab86f9dd468b48fd010673f49a6John McCall
1231d325daa506338ab86f9dd468b48fd010673f49a6John McCall    if (isFriend && D->isThisDeclarationADefinition()) {
1232d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // TODO: should we remember this connection regardless of whether
1233d325daa506338ab86f9dd468b48fd010673f49a6John McCall      // the friend declaration provided a body?
1234d325daa506338ab86f9dd468b48fd010673f49a6John McCall      FunctionTemplate->setInstantiatedFromMemberTemplate(
1235d325daa506338ab86f9dd468b48fd010673f49a6John McCall                                           D->getDescribedFunctionTemplate());
1236d325daa506338ab86f9dd468b48fd010673f49a6John McCall    }
123766724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
123866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1239a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
124024bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1241838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Function->setFunctionTemplateSpecialization(FunctionTemplate,
1242910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                            TemplateArgumentList::CreateCopy(SemaRef.Context,
124324bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.first,
124424bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor                                                             Innermost.second),
12451e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                                /*InsertPos=*/0);
124680f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth  } else if (isFriend) {
124780f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Note, we need this connection even if the friend doesn't have a body.
124880f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // Its body may exist but not have been attached yet due to deferred
124980f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // parsing.
125080f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // FIXME: It might be cleaner to set this when attaching the body to the
125180f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // friend function declaration, however that would require finding all the
125280f5b16efb658dabbcf971f42ed8b789aaaa6baaChandler Carruth    // instantiations and modifying them.
1253d325daa506338ab86f9dd468b48fd010673f49a6John McCall    Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
125402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  }
1255a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1256e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(Function, D))
1257e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    Function->setInvalidDecl();
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1259af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  bool isExplicitSpecialization = false;
1260a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
12616826314938f8510cd1a6b03b5d032592456ae27bJohn McCall  LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
12626826314938f8510cd1a6b03b5d032592456ae27bJohn McCall                        Sema::LookupOrdinaryName, Sema::ForRedeclaration);
12636826314938f8510cd1a6b03b5d032592456ae27bJohn McCall
1264af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  if (DependentFunctionTemplateSpecializationInfo *Info
1265af2094e7cecadf36667deb61a83587ffdd979bd3John McCall        = D->getDependentSpecializationInfo()) {
1266af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    assert(isFriend && "non-friend has dependent specialization info?");
1267af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1268af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // This needs to be set now for future sanity.
1269af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1270af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1271af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Instantiate the explicit template arguments.
1272af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1273af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                          Info->getRAngleLoc());
1274e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1275e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                      ExplicitArgs, TemplateArgs))
1276e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      return 0;
1277af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1278af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    // Map the candidate templates to their instantiations.
1279af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1280af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1281af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                Info->getTemplate(I),
1282af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                TemplateArgs);
1283af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      if (!Temp) return 0;
1284af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1285af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1286af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    }
1287af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1288af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1289af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    &ExplicitArgs,
1290af2094e7cecadf36667deb61a83587ffdd979bd3John McCall                                                    Previous))
1291af2094e7cecadf36667deb61a83587ffdd979bd3John McCall      Function->setInvalidDecl();
1292a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1293af2094e7cecadf36667deb61a83587ffdd979bd3John McCall    isExplicitSpecialization = true;
1294af2094e7cecadf36667deb61a83587ffdd979bd3John McCall
1295af2094e7cecadf36667deb61a83587ffdd979bd3John McCall  } else if (TemplateParams || !FunctionTemplate) {
1296a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // Look only into the namespace where the friend would be declared to
1297a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // find a previous declaration. This is the innermost enclosing namespace,
1298a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // as described in ActOnFriendFunctionDecl.
12996826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    SemaRef.LookupQualifiedName(Previous, DC);
1300a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1301a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1302a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1303a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1304a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
13056826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
13066826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1307a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1308a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
13099f54ad4381370c6b771424b53d219e661d6d6706John McCall  SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
13102c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain                                   isExplicitSpecialization);
1311e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
131276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  NamedDecl *PrincipalDecl = (TemplateParams
131376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              ? cast<NamedDecl>(FunctionTemplate)
131476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall                              : Function);
131576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1316a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // If the original function was part of a friend declaration,
1317a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  // inherit its namespace state and add it to the owner.
1318d325daa506338ab86f9dd468b48fd010673f49a6John McCall  if (isFriend) {
13196826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    NamedDecl *PrevDecl;
132076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    if (TemplateParams)
1321ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = FunctionTemplate->getPreviousDecl();
132276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    else
1323ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      PrevDecl = Function->getPreviousDecl();
132476d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
132576d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
13261b7f9cbed1b96b58a6e5f7808ebc9345a76a0936Richard Smith    DC->makeDeclVisibleInContext(PrincipalDecl);
1327ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
132877535dfde258765c056ef4c6786ef56cc48f0c76Gabor Greif    bool queuedInstantiation = false;
1329ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif
133053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++98 [temp.friend]p5: When a function is defined in a friend function
133153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is defined at each
133253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   instantiation of the class template. The function is defined even if it
133353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   is never used.
133453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // C++11 [temp.friend]p4: When a function is defined in a friend function
133553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   declaration in a class template, the function is instantiated when the
133653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //   function is odr-used.
133753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    //
133853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // If -Wc++98-compat is enabled, we go through the motions of checking for a
133953e535161dfa9850de394b300915fc250eb0fdf4Richard Smith    // redefinition, but don't instantiate the function.
134080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if ((!SemaRef.getLangOpts().CPlusPlus11 ||
134153e535161dfa9850de394b300915fc250eb0fdf4Richard Smith         SemaRef.Diags.getDiagnosticLevel(
134253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             diag::warn_cxx98_compat_friend_redefinition,
134353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith             Function->getLocation())
134453e535161dfa9850de394b300915fc250eb0fdf4Richard Smith           != DiagnosticsEngine::Ignored) &&
1345238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        D->isThisDeclarationADefinition()) {
1346238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for a function body.
1347238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      const FunctionDecl *Definition = 0;
134810620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt      if (Function->isDefined(Definition) &&
1349238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
135053e535161dfa9850de394b300915fc250eb0fdf4Richard Smith        SemaRef.Diag(Function->getLocation(),
135180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                     SemaRef.getLangOpts().CPlusPlus11 ?
135253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::warn_cxx98_compat_friend_redefinition :
135353e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                       diag::err_redefinition) << Function->getDeclName();
1354238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
135580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith        if (!SemaRef.getLangOpts().CPlusPlus11)
135653e535161dfa9850de394b300915fc250eb0fdf4Richard Smith          Function->setInvalidDecl();
1357a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      }
1358238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // Check for redefinitions due to other instantiations of this or
1359238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      // a similar friend function.
1360238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1361238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                                           REnd = Function->redecls_end();
1362238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                R != REnd; ++R) {
136313a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif        if (*R == Function)
136413a8affbb87cdb869adabe2a6e5998559f2598c4Gabor Greif          continue;
1365ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        switch (R->getFriendObjectKind()) {
1366ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        case Decl::FOK_None:
136780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith          if (!SemaRef.getLangOpts().CPlusPlus11 &&
136853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              !queuedInstantiation && R->isUsed(false)) {
1369ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            if (MemberSpecializationInfo *MSInfo
1370ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                = Function->getMemberSpecializationInfo()) {
1371ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              if (MSInfo->getPointOfInstantiation().isInvalid()) {
1372ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SourceLocation Loc = R->getLocation(); // FIXME
1373ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                MSInfo->setPointOfInstantiation(Loc);
1374ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                SemaRef.PendingLocalImplicitInstantiations.push_back(
1375ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                                                 std::make_pair(Function, Loc));
1376ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif                queuedInstantiation = true;
1377ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif              }
1378ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif            }
1379ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          }
1380ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif          break;
1381ab297ac4d3f7c078a34c75493b840ef6b5801be3Gabor Greif        default:
1382238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor          if (const FunctionDecl *RPattern
13836a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              = R->getTemplateInstantiationPattern())
138410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt            if (RPattern->isDefined(RPattern)) {
138553e535161dfa9850de394b300915fc250eb0fdf4Richard Smith              SemaRef.Diag(Function->getLocation(),
138680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                           SemaRef.getLangOpts().CPlusPlus11 ?
138753e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::warn_cxx98_compat_friend_redefinition :
138853e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                             diag::err_redefinition)
1389238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor                << Function->getDeclName();
13906a557d8f6b3d7a747a0b57960d4b86f05ba2e53cGabor Greif              SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
139180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith              if (!SemaRef.getLangOpts().CPlusPlus11)
139253e535161dfa9850de394b300915fc250eb0fdf4Richard Smith                Function->setInvalidDecl();
1393238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor              break;
1394238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor            }
1395238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor        }
1396238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor      }
1397238058c372cfb7bdaf489f51171eb1380ebfd6a6Douglas Gregor    }
1398a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor  }
1399a735b206fdb5c15767a45289e1ffb3b568f70f2bDouglas Gregor
140076d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall  if (Function->isOverloadedOperator() && !DC->isRecord() &&
140176d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
140276d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall    PrincipalDecl->setNonMemberOperator();
140376d326448d7e4c10b2896edc2ee855d1e68d1b88John McCall
1404eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt  assert(!D->isDefaulted() && "only methods should be defaulted");
1405e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return Function;
1406e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
14072dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1408d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorDecl *
1409d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas GregorTemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1410af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      TemplateParameterList *TemplateParams,
1411af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                      bool IsClassScopeSpecialization) {
14126b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1413d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (FunctionTemplate && !TemplateParams) {
14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // We are creating a function template specialization from a function
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // template. Check whether there is already a function template
1416d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // specialization for this particular set of template arguments.
1417a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
141824bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14201e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor    void *InsertPos = 0;
14212c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    FunctionDecl *SpecFunc
14222c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
14232c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis                                             InsertPos);
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14256b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor    // If we already have a function template specialization, return it.
14262c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis    if (SpecFunc)
14272c853e401ca406d417eb916e867226050e7be06bArgyrios Kyrtzidis      return SpecFunc;
14286b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor  }
14296b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1430b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  bool isFriend;
1431b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate)
1432b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1433b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  else
1434b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1435b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
143679c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor  bool MergeWithParentScope = (TemplateParams != 0) ||
1437a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    !(isa<Decl>(Owner) &&
143879c2278a66d8fc0943774d1b7c71a32f7764e1e2Douglas Gregor      cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
14392a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
144048dd19b19ddb9e105f8cf0bf6f0732ca4e6a385bDouglas Gregor
14414eab39f0745fb1949dbb40c4145771b927888242John McCall  // Instantiate enclosing template arguments for friends.
14425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateParameterList *, 4> TempParamLists;
14434eab39f0745fb1949dbb40c4145771b927888242John McCall  unsigned NumTempParamLists = 0;
14444eab39f0745fb1949dbb40c4145771b927888242John McCall  if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
14454eab39f0745fb1949dbb40c4145771b927888242John McCall    TempParamLists.set_size(NumTempParamLists);
14464eab39f0745fb1949dbb40c4145771b927888242John McCall    for (unsigned I = 0; I != NumTempParamLists; ++I) {
14474eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *TempParams = D->getTemplateParameterList(I);
14484eab39f0745fb1949dbb40c4145771b927888242John McCall      TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
14494eab39f0745fb1949dbb40c4145771b927888242John McCall      if (!InstParams)
14504eab39f0745fb1949dbb40c4145771b927888242John McCall        return NULL;
14514eab39f0745fb1949dbb40c4145771b927888242John McCall      TempParamLists[I] = InstParams;
14524eab39f0745fb1949dbb40c4145771b927888242John McCall    }
14534eab39f0745fb1949dbb40c4145771b927888242John McCall  }
14544eab39f0745fb1949dbb40c4145771b927888242John McCall
14555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ParmVarDecl *, 4> Params;
1456dc370c1e70a2f876c65be4057ead751b72c8ddd5Benjamin Kramer  TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
145721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!TInfo)
14582dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor    return 0;
145971074fdf40a8f5b53810712102b58c27efc30759Douglas Gregor  QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
14602dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1461723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // \brief If the type of this function, after ignoring parentheses,
1462723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // is not *directly* a function type, then we're instantiating a function
1463723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  // that was declared via a typedef, e.g.,
14645f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
14655f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   typedef int functype(int, int);
14665f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //   functype func;
14675f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  //
14685f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // In this case, we'll just go instantiate the ParmVarDecls that we
14695f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  // synthesized in the method declaration.
1470723df245307a530da5433dfb43accf187dc3e243Abramo Bagnara  if (!isa<FunctionProtoType>(T.IgnoreParens())) {
14715f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor    assert(!Params.size() && "Instantiating type could not yield parameters");
14725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 4> ParamTypes;
1473a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
1474a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                               D->getNumParams(), TemplateArgs, ParamTypes,
147512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor                               &Params))
1476a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      return 0;
14775f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor  }
14785f970eee81372dfc6a1457c3d6d052af04e32a38Douglas Gregor
1479c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1480c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc) {
1481c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1482b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                                 TemplateArgs);
1483a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    if (!QualifierLoc)
1484c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      return 0;
1485b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1486b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
1487b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  DeclContext *DC = Owner;
1488b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
1489c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    if (QualifierLoc) {
1490b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      CXXScopeSpec SS;
1491c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor      SS.Adopt(QualifierLoc);
1492b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.computeDeclContext(SS);
1493c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall
1494c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall      if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1495c54d688c604d28dcb9ab8f92047837c10c8f9c61John McCall        return 0;
1496b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else {
1497b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1498b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           D->getDeclContext(),
1499b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall                                           TemplateArgs);
1500b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    }
1501b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (!DC) return 0;
1502b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1503b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall
15042dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  // Build the instantiated method declaration.
1505b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1506dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  CXXMethodDecl *Method = 0;
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1508ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara  SourceLocation StartLoc = D->getInnerLocStart();
15092577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  DeclarationNameInfo NameInfo
15102577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
151117e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1513ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                        StartLoc, NameInfo, T, TInfo,
15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        Constructor->isExplicit(),
151516573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                        Constructor->isInlineSpecified(),
151686c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                        false, Constructor->isConstexpr());
151717e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
151817e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor    Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1519ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                                       Destructor->isInlineSpecified(),
152116573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                       false);
152265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
152365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1524ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                       StartLoc, NameInfo, T, TInfo,
15250130f3cc4ccd5f46361c48d5fe94133d74619424Douglas Gregor                                       Conversion->isInlineSpecified(),
1526f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                       Conversion->isExplicit(),
152786c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                       Conversion->isConstexpr(),
15289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith                                       Conversion->getLocEnd());
1529dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  } else {
15302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1531ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                   StartLoc, NameInfo, T, TInfo,
153216573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->isStatic(),
153316573fa9705b546b7597c273b25b85d6321e2b33Douglas Gregor                                   D->getStorageClassAsWritten(),
1534f52516038ab5d0b1b90a6dd32f46b7d6dabd04c8Douglas Gregor                                   D->isInlineSpecified(),
153586c3ae46250cdcc57778c27826060779a92f3815Richard Smith                                   D->isConstexpr(), D->getLocEnd());
1536dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
15376b906869527be40b0d38d755e9ef51b331b88162Douglas Gregor
1538d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (D->isInlined())
1539d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Method->setImplicitlyInline();
1540d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
1541c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor  if (QualifierLoc)
1542c22b5fff39a7520207f165fb16a27a34b944bd9cDouglas Gregor    Method->setQualifierInfo(QualifierLoc);
1543b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
1544d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor  if (TemplateParams) {
1545d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Our resulting instantiation is actually a function template, since we
1546d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // are substituting only the outer template parameters. For example, given
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
1548d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   template<typename T>
1549d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   struct X {
1550d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //     template<typename U> void f(T, U);
1551d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   };
1552d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1553d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //   X<int> x;
1554d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    //
1555d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // We are instantiating the member template "f" within X<int>, which means
1556d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // substituting int for T, but leaving "f" as a member function template.
1557d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    // Build the function template itself.
1558d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1559d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    Method->getLocation(),
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Method->getDeclName(),
1561d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor                                                    TemplateParams, Method);
1562b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    if (isFriend) {
1563b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setLexicalDeclContext(Owner);
1564b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall      FunctionTemplate->setObjectOfFriendDecl(true);
1565b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    } else if (D->isOutOfLine())
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1567d60e105e6d1624da647ef7dd35a9cf6fad1b763eDouglas Gregor    Method->setDescribedFunctionTemplate(FunctionTemplate);
156866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  } else if (FunctionTemplate) {
156966724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record this function template specialization.
1570a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    std::pair<const TemplateArgument *, unsigned> Innermost
157124bae92f08ae098cc50a602d8cf1273b423e14daDouglas Gregor      = TemplateArgs.getInnermost();
1572838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    Method->setFunctionTemplateSpecialization(FunctionTemplate,
1573910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                         TemplateArgumentList::CreateCopy(SemaRef.Context,
1574910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.first,
1575910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                          Innermost.second),
15761e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor                                              /*InsertPos=*/0);
1577b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (!isFriend) {
157866724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor    // Record that this is an instantiation of a member function.
15792db323294ac02296125e1e0beb4c3595992e75bbDouglas Gregor    Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
158066724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor  }
1581a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we are instantiating a member function defined
15837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // out-of-line, the instantiation will have the same lexical
15847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // context (which will be a namespace scope) as the template.
1585b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (isFriend) {
15864eab39f0745fb1949dbb40c4145771b927888242John McCall    if (NumTempParamLists)
15874eab39f0745fb1949dbb40c4145771b927888242John McCall      Method->setTemplateParameterListsInfo(SemaRef.Context,
15884eab39f0745fb1949dbb40c4145771b927888242John McCall                                            NumTempParamLists,
15894eab39f0745fb1949dbb40c4145771b927888242John McCall                                            TempParamLists.data());
15904eab39f0745fb1949dbb40c4145771b927888242John McCall
1591b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setLexicalDeclContext(Owner);
1592b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    Method->setObjectOfFriendDecl(true);
1593b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (D->isOutOfLine())
15947caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    Method->setLexicalDeclContext(D->getLexicalDeclContext());
15951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15965545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // Attach the parameters
15975545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  for (unsigned P = 0; P < Params.size(); ++P)
15985545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Params[P]->setOwningFunction(Method);
15994278c654b645402554eb52a48e9c7097c9f1233aDavid Blaikie  Method->setParams(Params);
16005545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
16015545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  if (InitMethodInstantiation(Method, D))
16025545e166a956a20d7a6b58408e251a1119025485Douglas Gregor    Method->setInvalidDecl();
16032dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
16042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
16052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara                        Sema::ForRedeclaration);
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1607b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (!FunctionTemplate || TemplateParams || isFriend) {
1608b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall    SemaRef.LookupQualifiedName(Previous, Record);
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1610dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // In C++, the previous declaration we find might be a tag type
1611dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // (class or enum). In this case, the new declaration will hide the
1612dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // tag type. Note that this does does not apply if we're declaring a
1613dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor    // typedef (C++ [dcl.typedef]p4).
16146826314938f8510cd1a6b03b5d032592456ae27bJohn McCall    if (Previous.isSingleTagDecl())
16156826314938f8510cd1a6b03b5d032592456ae27bJohn McCall      Previous.clear();
1616dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  }
16172dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1618af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (!IsClassScopeSpecialization)
16192c712f50cd56eaf3662989b556e9c6b1e8fcd11aKaelyn Uhrain    SemaRef.CheckFunctionDeclaration(0, Method, Previous, false);
162065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
16214ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor  if (D->isPure())
16224ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor    SemaRef.CheckPureMethod(Method, SourceRange());
16234ba3136b3eb9740a07bd61d0ab23ce9a8d894deeDouglas Gregor
16241f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Propagate access.  For a non-friend declaration, the access is
16251f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // whatever we're propagating from.  For a friend, it should be the
16261f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // previous declaration we just found.
16271f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (isFriend && Method->getPreviousDecl())
16281f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(Method->getPreviousDecl()->getAccess());
16291f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  else
16301f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Method->setAccess(D->getAccess());
16311f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  if (FunctionTemplate)
16321f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    FunctionTemplate->setAccess(Method->getAccess());
163346460a68f6508775e98c19b4bb8454bb471aac24John McCall
16349eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson  SemaRef.CheckOverrideControl(Method);
16359eefa229dfb71400a6bbee326420a7f0e2e91f1fAnders Carlsson
16363bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  // If a function is defined as defaulted or deleted, mark it as such now.
1637ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith  if (D->isExplicitlyDefaulted())
1638ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDefaulted(Method, Method->getLocation());
16393bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman  if (D->isDeletedAsWritten())
1640ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith    SemaRef.SetDeclDeleted(Method, Method->getLocation());
16413bc451593fa44bfc45753e44e37cb4242e714f82Eli Friedman
16421f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // If there's a function template, let our caller handle it.
1643b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  if (FunctionTemplate) {
16441f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
16451f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16461f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Don't hide a (potentially) valid declaration with an invalid one.
1647b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  } else if (Method->isInvalidDecl() && !Previous.empty()) {
16481f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // do nothing
16491f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16501f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, check access to friends and make them visible.
16511f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (isFriend) {
16521f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // We only need to re-check access for methods which we didn't
16531f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    // manage to match during parsing.
16541f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    if (!D->getPreviousDecl())
16551f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall      SemaRef.CheckFriendAccess(Method);
16561f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16571f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Record->makeDeclVisibleInContext(Method);
16581f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall
16591f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // Otherwise, add the declaration.  We don't need to do this for
16601f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // class-scope specializations because we'll have matched them with
16611f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  // the appropriate template.
16621f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall  } else if (!IsClassScopeSpecialization) {
16631f2e1a96bec2ba6418ae7f2d2b525a3575203b6aJohn McCall    Owner->addDecl(Method);
1664b0cb022daec8671406ab25f4b5d5a6d48d823bc4John McCall  }
1665eb88ae5f9acdd17ec76fb83e151a77e25e4e8f31Sean Hunt
16662dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor  return Method;
16672dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
16682dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1669615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1670dec06664a1c4d8984251083db2215875aea1c80dDouglas Gregor  return VisitCXXMethodDecl(D);
1671615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor}
1672615c5d4674355ba830b9978f462ca7a8c5d15f85Douglas Gregor
167303b2b07aaef3a585aec13048a33356c7f635de72Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
167417e32f30e2d1eaf6639d3d4e2196a8d7c709fbacDouglas Gregor  return VisitCXXMethodDecl(D);
167503b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor}
167603b2b07aaef3a585aec13048a33356c7f635de72Douglas Gregor
1677bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas GregorDecl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
167865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return VisitCXXMethodDecl(D);
1679bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor}
1680bb969ed4193e2eadabfaa0dfd0b94312b6146349Douglas Gregor
16816477b69cc93e0a0ff15036d60d604f3544da0f29Douglas GregorParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
168266874fb18afbffb8b2ca05576851a64534be3352David Blaikie  return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
168366874fb18afbffb8b2ca05576851a64534be3352David Blaikie                                  /*ExpectParameterPack=*/ false);
16842dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor}
16852dc0e64e57b2a1786fa53a7dbd1d5c8e255eadb0Douglas Gregor
1686e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallDecl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1687e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                                    TemplateTypeParmDecl *D) {
1688e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // TODO: don't always clone when decls are refcounted.
16894fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  assert(D->getTypeForDecl()->isTemplateTypeParmType());
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1691e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateTypeParmDecl *Inst =
1692344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara    TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1693344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                 D->getLocStart(), D->getLocation(),
16944fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getDepth() - TemplateArgs.getNumLevels(),
16954fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 D->getIndex(), D->getIdentifier(),
1696e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->wasDeclaredWithTypename(),
1697e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                 D->isParameterPack());
16989a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Inst->setAccess(AS_public);
1699a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17000f8716b7bb25d61a82f699b3975167451f7b5a68Douglas Gregor  if (D->hasDefaultArgument())
1701a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
1702e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
1703a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1704550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1705550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1706a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1707e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return Inst;
1708e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall}
1709e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
171033642df30088f2ddb0b22c609523ab8df9dff595Douglas GregorDecl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
171133642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor                                                 NonTypeTemplateParmDecl *D) {
171233642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  // Substitute into the type of the non-type template parameter.
17136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
17145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
17155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 4> ExpandedParameterPackTypes;
17166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  bool IsExpandedParameterPack = false;
1717a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  TypeSourceInfo *DI;
171833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  QualType T;
171933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  bool Invalid = false;
17206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
17216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (D->isExpandedParameterPack()) {
1722a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    // The non-type template parameter pack is an already-expanded pack
17236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // expansion of types. Substitute into each of the expanded types.
17246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
17256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
17266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
17276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
17286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               TemplateArgs,
1729a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                               D->getLocation(),
17306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                               D->getDeclName());
17316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewDI)
17326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1733a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
17366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NewT.isNull())
17386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
17396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      ExpandedParameterPackTypes.push_back(NewT);
17406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
1741a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    IsExpandedParameterPack = true;
17436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    DI = D->getTypeSourceInfo();
17446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    T = DI->getType();
17456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
17466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // The non-type template parameter pack's type is a pack expansion of types.
17476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether we need to expand this parameter pack into separate
17486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // types.
174939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
17506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    TypeLoc Pattern = Expansion.getPatternLoc();
17515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
17526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1753a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Determine whether the set of unexpanded parameter packs can and should
17556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // be expanded.
17566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool Expand = true;
17576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    bool RetainExpansion = false;
1758dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> OrigNumExpansions
17596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      = Expansion.getTypePtr()->getNumExpansions();
1760dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions = OrigNumExpansions;
17616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
17626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                Pattern.getSourceRange(),
1763a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                                Unexpanded,
17646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TemplateArgs,
1765a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                Expand, RetainExpansion,
17666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NumExpansions))
17676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1768a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (Expand) {
17706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
17716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
17726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1773a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                  D->getLocation(),
17746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getDeclName());
17756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (!NewDI)
17766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
1777a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypesAsWritten.push_back(NewDI);
17796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
17806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              NewDI->getType(),
17816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                              D->getLocation());
17826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        if (NewT.isNull())
17836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          return 0;
17846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ExpandedParameterPackTypes.push_back(NewT);
17856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1786a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
17876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // Note that we have an expanded parameter pack. The "type" of this
17886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // expanded parameter pack is the original expansion type, but callers
17896952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // will end up using the expanded parameter pack types for type-checking.
17906952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      IsExpandedParameterPack = true;
17916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = D->getTypeSourceInfo();
17926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
17936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else {
17946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // We cannot fully expand the pack expansion now, so substitute into the
17956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      // pattern and create a new pack expansion type.
17966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
17976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1798a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     D->getLocation(),
17996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     D->getDeclName());
18006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!NewPattern)
18016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1802a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
18046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                      NumExpansions);
18056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (!DI)
18066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        return 0;
1807a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = DI->getType();
18096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
18106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  } else {
18116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Simple case: substitution into a parameter that is not a parameter pack.
1812a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
18136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                           D->getLocation(), D->getDeclName());
18146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (!DI)
18156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      return 0;
1816a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    // Check that this type is acceptable for a non-type template parameter.
1818a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
18196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                  D->getLocation());
18206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    if (T.isNull()) {
18216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      T = SemaRef.Context.IntTy;
18226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      Invalid = true;
18236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    }
182433642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  }
1825a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  NonTypeTemplateParmDecl *Param;
18276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  if (IsExpandedParameterPack)
1828a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1829ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
1830ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getLocation(),
1831a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1832a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
18336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getIdentifier(), T,
18346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            DI,
18356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.data(),
18366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            ExpandedParameterPackTypes.size(),
18376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                    ExpandedParameterPackTypesAsWritten.data());
18386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor  else
1839a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
1840ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                            D->getInnerLocStart(),
18416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->getLocation(),
1842a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                    D->getDepth() - TemplateArgs.getNumLevels(),
1843a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getPosition(),
1844a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                            D->getIdentifier(), T,
18456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                            D->isParameterPack(), DI);
1846a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
18479a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
184833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  if (Invalid)
184933642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor    Param->setInvalidDecl();
1850a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1851d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
1852a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1853a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
1854550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // scope.
1855550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
185633642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor  return Param;
185733642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor}
185833642df30088f2ddb0b22c609523ab8df9dff595Douglas Gregor
18596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smithstatic void collectUnexpandedParameterPacks(
18606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Sema &S,
18616964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    TemplateParameterList *Params,
18626964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
18636964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  for (TemplateParameterList::const_iterator I = Params->begin(),
18646964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             E = Params->end(); I != E; ++I) {
18656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if ((*I)->isTemplateParameterPack())
18666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      continue;
18676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
18686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
18696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                        Unexpanded);
18706964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
18716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
18726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                      Unexpanded);
18736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  }
18746964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith}
18756964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
18760dde18e5a713bc186062ca1ebc9967500b07faeeAnders CarlssonDecl *
18779106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas GregorTemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
18789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor                                                  TemplateTemplateParmDecl *D) {
18799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Instantiate the template parameter list of the template template parameter.
18809106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *TempParams = D->getTemplateParameters();
18819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  TemplateParameterList *InstParams;
18826964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  SmallVector<TemplateParameterList*, 8> ExpandedParams;
18836964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
18846964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  bool IsExpandedParameterPack = false;
18856964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
18866964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (D->isExpandedParameterPack()) {
18876964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack is an already-expanded pack
18886964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // expansion of template parameters. Substitute into each of the expanded
18896964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // parameters.
18906964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
18916964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
18926964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith         I != N; ++I) {
18936964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
18946964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      TemplateParameterList *Expansion =
18956964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        SubstTemplateParams(D->getExpansionTemplateParameters(I));
18966964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!Expansion)
18976964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
18986964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      ExpandedParams.push_back(Expansion);
18996964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19006964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19016964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    IsExpandedParameterPack = true;
19026964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    InstParams = TempParams;
19036964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else if (D->isPackExpansion()) {
19046964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // The template template parameter pack expands to a pack of template
19056964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // template parameters. Determine whether we need to expand this parameter
19066964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // pack into separate parameters.
19076964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    SmallVector<UnexpandedParameterPack, 2> Unexpanded;
19086964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
19096964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                    Unexpanded);
19106964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19116964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // Determine whether the set of unexpanded parameter packs can and should
19126964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    // be expanded.
19136964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool Expand = true;
19146964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    bool RetainExpansion = false;
1915dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumExpansions;
19166964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
19176964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TempParams->getSourceRange(),
19186964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Unexpanded,
19196964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                TemplateArgs,
19206964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                Expand, RetainExpansion,
19216964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                                NumExpansions))
19226964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
19236964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19246964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    if (Expand) {
19256964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      for (unsigned I = 0; I != *NumExpansions; ++I) {
19266964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
19276964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        LocalInstantiationScope Scope(SemaRef);
19286964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
19296964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        if (!Expansion)
19306964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith          return 0;
19316964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        ExpandedParams.push_back(Expansion);
19326964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      }
19336964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19346964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // Note that we have an expanded parameter pack. The "type" of this
19356964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // expanded parameter pack is the original expansion type, but callers
19366964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // will end up using the expanded parameter pack types for type-checking.
19376964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      IsExpandedParameterPack = true;
19386964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = TempParams;
19396964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    } else {
19406964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // We cannot fully expand the pack expansion now, so just substitute
19416964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      // into the pattern.
19426964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
19436964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith
19446964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      LocalInstantiationScope Scope(SemaRef);
19456964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      InstParams = SubstTemplateParams(TempParams);
19466964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      if (!InstParams)
19476964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith        return 0;
19486964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    }
19496964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  } else {
19509106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // Perform the actual substitution of template parameters within a new,
19519106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    // local instantiation scope.
19522a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    LocalInstantiationScope Scope(SemaRef);
19539106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    InstParams = SubstTemplateParams(TempParams);
19549106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor    if (!InstParams)
19556964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith      return 0;
1956a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  }
1957a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19589106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // Build the template template parameter.
19596964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  TemplateTemplateParmDecl *Param;
19606964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  if (IsExpandedParameterPack)
19616964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
19626964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
19636964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                   D->getDepth() - TemplateArgs.getNumLevels(),
19646964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
19656964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams,
19666964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             ExpandedParams);
19676964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith  else
19686964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith    Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
19696964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getLocation(),
1970a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                   D->getDepth() - TemplateArgs.getNumLevels(),
19716964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getPosition(),
19726964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->isParameterPack(),
19736964b3f80ce1ba489e7e25e7cd58062699af9b0cRichard Smith                                             D->getIdentifier(), InstParams);
1974d92f7a297c0ed3f7d0ebcbb557e1d4c1925b8c72Abramo Bagnara  Param->setDefaultArgument(D->getDefaultArgument(), false);
19759a299e0575ce235f491014627c7267e2d2cd73deDouglas Gregor  Param->setAccess(AS_public);
1976a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
1977a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // Introduce this template parameter's instantiation into the instantiation
19789106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  // scope.
19799106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1980a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
19819106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor  return Param;
19829106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor}
19839106ef78f8fcc9df1a60c7a5b64c7d967194207eDouglas Gregor
198448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas GregorDecl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1985db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // Using directives are never dependent (and never contain any types or
1986db9924191092b4d426cc066637d81698211846aaDouglas Gregor  // expressions), so they require no explicit instantiation work.
1987a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
198848c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  UsingDirectiveDecl *Inst
198948c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor    = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1990a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNamespaceKeyLocation(),
1991db9924191092b4d426cc066637d81698211846aaDouglas Gregor                                 D->getQualifierLoc(),
1992a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getIdentLocation(),
1993a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                 D->getNominatedNamespace(),
199448c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor                                 D->getCommonAncestor());
1995536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
1996536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // Add the using directive to its declaration context
1997536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  // only if this is not a function or method.
1998536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara  if (!Owner->isFunctionOrMethod())
1999536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara    Owner->addDecl(Inst);
2000536afbeb5609db87d98da8402ed0fd58f61f5d3aAbramo Bagnara
200148c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor  return Inst;
200248c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor}
200348c32a7e7c977f317dc1dc19524c2f54d29c7270Douglas Gregor
2004ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
20051b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20061b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The nested name specifier may be dependent, for example
20071b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template <typename T> struct t {
20081b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s1 { T f1(); };
20091b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //       struct s2 : s1 { using s1::f1; };
20101b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     };
20111b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  //     template struct t<int>;
20121b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // Here, in using s1::f1, s1 refers to t<T>::s1;
20131b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // we need to substitute for t<int>::s1.
20145149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
20155149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
20165149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
20175149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
2018dc355713be51fcb4ee52d9fd6b4548ceff47fadfDouglas Gregor    return 0;
20191b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor
20201b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // The name info is non-dependent, so no transformation
20211b398205267ea69f35230eea50e0225db22ebb7eDouglas Gregor  // is required.
2022ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo = D->getNameInfo();
2023ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20249f54ad4381370c6b771424b53d219e661d6d6706John McCall  // We only need to do redeclaration lookups if we're in a class
20259f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scope (in fact, it's not really even possible in non-class
20269f54ad4381370c6b771424b53d219e661d6d6706John McCall  // scopes).
20279f54ad4381370c6b771424b53d219e661d6d6706John McCall  bool CheckRedeclaration = Owner->isRecord();
20289f54ad4381370c6b771424b53d219e661d6d6706John McCall
2029ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2030ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                    Sema::ForRedeclaration);
20319f54ad4381370c6b771424b53d219e661d6d6706John McCall
2032ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
2033ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->getUsingLocation(),
20345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                       QualifierLoc,
2035ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                       NameInfo,
2036ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                       D->isTypeName());
2037ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20385149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  CXXScopeSpec SS;
20395149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
20409f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (CheckRedeclaration) {
20419f54ad4381370c6b771424b53d219e661d6d6706John McCall    Prev.setHideTags(false);
20429f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.LookupQualifiedName(Prev, Owner);
20439f54ad4381370c6b771424b53d219e661d6d6706John McCall
20449f54ad4381370c6b771424b53d219e661d6d6706John McCall    // Check for invalid redeclarations.
20459f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
20469f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->isTypeName(), SS,
20479f54ad4381370c6b771424b53d219e661d6d6706John McCall                                            D->getLocation(), Prev))
20489f54ad4381370c6b771424b53d219e661d6d6706John McCall      NewUD->setInvalidDecl();
20499f54ad4381370c6b771424b53d219e661d6d6706John McCall
20509f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
20519f54ad4381370c6b771424b53d219e661d6d6706John McCall
20529f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (!NewUD->isInvalidDecl() &&
20539f54ad4381370c6b771424b53d219e661d6d6706John McCall      SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
2054ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                      D->getLocation()))
2055ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    NewUD->setInvalidDecl();
20569f54ad4381370c6b771424b53d219e661d6d6706John McCall
2057ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2058ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  NewUD->setAccess(D->getAccess());
2059ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Owner->addDecl(NewUD);
2060ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
20619f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Don't process the shadow decls for an invalid decl.
20629f54ad4381370c6b771424b53d219e661d6d6706John McCall  if (NewUD->isInvalidDecl())
20639f54ad4381370c6b771424b53d219e661d6d6706John McCall    return NewUD;
20649f54ad4381370c6b771424b53d219e661d6d6706John McCall
2065c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2066c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD))
2067c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith      NewUD->setInvalidDecl();
2068c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith    return NewUD;
2069c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith  }
2070c5a89a1cc2f168ad0a115c560b8de5f1c952d8c5Richard Smith
2071323c310efa0abd7a786b0303501186b5f33eb8d7John McCall  bool isFunctionScope = Owner->isFunctionOrMethod();
2072323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
20739f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Process the shadow decls.
20749f54ad4381370c6b771424b53d219e661d6d6706John McCall  for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
20759f54ad4381370c6b771424b53d219e661d6d6706John McCall         I != E; ++I) {
20769f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *Shadow = *I;
20779f54ad4381370c6b771424b53d219e661d6d6706John McCall    NamedDecl *InstTarget =
2078b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2079b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                          Shadow->getLocation(),
2080b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                        Shadow->getTargetDecl(),
2081b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor                                                           TemplateArgs));
2082b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor    if (!InstTarget)
2083b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      return 0;
20849f54ad4381370c6b771424b53d219e661d6d6706John McCall
20859f54ad4381370c6b771424b53d219e661d6d6706John McCall    if (CheckRedeclaration &&
20869f54ad4381370c6b771424b53d219e661d6d6706John McCall        SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
20879f54ad4381370c6b771424b53d219e661d6d6706John McCall      continue;
20889f54ad4381370c6b771424b53d219e661d6d6706John McCall
20899f54ad4381370c6b771424b53d219e661d6d6706John McCall    UsingShadowDecl *InstShadow
20909f54ad4381370c6b771424b53d219e661d6d6706John McCall      = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
20919f54ad4381370c6b771424b53d219e661d6d6706John McCall    SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2092323c310efa0abd7a786b0303501186b5f33eb8d7John McCall
2093323c310efa0abd7a786b0303501186b5f33eb8d7John McCall    if (isFunctionScope)
2094323c310efa0abd7a786b0303501186b5f33eb8d7John McCall      SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
20959f54ad4381370c6b771424b53d219e661d6d6706John McCall  }
2096ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2097ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return NewUD;
2098ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2099ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
2100ed97649e9574b9d854fa4d6109c9333ae0993554John McCallDecl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
21019f54ad4381370c6b771424b53d219e661d6d6706John McCall  // Ignore these;  we handle them in bulk when processing the UsingDecl.
21029f54ad4381370c6b771424b53d219e661d6d6706John McCall  return 0;
2103ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
2104ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
21075149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
2108a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
21095149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor                                          TemplateArgs);
21105149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21117ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    return 0;
21127ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21137ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  CXXScopeSpec SS;
21145149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21157ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
2116ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  // Since NameInfo refers to a typename, it cannot be a C++ special name.
2117accaf19bc1129c0273ec50dba52318e60bc29103Benjamin Kramer  // Hence, no transformation is required for it.
2118ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
21197ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  NamedDecl *UD =
21207ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2121ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
21227ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
21237ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ true, D->getTypenameLoc());
21244469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2125ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2126ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21277ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  return UD;
21287ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
21297ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
21307ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallDecl * TemplateDeclInstantiator
21317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
21325149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  NestedNameSpecifierLoc QualifierLoc
21335149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor      = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
21345149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  if (!QualifierLoc)
21350dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson    return 0;
2136a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
21370dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson  CXXScopeSpec SS;
21385149f37cfc736d03233bf92b5ba7c6e866c6647bDouglas Gregor  SS.Adopt(QualifierLoc);
21391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2140ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara  DeclarationNameInfo NameInfo
2141ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara    = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2142ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara
21431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  NamedDecl *UD =
21449488ea120e093068021f944176c3d610dd540914John McCall    SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
2145ef3dce817d43faadbf21ce9102d33a9d84b02e09Abramo Bagnara                                  D->getUsingLoc(), SS, NameInfo, 0,
21467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*instantiation*/ true,
21477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                                  /*typename*/ false, SourceLocation());
21484469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  if (UD)
2149ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2150ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
21510d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return UD;
21520dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson}
21530dde18e5a713bc186062ca1ebc9967500b07faeeAnders Carlsson
2154af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2155af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetDecl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2156af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                     ClassScopeFunctionSpecializationDecl *Decl) {
2157af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  CXXMethodDecl *OldFD = Decl->getSpecialization();
21586b02009359a462ffe633696a4441313b462e6566Nico Weber  CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD,
21596b02009359a462ffe633696a4441313b462e6566Nico Weber                                                                0, true));
2160af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2161af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2162af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                        Sema::ForRedeclaration);
2163af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
21646b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo TemplateArgs;
21656b02009359a462ffe633696a4441313b462e6566Nico Weber  TemplateArgumentListInfo* TemplateArgsPtr = 0;
21666b02009359a462ffe633696a4441313b462e6566Nico Weber  if (Decl->hasExplicitTemplateArgs()) {
21676b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgs = Decl->templateArgs();
21686b02009359a462ffe633696a4441313b462e6566Nico Weber    TemplateArgsPtr = &TemplateArgs;
21696b02009359a462ffe633696a4441313b462e6566Nico Weber  }
21706b02009359a462ffe633696a4441313b462e6566Nico Weber
2171af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
21726b02009359a462ffe633696a4441313b462e6566Nico Weber  if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
21736b02009359a462ffe633696a4441313b462e6566Nico Weber                                                  Previous)) {
2174af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    NewFD->setInvalidDecl();
2175af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return NewFD;
2176af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  }
2177af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2178af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Associate the specialization with the pattern.
2179af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2180af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Specialization && "Class scope Specialization is null");
2181af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2182af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2183af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return NewFD;
2184af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
2185af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
2186ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallDecl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2187d6350aefb1396b299e199c7f1fe51bb40c12e75eDouglas Gregor                      const MultiLevelTemplateArgumentList &TemplateArgs) {
21887e06390f8a60440d6fc5f0e633acdc2edd8ee924Douglas Gregor  TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
21892fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor  if (D->isInvalidDecl())
21902fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor    return 0;
21912fa98001f832836e3f652c211a9d2f80501d659aDouglas Gregor
21928dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor  return Instantiator.Visit(D);
21938dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor}
21948dbc2694424b4e842b1d5ea39744a137b58600c3Douglas Gregor
2195e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \brief Instantiates a nested template parameter list in the current
2196e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// instantiation context.
2197e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2198e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \param L The parameter list to instantiate
2199e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall///
2200e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall/// \returns NULL if there was an error
2201e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCallTemplateParameterList *
2202ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCallTemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2203e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Get errors for all the parameters before bailing out.
2204e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  bool Invalid = false;
2205e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2206e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  unsigned N = L->size();
22075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  typedef SmallVector<NamedDecl *, 8> ParamVector;
2208e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  ParamVector Params;
2209e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  Params.reserve(N);
2210e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2211e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall       PI != PE; ++PI) {
2212bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2213e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    Params.push_back(D);
22149148c3f5829f4d031249faeb1043e7be914539e8Douglas Gregor    Invalid = Invalid || !D || D->isInvalidDecl();
2215e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  }
2216e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2217e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  // Clean up if we had an error.
2218ff331c15729f7d4439d253c97f4d60f2a7ffd0c6Douglas Gregor  if (Invalid)
2219e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    return NULL;
2220e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2221e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  TemplateParameterList *InstL
2222e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall    = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2223e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getLAngleLoc(), &Params.front(), N,
2224e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall                                    L->getRAngleLoc());
2225e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall  return InstL;
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
2227e29ba20148e9b7835ad463b39cd4ee9223eafbbfJohn McCall
2228a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \brief Instantiate the declaration of a class template partial
2229ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization.
2230ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2231ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// \param ClassTemplate the (instantiated) class template that is partially
2232ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor// specialized by the instantiation of \p PartialSpec.
2233ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2234a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi/// \param PartialSpec the (uninstantiated) class template partial
2235ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor/// specialization that we are instantiating.
2236ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor///
2237d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// \returns The instantiated partial specialization, if successful; otherwise,
2238d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor/// NULL to indicate an error.
2239d65587f7a6d38965fa37158d3f57990a7faf3836Douglas GregorClassTemplatePartialSpecializationDecl *
2240ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorTemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2241ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                            ClassTemplateDecl *ClassTemplate,
2242ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                          ClassTemplatePartialSpecializationDecl *PartialSpec) {
2243550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // Create a local instantiation scope for this class template partial
2244550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // specialization, which will contain the instantiations of the template
2245550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  // parameters.
22462a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope Scope(SemaRef);
2247a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2248ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template parameters of the class template partial
2249ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2250ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2251ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2252ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (!InstParams)
2253d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2254a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2255ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Substitute into the template arguments of the class template partial
2256ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization.
2257d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstTemplateArgs; // no angle locations
2258a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(),
2259a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                    PartialSpec->getNumTemplateArgsAsWritten(),
2260e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                    InstTemplateArgs, TemplateArgs))
2261e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return 0;
2262a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Check that the template argument list is well-formed for this
2264ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // class template.
22655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> Converted;
2266a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2267ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        PartialSpec->getLocation(),
2268a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                        InstTemplateArgs,
2269ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        false,
2270ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                        Converted))
2271d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2272ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2273ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Figure out where to insert this class template partial specialization
2274ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // in the member template's set of class template partial specializations.
2275ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  void *InsertPos = 0;
2276ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplateSpecializationDecl *PrevDecl
2277910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = ClassTemplate->findPartialSpecialization(Converted.data(),
2278910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                               Converted.size(), InsertPos);
2279a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2280ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the canonical type that describes the converted template
2281ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // arguments of the class template partial specialization.
2282a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  QualType CanonType
2283ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2284910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.data(),
2285910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                    Converted.size());
2286ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
2287ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Build the fully-sugared type for this class template
2288ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specialization as the user wrote in the specialization
2289ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // itself. This means that we'll pretty-print the type retrieved
2290ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // from the specialization's declaration the way that the user
2291ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // actually wrote the specialization, rather than formatting the
2292ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // name based on the "canonical" representation used to store the
2293ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // template arguments in the specialization.
22943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *WrittenTy
22953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = SemaRef.Context.getTemplateSpecializationTypeInfo(
22963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    TemplateName(ClassTemplate),
22973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                    PartialSpec->getLocation(),
2298d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                    InstTemplateArgs,
2299ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                    CanonType);
2300a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2301ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (PrevDecl) {
2302ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // We've already seen a partial specialization with the same template
2303ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // parameters and template arguments. This can happen, for example, when
2304ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // substituting the outer template arguments ends up causing two
2305ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // class template partial specializations of a member class template
2306ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    // to have identical forms, e.g.,
2307ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2308ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   template<typename T, typename U>
2309ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   struct Outer {
2310ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename X, typename Y> struct Inner;
2311ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<T, Y>;
2312ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //     template<typename Y> struct Inner<U, Y>;
2313ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   };
2314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //
2315ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //   Outer<int, int> outer; // error: the partial specializations of Inner
2316ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    //                          // have the same signature.
2317ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2318d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor      << WrittenTy->getType();
2319ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2320ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      << SemaRef.Context.getTypeDeclType(PrevDecl);
2321d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor    return 0;
2322ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2323a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2324a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2325ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Create the class template partial specialization declaration.
2326ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  ClassTemplatePartialSpecializationDecl *InstPartialSpec
2327a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
232813c8577201e4fc0ddac5f09d05fd1778832137d1Douglas Gregor                                                     PartialSpec->getTagKind(),
2329a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     Owner,
2330ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocStart(),
2331ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                                                     PartialSpec->getLocation(),
2332ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                     InstParams,
2333a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                                     ClassTemplate,
2334910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.data(),
2335910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                     Converted.size(),
2336d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                     InstTemplateArgs,
23373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                                     CanonType,
2338dc60c1eb4acbde6edcec9760de92f9098593d915Douglas Gregor                                                     0,
2339cc0b1bc979b650a8a8b34b2032a074fd7724a90dArgyrios Kyrtzidis                             ClassTemplate->getNextPartialSpecSequenceNumber());
2340b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  // Substitute the nested name specifier, if any.
2341b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall  if (SubstQualifier(PartialSpec, InstPartialSpec))
2342b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall    return 0;
2343b6217665c6a987f2d6c8665fd70365d7719ac4dfJohn McCall
2344ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  InstPartialSpec->setInstantiatedFromMember(PartialSpec);
23454469e8a97cdca3725b4f8f366916143113c029acDouglas Gregor  InstPartialSpec->setTypeAsWritten(WrittenTy);
2346a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2347ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // Add this partial specialization to the set of class template partial
2348ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  // specializations.
23491e1e9722cb4ea6027e2c4885c7a8f26d3726ca7dDouglas Gregor  ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0);
2350d65587f7a6d38965fa37158d3f57990a7faf3836Douglas Gregor  return InstPartialSpec;
2351ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
2352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
235321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTypeSourceInfo*
235421ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCallTemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
23555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                              SmallVectorImpl<ParmVarDecl *> &Params) {
235621ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
235721ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(OldTInfo && "substituting function without type source info");
235821ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  assert(Params.empty() && "parameter vector is non-empty at start");
2359cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
2360cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  CXXRecordDecl *ThisContext = 0;
2361cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  unsigned ThisTypeQuals = 0;
2362cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2363cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisContext = Method->getParent();
2364cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    ThisTypeQuals = Method->getTypeQualifiers();
2365cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  }
2366cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor
23676cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall  TypeSourceInfo *NewTInfo
23686cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall    = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
23696cd3b9fb8a29bb70fff01719bdde238723d67c10John McCall                                    D->getTypeSpecStartLoc(),
2370cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    D->getDeclName(),
2371cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor                                    ThisContext, ThisTypeQuals);
237221ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  if (!NewTInfo)
237321ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall    return 0;
23745545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2375cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  if (NewTInfo != OldTInfo) {
2376cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // Get parameters from the new type info.
2377140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
237839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    if (FunctionProtoTypeLoc OldProtoLoc =
237939e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            OldTL.getAs<FunctionProtoTypeLoc>()) {
2380140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara      TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
238139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2382500d729e85028944355a119f9823ac99fa5ddcabRichard Smith      unsigned NewIdx = 0;
238339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs();
238412c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor           OldIdx != NumOldParams; ++OldIdx) {
238539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx);
2386500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2387500d729e85028944355a119f9823ac99fa5ddcabRichard Smith
2388dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie        Optional<unsigned> NumArgumentsInExpansion;
2389500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (OldParam->isParameterPack())
2390500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          NumArgumentsInExpansion =
2391500d729e85028944355a119f9823ac99fa5ddcabRichard Smith              SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
2392500d729e85028944355a119f9823ac99fa5ddcabRichard Smith                                                 TemplateArgs);
2393500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        if (!NumArgumentsInExpansion) {
2394a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi          // Simple case: normal parameter, or a parameter pack that's
239512c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          // instantiated to a (still-dependent) parameter pack.
239639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie          ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
239712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor          Params.push_back(NewParam);
2398500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->InstantiatedLocal(OldParam, NewParam);
2399500d729e85028944355a119f9823ac99fa5ddcabRichard Smith        } else {
2400500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          // Parameter pack expansion: make the instantiation an argument pack.
2401500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          Scope->MakeInstantiatedLocalArgPack(OldParam);
2402500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
240339e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++);
2404500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Params.push_back(NewParam);
2405500d729e85028944355a119f9823ac99fa5ddcabRichard Smith            Scope->InstantiatedLocalPackArg(OldParam, NewParam);
2406500d729e85028944355a119f9823ac99fa5ddcabRichard Smith          }
240712c9c00024a01819e3a70ef6d951d32efaeb9312Douglas Gregor        }
24086920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2409895162da2d52f4243f61081d7436de66af4503fcDouglas Gregor    }
2410cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  } else {
2411cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // The function type itself was not dependent and therefore no
2412cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // substitution occurred. However, we still need to instantiate
2413cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    // the function parameters themselves.
2414140a2bd77539b4537010d8cd6a0a3805ce724b3eAbramo Bagnara    TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
241539e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie    if (FunctionProtoTypeLoc OldProtoLoc =
241639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie            OldTL.getAs<FunctionProtoTypeLoc>()) {
241739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) {
241839e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie        ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i));
24196920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        if (!Parm)
24206920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor          return 0;
24216920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor        Params.push_back(Parm);
24226920cdce298ac9ba50dc7ebb7dea982a300b0664Douglas Gregor      }
2423cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor    }
2424cb27b0f70d2017295776afafe3616e0bcd74ab51Douglas Gregor  }
242521ef0fa27b0783ec0bc6aa5b524feb2ec840f952John McCall  return NewTInfo;
24265545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
24275545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
2428e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// Introduce the instantiated function parameters into the local
2429e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// instantiation scope, and set the parameter names to those used
2430e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith/// in the template.
2431e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
2432e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             const FunctionDecl *PatternDecl,
2433e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                             LocalInstantiationScope &Scope,
2434e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
2435e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned FParamIdx = 0;
2436e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2437e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2438e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (!PatternParam->isParameterPack()) {
2439e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Simple case: not a parameter pack.
2440e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(FParamIdx < Function->getNumParams());
2441e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2442e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2443e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocal(PatternParam, FunctionParam);
2444e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2445e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2446e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2447e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2448e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // Expand the parameter pack.
2449e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Scope.MakeInstantiatedLocalArgPack(PatternParam);
2450dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie    Optional<unsigned> NumArgumentsInExpansion
2451e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
2452500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    assert(NumArgumentsInExpansion &&
2453500d729e85028944355a119f9823ac99fa5ddcabRichard Smith           "should only be called when all template arguments are known");
2454500d729e85028944355a119f9823ac99fa5ddcabRichard Smith    for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
2455e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
2456e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      FunctionParam->setDeclName(PatternParam->getDeclName());
2457e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
2458e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ++FParamIdx;
2459e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2460e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2461e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2462e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2463e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithstatic void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New,
2464e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     const FunctionProtoType *Proto,
2465e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                           const MultiLevelTemplateArgumentList &TemplateArgs) {
246613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  assert(Proto->getExceptionSpecType() != EST_Uninstantiated);
246713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2468e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // C++11 [expr.prim.general]p3:
2469e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   If a declaration declares a member function or member function
2470e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   template of a class X, the expression this is a prvalue of type
2471e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2472e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   and the end of the function-definition, member-declarator, or
2473e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  //   declarator.
2474e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  CXXRecordDecl *ThisContext = 0;
2475e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  unsigned ThisTypeQuals = 0;
2476e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) {
2477e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisContext = Method->getParent();
2478e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ThisTypeQuals = Method->getTypeQualifiers();
2479e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2480e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals,
248180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith                                   SemaRef.getLangOpts().CPlusPlus11);
2482e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2483e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // The function has an exception specification or a "noreturn"
2484e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // attribute. Substitute into each of the exception types.
2485e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  SmallVector<QualType, 4> Exceptions;
2486e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
2487e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // FIXME: Poor location information!
2488e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (const PackExpansionType *PackExpansion
2489e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          = Proto->getExceptionType(I)->getAs<PackExpansionType>()) {
2490e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // We have a pack expansion. Instantiate it.
2491e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2492e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(),
2493e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                              Unexpanded);
2494e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(!Unexpanded.empty() &&
2495e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith             "Pack expansion without parameter packs?");
2496e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2497e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Expand = false;
2498e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool RetainExpansion = false;
2499dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions
2500e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                        = PackExpansion->getNumExpansions();
2501e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(),
2502e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  SourceRange(),
2503e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Unexpanded,
2504e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  TemplateArgs,
2505e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  Expand,
2506e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  RetainExpansion,
2507e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                  NumExpansions))
2508e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2509e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2510e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!Expand) {
2511e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // We can't expand this pack expansion into separate arguments yet;
2512e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // just substitute into the pattern and create a new pack expansion
2513e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        // type.
2514e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2515e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2516e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2517e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2518e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull())
2519e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2520e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2521e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        T = SemaRef.Context.getPackExpansionType(T, NumExpansions);
2522e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2523e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        continue;
2524e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2525e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2526e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Substitute into the pack expansion pattern for each template
2527e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      bool Invalid = false;
2528e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) {
2529e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx);
2530e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2531e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        QualType T = SemaRef.SubstType(PackExpansion->getPattern(),
2532e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                       TemplateArgs,
2533e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                     New->getLocation(), New->getDeclName());
2534e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        if (T.isNull()) {
2535e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          Invalid = true;
2536e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          break;
2537e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        }
2538e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2539e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        Exceptions.push_back(T);
2540e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      }
2541e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2542e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (Invalid)
2543e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        break;
2544e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2545e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2546e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2547e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2548e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    QualType T
2549e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
2550e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                          New->getLocation(), New->getDeclName());
2551e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (T.isNull() ||
2552e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
2553e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      continue;
2554e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2555e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    Exceptions.push_back(T);
2556e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2557e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Expr *NoexceptExpr = 0;
2558e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) {
2559e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    EnterExpressionEvaluationContext Unevaluated(SemaRef,
2560e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                 Sema::ConstantEvaluated);
2561e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs);
2562e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable())
2563e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart());
2564e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2565e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    if (E.isUsable()) {
2566e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      NoexceptExpr = E.take();
2567e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      if (!NoexceptExpr->isTypeDependent() &&
2568e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith          !NoexceptExpr->isValueDependent())
2569ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor        NoexceptExpr
2570ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor          = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr,
2571ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              0, diag::err_noexcept_needs_constant_expression,
2572ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor              /*AllowFold*/ false).take();
2573e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
2574e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  }
2575e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2576e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Rebuild the function type
2577e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionProtoType *NewProto
2578e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    = New->getType()->getAs<FunctionProtoType>();
2579e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  assert(NewProto && "Template instantiation without function prototype?");
2580e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2581e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
2582e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.ExceptionSpecType = Proto->getExceptionSpecType();
2583e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NumExceptions = Exceptions.size();
2584e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.Exceptions = Exceptions.data();
2585e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  EPI.NoexceptExpr = NoexceptExpr;
2586e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2587e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2588bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(NewProto->arg_type_begin(),
2589bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     NewProto->getNumArgs()),
2590e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                               EPI));
2591e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2592e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2593e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smithvoid Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
2594e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                    FunctionDecl *Decl) {
259513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
259613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  if (Proto->getExceptionSpecType() != EST_Uninstantiated)
2597e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2598e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2599e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
2600e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                             InstantiatingTemplate::ExceptionSpecification());
2601b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (Inst) {
2602b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // We hit the instantiation depth limit. Clear the exception specification
2603b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    // so that our callers don't have to cope with EST_Uninstantiated.
2604b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2605b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    EPI.ExceptionSpecType = EST_None;
2606b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Decl->setType(Context.getFunctionType(Proto->getResultType(),
2607bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                    ArrayRef<QualType>(Proto->arg_type_begin(),
2608bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                       Proto->getNumArgs()),
2609b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith                                          EPI));
2610e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    return;
2611b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  }
2612e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2613e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // Enter the scope of this instantiation. We don't use
2614e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  // PushDeclContext because we don't have a scope.
2615e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  Sema::ContextRAII savedContext(*this, Decl);
2616e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  LocalInstantiationScope Scope(*this);
2617e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
2618e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  MultiLevelTemplateArgumentList TemplateArgs =
2619e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true);
2620e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
262113bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  FunctionDecl *Template = Proto->getExceptionSpecTemplate();
262213bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs);
2623e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
262413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith  ::InstantiateExceptionSpec(*this, Decl,
262513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             Template->getType()->castAs<FunctionProtoType>(),
262613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith                             TemplateArgs);
2627e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith}
2628e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith
26291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Initializes the common fields of an instantiation function
2630e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// declaration (New) from the corresponding fields of its template (Tmpl).
2631e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2632e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns true if there was an error
26331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
26341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
2635e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                                                    FunctionDecl *Tmpl) {
263685f485a70fbec54c9b4562dfc4d95188ea6c9b48David Blaikie  if (Tmpl->isDeleted())
263710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    New->setDeletedAsWritten();
26381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2639cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // If we are performing substituting explicitly-specified template arguments
2640cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // or deduced template arguments into a function template and we reach this
2641cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // point, we are now past the point where SFINAE applies and have committed
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to keeping the new function template specialization. We therefore
26431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // convert the active template instantiation for the function template
2644cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // into a template instantiation for this specific function template
2645cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // specialization, which is not a SFINAE context, so that we diagnose any
2646cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  // further errors in the declaration itself.
2647cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  typedef Sema::ActiveTemplateInstantiation ActiveInstType;
2648cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
2649cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
2650cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
26511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (FunctionTemplateDecl *FunTmpl
26524a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky          = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
26531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(FunTmpl->getTemplatedDecl() == Tmpl &&
2654cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor             "Deduction from the wrong function template?");
2655bcbb8bd3326a86aa70b7df386ae3c86c9ad255c5Daniel Dunbar      (void) FunTmpl;
2656cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor      ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
26574a9e60fc7c36e323ae376601cc704fed4beb68aeNick Lewycky      ActiveInst.Entity = New;
2658cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor    }
2659cca9e9674a5e50a283185d8e9d8a5c3414eb008eDouglas Gregor  }
26601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26610ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
26620ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  assert(Proto && "Function template without prototype?");
26630ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
266460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
2665e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
2666e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2667e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // DR1330: In C++11, defer instantiation of a non-trivial
2668e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    // exception specification.
266980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith    if (SemaRef.getLangOpts().CPlusPlus11 &&
2670e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_None &&
2671e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_DynamicNone &&
2672e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        EPI.ExceptionSpecType != EST_BasicNoexcept) {
267313bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      FunctionDecl *ExceptionSpecTemplate = Tmpl;
267413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      if (EPI.ExceptionSpecType == EST_Uninstantiated)
267513bffc532bafd45d4a77867993c1afb83c7661beRichard Smith        ExceptionSpecTemplate = EPI.ExceptionSpecTemplate;
2676b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith      assert(EPI.ExceptionSpecType != EST_Unevaluated &&
2677b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith             "instantiating implicitly-declared special member");
267813bffc532bafd45d4a77867993c1afb83c7661beRichard Smith
2679e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      // Mark the function has having an uninstantiated exception specification.
2680e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      const FunctionProtoType *NewProto
2681e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith        = New->getType()->getAs<FunctionProtoType>();
2682e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      assert(NewProto && "Template instantiation without function prototype?");
2683e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI = NewProto->getExtProtoInfo();
2684e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecType = EST_Uninstantiated;
2685e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      EPI.ExceptionSpecDecl = New;
268613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith      EPI.ExceptionSpecTemplate = ExceptionSpecTemplate;
2687e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
2688bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                  ArrayRef<QualType>(NewProto->arg_type_begin(),
2689bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                     NewProto->getNumArgs()),
2690e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith                                                   EPI));
2691e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    } else {
2692e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith      ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs);
2693e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith    }
26940ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor  }
26950ae7b3f1d5403265f693ed75384603ca8fbba74dDouglas Gregor
269619f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  // Get the definition. Leaves the variable unchanged if undefined.
2697e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  const FunctionDecl *Definition = Tmpl;
269819f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola  Tmpl->isDefined(Definition);
269919f74acdf8842ceece578b7307884f5ba22d7f59Rafael Espindola
270023323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins  SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
270123323e0253716ff03c95a00fb6903019daafe3aaDeLesley Hutchins                           LateAttrs, StartingScope);
27027cf84d66965a7706004d8590b5af5fe54b85f525Douglas Gregor
2703e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  return false;
2704e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2705e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
27065545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \brief Initializes common fields of an instantiated method
27075545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// declaration (New) from the corresponding fields of its template
27085545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// (Tmpl).
27095545e166a956a20d7a6b58408e251a1119025485Douglas Gregor///
27105545e166a956a20d7a6b58408e251a1119025485Douglas Gregor/// \returns true if there was an error
27111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
27121eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
27135545e166a956a20d7a6b58408e251a1119025485Douglas Gregor                                                  CXXMethodDecl *Tmpl) {
2714e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  if (InitFunctionInstantiation(New, Tmpl))
2715e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return true;
27161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27175545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  New->setAccess(Tmpl->getAccess());
2718e7184df728bb339633d88c774b5097dd9318cc8aFariborz Jahanian  if (Tmpl->isVirtualAsWritten())
271985606ebf3dd1b5dd81a59ef25b5ad47627664774Douglas Gregor    New->setVirtualAsWritten(true);
27205545e166a956a20d7a6b58408e251a1119025485Douglas Gregor
27215545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: attributes
27225545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  // FIXME: New needs a pointer to Tmpl
27235545e166a956a20d7a6b58408e251a1119025485Douglas Gregor  return false;
27245545e166a956a20d7a6b58408e251a1119025485Douglas Gregor}
2725a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2726a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given function from its
2727a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2728a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
2729b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
2730b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
2731b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// for the function, but it's close.
2732b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2733a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \param Function the already-instantiated declaration of a
2734b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// function template specialization or member function of a class template
2735b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// specialization.
2736b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor///
2737b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
2738b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor/// are required by this instantiation.
2739e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2740e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2741e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where the body of the function is required. Complain if
2742e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// there is no such body.
2743f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregorvoid Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
2744b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor                                         FunctionDecl *Function,
2745e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool Recursive,
2746e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                         bool DefinitionRequired) {
274710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt  if (Function->isInvalidDecl() || Function->isDefined())
274854dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor    return;
274954dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
2750af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // Never instantiate an explicit specialization except if it is a class scope
2751af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  // explicit specialization.
2752af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2753af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet      !Function->getClassScopeSpecializationPattern())
2754251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
27556cfacfe54c75baa4d67f1fbdf4f80644b662818eDouglas Gregor
27561eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor  // Find the function body that we'll be substituting.
27573b846b6c252972a6f142aa226c1e65aebd0feecaDouglas Gregor  const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
2758f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "instantiating a non-template");
2759f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt
2760f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  Stmt *Pattern = PatternDecl->getBody(PatternDecl);
2761f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
2762f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern) {
2763f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    // Try to find a defaulted definition
2764f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt    PatternDecl->isDefined(PatternDecl);
2765dfab854e6855dad076c0207b29859d452e398437Sean Hunt  }
2766f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  assert(PatternDecl && "template definition is not a template");
27671eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
27688387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Postpone late parsed template instantiations.
2769f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (PatternDecl->isLateTemplateParsed() &&
27708a29bc047a374df2464869b55581c24def68c2ecNick Lewycky      !LateTemplateParser) {
27718387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    PendingInstantiations.push_back(
27728387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      std::make_pair(Function, PointOfInstantiation));
27738387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    return;
27748387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
27758387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
27768387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  // Call the LateTemplateParser callback if there a need to late parse
2777a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  // a templated function definition.
2778f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && PatternDecl->isLateTemplateParsed() &&
27798387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet      LateTemplateParser) {
27804a47e8d35dc1778ef7e428d9edd7676be67e725fFrancois Pichet    LateTemplateParser(OpaqueParser, PatternDecl);
27818387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet    Pattern = PatternDecl->getBody(PatternDecl);
27828387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet  }
27838387e2a41eef6fa17fb140a18c29b6eee9dd2b8aFrancois Pichet
2784f996e051d9953550982b57132daad8a5e3f7bd65Sean Hunt  if (!Pattern && !PatternDecl->isDefaulted()) {
2785e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
2786e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (Function->getPrimaryTemplate())
2787a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2788e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_func_template)
2789e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << Function->getPrimaryTemplate();
2790e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      else
2791a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PointOfInstantiation,
2792e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::err_explicit_instantiation_undefined_member)
2793e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor          << 1 << Function->getDeclName() << Function->getDeclContext();
2794a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2795e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      if (PatternDecl)
2796a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        Diag(PatternDecl->getLocation(),
2797e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor             diag::note_explicit_instantiation_here);
2798cfe833be882f600206f1587f157b025b368497d7Douglas Gregor      Function->setInvalidDecl();
279958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Function->getTemplateSpecializationKind()
280058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
280162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
280258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Function, PointOfInstantiation));
2803e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    }
280458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
28051eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor    return;
2806e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor  }
28071eee0e753fb390b04848846e837714ec774b7bfdDouglas Gregor
2808d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  // C++0x [temp.explicit]p9:
2809d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   Except for inline functions, other explicit instantiation declarations
28101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   have the effect of suppressing the implicit instantiation of the entity
2811d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor  //   to which they refer.
28121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Function->getTemplateSpecializationKind()
2813d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor        == TSK_ExplicitInstantiationDeclaration &&
28147ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor      !PatternDecl->isInlined())
2815d0e3daf2b980b505e535d35b432c938c6d0208efDouglas Gregor    return;
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2817d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith  if (PatternDecl->isInlined())
2818d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith    Function->setImplicitlyInline();
2819d4497dde6fc8f5ce79e0ec37682b8dc920bbbef0Richard Smith
2820f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2821f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor  if (Inst)
2822a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi    return;
2823a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2824e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  // Copy the inner loc start from the pattern.
2825e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara  Function->setInnerLocStart(PatternDecl->getInnerLocStart());
2826e994624c001143ee2b8a7a4715aaad5efcd71f18Abramo Bagnara
2827b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // If we're performing recursive template instantiation, create our own
2828b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
2829b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  // while we're still within our own instantiation context.
28305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
283162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
28322a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  if (Recursive) {
28332a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
283462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
28352a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky  }
28361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2837a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  EnterExpressionEvaluationContext EvalContext(*this,
2838f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                               Sema::PotentiallyEvaluated);
2839e2c31ff0bc622e6fd7d47d7e08b53840f3be6c89Douglas Gregor
284054dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor  // Introduce a new scope where local variable instantiations will be
284160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // recorded, unless we're actually a member function within a local
284260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // class, in which case we need to merge our results with the parent
284360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // scope (of the enclosing function).
284460406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  bool MergeWithParentScope = false;
284560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
284660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    MergeWithParentScope = Rec->isLocalClass();
284760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
284860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  LocalInstantiationScope Scope(*this, MergeWithParentScope);
28491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28501d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  if (PatternDecl->isDefaulted())
28511d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    SetDeclDefaulted(Function, PatternDecl->getLocation());
28521d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  else {
28531d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    ActOnStartOfFunctionDef(0, Function);
28547c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
28551d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // Enter the scope of this instantiation. We don't use
28561d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    // PushDeclContext because we don't have a scope.
28571d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    Sema::ContextRAII savedContext(*this, Function);
28587c5d28b6342229fb648aea59dc063f67ff16bc81Richard Smith
28591d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    MultiLevelTemplateArgumentList TemplateArgs =
28601d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith      getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
286154dabfca850ca9e60e9ffb60003529f868d4d127Douglas Gregor
28621d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
28631d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith                                     TemplateArgs);
28641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2865cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // If this is a constructor, instantiate the member initializers.
2866cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (const CXXConstructorDecl *Ctor =
2867cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt          dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2868cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2869cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                                 TemplateArgs);
2870cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    }
2871cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2872cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    // Instantiate the function body.
2873cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    StmtResult Body = SubstStmt(Pattern, TemplateArgs);
2874cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt
2875cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    if (Body.isInvalid())
2876cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt      Function->setInvalidDecl();
2877a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2878cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt    ActOnFinishFunctionBody(Function, Body.get(),
2879cd10dec673680fd18a2e5a27646173780c059d32Sean Hunt                            /*IsInstantiation=*/true);
2880b9f1b8d877541e76390cd3807c2dcff2f950360aDouglas Gregor
28811d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    PerformDependentDiagnostics(PatternDecl, TemplateArgs);
28820c01d18094100db92d38daa923c95661512db203John McCall
28831d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith    savedContext.pop();
28841d28caf3b5254e60d3e3a1d2d37e5df2e5924111Richard Smith  }
2885aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor
2886aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  DeclGroupRef DG(Function);
2887aba43bb13b3aa3e81990989375fba3a902bfe1c2Douglas Gregor  Consumer.HandleTopLevelDecl(DG);
28881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
288960406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // This class may have local implicit instantiations that need to be
289060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  // instantiation within this scope.
289162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  PerformPendingInstantiations(/*LocalOnly=*/true);
289260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  Scope.Exit();
289360406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
2894b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  if (Recursive) {
28952a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Define any pending vtables.
28962a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    DefineUsedVTables();
28972a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2898b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Instantiate any pending implicit instantiations found during the
28991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
290062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
29011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29022a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    // Restore the set of pending vtables.
29038155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
29048155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded.");
29052a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky    VTableUses.swap(SavedVTableUses);
29062a5f99eb4e2af771faacfceb9f78e230129c5e5aNick Lewycky
2907b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor    // Restore the set of pending implicit instantiations.
29088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
29098155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded.");
291062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
2911b33fe2ff12676bff9db595fdf77e29014d7ba397Douglas Gregor  }
2912a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
2913a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor
2914a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// \brief Instantiate the definition of the given variable from its
2915a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor/// template.
2916a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor///
29177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param PointOfInstantiation the point at which the instantiation was
29187caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// required. Note that this is not precisely a "point of instantiation"
29197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// for the function, but it's close.
29207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
29217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Var the already-instantiated declaration of a static member
29227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// variable of a class template specialization.
29237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor///
29247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// \param Recursive if true, recursively instantiates any functions that
29257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor/// are required by this instantiation.
2926e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor///
2927e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// \param DefinitionRequired if true, then we are performing an explicit
2928e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// instantiation where an out-of-line definition of the member variable
2929e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor/// is required. Complain if there is no such definition.
29307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid Sema::InstantiateStaticDataMemberDefinition(
29317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                          SourceLocation PointOfInstantiation,
29327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor                                                 VarDecl *Var,
2933e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool Recursive,
2934e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor                                                 bool DefinitionRequired) {
29357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var->isInvalidDecl())
29367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
29371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Find the out-of-line definition of this static data member.
29397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
29407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Def && "This data member was not instantiated from a template?");
2941a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  assert(Def->isStaticDataMember() && "Not a static data member?");
29420d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  Def = Def->getOutOfLineDefinition();
29431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29440d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor  if (!Def) {
29457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // We did not find an out-of-line definition of this static data member,
29467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // so we won't perform any instantiation. Rather, we rely on the user to
29471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiate this definition (or provide a specialization for it) in
29481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // another translation unit.
2949e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor    if (DefinitionRequired) {
29500d03514da06dffb39a260a1228ea3fd01d196fa4Douglas Gregor      Def = Var->getInstantiatedFromStaticDataMember();
2951a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      Diag(PointOfInstantiation,
2952e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor           diag::err_explicit_instantiation_undefined_member)
2953e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor        << 2 << Var->getDeclName() << Var->getDeclContext();
2954e2d3a3de71b2fa35614cb732a6da95a41fa38ad9Douglas Gregor      Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
295558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    } else if (Var->getTemplateSpecializationKind()
295658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                 == TSK_ExplicitInstantiationDefinition) {
295762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.push_back(
295858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth        std::make_pair(Var, PointOfInstantiation));
295958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    }
296058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth
29617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
29627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
29637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2964234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
2965234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola
2966251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // Never instantiate an explicit specialization.
2967234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitSpecialization)
2968251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
2969a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
2970251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  // C++0x [temp.explicit]p9:
2971251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   Except for inline functions, other explicit instantiation declarations
2972251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   have the effect of suppressing the implicit instantiation of the entity
2973251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  //   to which they refer.
2974234fe654a3dd2888be42ae5db34db96c5c2c4ba3Rafael Espindola  if (TSK == TSK_ExplicitInstantiationDeclaration)
2975251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    return;
29761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2977afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  // Make sure to pass the instantiated variable to the consumer at the end.
2978afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  struct PassToConsumerRAII {
2979afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ASTConsumer &Consumer;
2980afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    VarDecl *Var;
2981afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
2982afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
2983afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      : Consumer(Consumer), Var(Var) { }
2984afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis
2985afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    ~PassToConsumerRAII() {
2986afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis      Consumer.HandleCXXStaticMemberVarInstantiation(Var);
2987afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    }
2988afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis  } PassToConsumerRAII(Consumer, Var);
2989025039377d7247620750205dbd61ca1ba336f7e0Rafael Espindola
2990f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor  // If we already have a definition, we're done.
299195e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  if (VarDecl *Def = Var->getDefinition()) {
299295e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // We may be explicitly instantiating something we've already implicitly
299395e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    // instantiated.
299495e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky    Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
299595e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky                                       PointOfInstantiation);
2996f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    return;
299795e3872918557b55b62121a7df5f1ee76d45881aNick Lewycky  }
2998f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
29997caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
30007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Inst)
30017caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return;
30021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // If we're performing recursive template instantiation, create our own
30047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // queue of pending implicit instantiations that we will instantiate later,
30057caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // while we're still within our own instantiation context.
30065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<VTableUse, 16> SavedVTableUses;
300762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  std::deque<PendingImplicitInstantiation> SavedPendingInstantiations;
30088155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  if (Recursive) {
30098155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
301062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
30118155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky  }
30121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // Enter the scope of this instantiation. We don't use
30147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  // PushDeclContext because we don't have a scope.
3015f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  ContextRAII previousContext(*this, Var->getDeclContext());
30167bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  LocalInstantiationScope Local(*this);
30177bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
30181028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor  VarDecl *OldVar = Var;
3019ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall  Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
30206bb4dcb412d53d05a80017df81d41e447e2aa3eaNico Weber                                        getTemplateInstantiationArgs(Var)));
3021f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall
3022f5ba7e089daadcd60b0f6e31d932be8bb6045281John McCall  previousContext.pop();
30237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
30247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Var) {
3025afda905e60151f0bc34c187d51a798b4265f69afArgyrios Kyrtzidis    PassToConsumerRAII.Var = Var;
3026583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
3027583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    assert(MSInfo && "Missing member specialization information?");
3028583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor    Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
3029583f33b8a9227bace1a77a15404b4c64dc619d69Douglas Gregor                                       MSInfo->getPointOfInstantiation());
30307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  }
30317bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor  Local.Exit();
30327bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor
30337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Recursive) {
30348155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Define any newly required vtables.
30358155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    DefineUsedVTables();
30368155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
30377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate any pending implicit instantiations found during the
30381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // instantiation of this template.
303962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PerformPendingInstantiations();
30401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30418155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    // Restore the set of pending vtables.
30428155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(VTableUses.empty() &&
30438155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "VTableUses should be empty before it is discarded, "
30448155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
30458155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    VTableUses.swap(SavedVTableUses);
30468155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky
30477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Restore the set of pending implicit instantiations.
30488155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky    assert(PendingInstantiations.empty() &&
30498155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "PendingInstantiations should be empty before it is discarded, "
30508155910a192dafa423d6b932b7d127d48e4641e8Nick Lewycky           "while instantiating static data member.");
305162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    PendingInstantiations.swap(SavedPendingInstantiations);
30521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3053a58861f6490780baec50689e06ca65f7438b85dcDouglas Gregor}
3054815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3055090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlssonvoid
3056090253155017b7eec031bbd7bf07824a448e1d7aAnders CarlssonSema::InstantiateMemInitializers(CXXConstructorDecl *New,
3057090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                                 const CXXConstructorDecl *Tmpl,
3058090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                           const MultiLevelTemplateArgumentList &TemplateArgs) {
30591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306090ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu  SmallVector<CXXCtorInitializer*, 4> NewInits;
306154b3ba8cf2eb4886a88cdb8adedb15f43333ff1dRichard Smith  bool AnyErrors = Tmpl->isInvalidDecl();
3062a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3063090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Instantiate all the initializers.
3064090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
306572f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor                                            InitsEnd = Tmpl->init_end();
306672f6d678c8de9f3a770e8ae5fc4979abf3940668Douglas Gregor       Inits != InitsEnd; ++Inits) {
3067cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    CXXCtorInitializer *Init = *Inits;
3068090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
3069030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // Only instantiate written initializers, let Sema re-construct implicit
3070030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    // ones.
3071030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth    if (!Init->isWritten())
3072030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth      continue;
3073030ef472f31709e175895853fcb43d61d09022c7Chandler Carruth
30743fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    SourceLocation EllipsisLoc;
3075a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
30763fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    if (Init->isPackExpansion()) {
30773fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      // This is a pack expansion. We should expand it now.
307876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
30795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<UnexpandedParameterPack, 2> Unexpanded;
30803fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      collectUnexpandedParameterPacks(BaseTL, Unexpanded);
30813fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      bool ShouldExpand = false;
3082d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      bool RetainExpansion = false;
3083dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie      Optional<unsigned> NumExpansions;
3084a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
30853fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          BaseTL.getSourceRange(),
3086a71f9d0a5e1f8cafdd23a17e292de22fdc8e99ffDavid Blaikie                                          Unexpanded,
3087a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                          TemplateArgs, ShouldExpand,
3088d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                          RetainExpansion,
30893fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                          NumExpansions)) {
30903fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        AnyErrors = true;
30913fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        New->setInvalidDecl();
30923fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        continue;
30933fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
30943fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      assert(ShouldExpand && "Partial instantiation of base initializer?");
3095a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3096a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi      // Loop over all of the arguments in the argument pack(s),
3097cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor      for (unsigned I = 0; I != *NumExpansions; ++I) {
30983fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
30993fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31003fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the initializer.
31015b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
31025b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                               /*CXXDirectInit=*/true);
31035b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        if (TempInit.isInvalid()) {
31043fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31053fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31063fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
31073fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31083fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Instantiate the base type.
310976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor        TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
3110a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              TemplateArgs,
3111a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi                                              Init->getSourceLocation(),
31123fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                              New->getDeclName());
31133fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (!BaseTInfo) {
31143fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31153fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31163fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
31173fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31183fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        // Build the initializer.
31196df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
31205b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                                     BaseTInfo, TempInit.take(),
31213fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     New->getParent(),
31223fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor                                                     SourceLocation());
31233fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        if (NewInit.isInvalid()) {
31243fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          AnyErrors = true;
31253fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor          break;
31263fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        }
3127a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31283fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor        NewInits.push_back(NewInit.get());
31293fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      }
3130a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
31313fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor      continue;
31323fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor    }
31333fb9e4b89f72823f162096086f0f964e6dcf66d6Douglas Gregor
31346b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor    // Instantiate the initializer.
31355b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
31365b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                           /*CXXDirectInit=*/true);
31375b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (TempInit.isInvalid()) {
31386b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      AnyErrors = true;
31396b98b2e5f33ce2dcdb7fa385f7e21672d49f1a69Douglas Gregor      continue;
3140090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3141a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3142090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    MemInitResult NewInit;
314376852c218a207ef43583515cb835b6e855353a0fDouglas Gregor    if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
314476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
314576852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        TemplateArgs,
314676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        Init->getSourceLocation(),
314776852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                        New->getDeclName());
314876852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (!TInfo) {
31499db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        AnyErrors = true;
3150802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        New->setInvalidDecl();
3151802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor        continue;
3152802ab45fea51beff12f386329d4928811a479c6eDouglas Gregor      }
31536df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
315476852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      if (Init->isBaseInitializer())
31555b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(),
315676852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                       New->getParent(), EllipsisLoc);
315776852c218a207ef43583515cb835b6e855353a0fDouglas Gregor      else
31585b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(),
315976852c218a207ef43583515cb835b6e855353a0fDouglas Gregor                                  cast<CXXRecordDecl>(CurContext->getParent()));
3160090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    } else if (Init->isMemberInitializer()) {
3161b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
316200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMemberLocation(),
316300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     Init->getMember(),
316400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                                     TemplateArgs));
3165b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!Member) {
3166b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3167b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
3168b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        continue;
3169b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
31701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31715b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(Member, TempInit.take(),
31726df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
317300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet    } else if (Init->isIndirectMemberInitializer()) {
317400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      IndirectFieldDecl *IndirectMember =
3175b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor         cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
317600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getMemberLocation(),
317700eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                 Init->getIndirectMember(), TemplateArgs));
317800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
3179b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      if (!IndirectMember) {
3180b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        AnyErrors = true;
3181b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor        New->setInvalidDecl();
31826df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl        continue;
3183b710722d2348cd0945d2b4f02062c7f685146eb0Douglas Gregor      }
31846df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl
31855b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(),
31866df6548e44a61c444bd85dccd0398cba047c79b1Sebastian Redl                                       Init->getSourceLocation());
3187090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3188090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
31899db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NewInit.isInvalid()) {
31909db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      AnyErrors = true;
3191090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson      New->setInvalidDecl();
31929db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
319390ab75b5ad328d2b155ec83fd4e80cd0f7af5729Richard Trieu      NewInits.push_back(NewInit.get());
3194090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson    }
3195090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  }
31961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3197090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson  // Assign all the initializers to the new constructor.
3198d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ActOnMemInitializers(New,
3199090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       /*FIXME: ColonLoc */
3200090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson                       SourceLocation(),
320193c8617bec98aeb769ee9f569d7ed439eec03249David Blaikie                       NewInits,
32029db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                       AnyErrors);
3203090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson}
3204090253155017b7eec031bbd7bf07824a448e1d7aAnders Carlsson
320552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// TODO: this could be templated if the various decl types used the
320652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall// same method name.
320752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(ClassTemplateDecl *Pattern,
320852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              ClassTemplateDecl *Instance) {
320952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
321052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
321152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
321252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
321352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
321452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberTemplate();
321552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
321652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
321752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
321852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
321952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
32200d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregorstatic bool isInstantiationOf(FunctionTemplateDecl *Pattern,
32210d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor                              FunctionTemplateDecl *Instance) {
32220d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  Pattern = Pattern->getCanonicalDecl();
3223a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32240d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  do {
32250d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getCanonicalDecl();
32260d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    if (Pattern == Instance) return true;
32270d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    Instance = Instance->getInstantiatedFromMemberTemplate();
32280d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  } while (Instance);
3229a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
32300d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  return false;
32310d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor}
32320d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3233a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumistatic bool
3234ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas GregorisInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
3235ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                  ClassTemplatePartialSpecializationDecl *Instance) {
3236a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi  Pattern
3237ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
3238ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  do {
3239ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = cast<ClassTemplatePartialSpecializationDecl>(
3240ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                                Instance->getCanonicalDecl());
3241ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (Pattern == Instance)
3242ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      return true;
3243ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Instance = Instance->getInstantiatedFromMember();
3244ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  } while (Instance);
3245a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3246ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  return false;
3247ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor}
3248ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
324952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(CXXRecordDecl *Pattern,
325052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              CXXRecordDecl *Instance) {
325152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
325252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
325352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
325452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
325552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
325652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberClass();
325752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
325852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
325952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
326052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
326152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
326252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(FunctionDecl *Pattern,
326352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              FunctionDecl *Instance) {
326452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
326552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
326652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
326752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
326852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
326952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberFunction();
327052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
327152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
327252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
327352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
327452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
327552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOf(EnumDecl *Pattern,
327652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                              EnumDecl *Instance) {
327752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
327852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
327952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
328052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
328152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
328252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromMemberEnum();
328352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
328452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
328552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
328652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
328752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3288ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingShadowDecl *Pattern,
3289ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingShadowDecl *Instance,
3290ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3291ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
3292ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3293ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3294ed97649e9574b9d854fa4d6109c9333ae0993554John McCallstatic bool isInstantiationOf(UsingDecl *Pattern,
3295ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              UsingDecl *Instance,
3296ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                              ASTContext &C) {
3297ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
3298ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
3299ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
33007ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
33017ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              UsingDecl *Instance,
33027ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall                              ASTContext &C) {
3303ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
33047ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall}
33057ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
33067ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
33070d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              UsingDecl *Instance,
33080d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                              ASTContext &C) {
3309ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
33100d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
33110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
331252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCallstatic bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
331352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall                                              VarDecl *Instance) {
331452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  assert(Instance->isStaticDataMember());
331552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
331652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  Pattern = Pattern->getCanonicalDecl();
331752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
331852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  do {
331952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getCanonicalDecl();
332052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Pattern == Instance) return true;
332152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    Instance = Instance->getInstantiatedFromStaticDataMember();
332252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  } while (Instance);
332352a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
332452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  return false;
332552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall}
332652a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3327ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// Other is the prospective instantiation
3328ed97649e9574b9d854fa4d6109c9333ae0993554John McCall// D is the prospective pattern
3329815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregorstatic bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
33300d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (D->getKind() != Other->getKind()) {
33317ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingTypenameDecl *UUD
33327ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
33337ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
33347ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall        return isInstantiationOf(UUD, UD, Ctx);
33357ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall      }
33367ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    }
33377ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
33387ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall    if (UnresolvedUsingValueDecl *UUD
33397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall          = dyn_cast<UnresolvedUsingValueDecl>(D)) {
33400d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
33410d8df780aef1acda5962347a32591efc629b6748Anders Carlsson        return isInstantiationOf(UUD, UD, Ctx);
33420d8df780aef1acda5962347a32591efc629b6748Anders Carlsson      }
33430d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    }
3344815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
33450d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return false;
33460d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  }
33471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
334852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
334952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
33501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
335252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<FunctionDecl>(D), Function);
3353815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
335452a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
335552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<EnumDecl>(D), Enum);
3356815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
33577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(Other))
335852a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    if (Var->isStaticDataMember())
335952a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
336052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
336152a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall  if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
336252a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
3363a5bf7f13d7772b164750997f95ab18487bbc4114Douglas Gregor
33640d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor  if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
33650d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor    return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
33660d696533420fca4cf32694621e3edf582ad4d06eDouglas Gregor
3367ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (ClassTemplatePartialSpecializationDecl *PartialSpec
3368ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
3369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
3370ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             PartialSpec);
3371ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor
3372d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
3373d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    if (!Field->getDeclName()) {
3374d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson      // This is an unnamed field.
33751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
3376d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson        cast<FieldDecl>(D);
3377d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    }
3378d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  }
33791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3380ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
3381ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
3382ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3383ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
3384ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
3385ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
3386815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D->getDeclName() && isa<NamedDecl>(Other) &&
3387815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
3388815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3389815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3390815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregortemplate<typename ForwardIterator>
33911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic NamedDecl *findInstantiationOf(ASTContext &Ctx,
3392815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      NamedDecl *D,
3393815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator first,
3394815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor                                      ForwardIterator last) {
3395815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  for (; first != last; ++first)
3396815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (isInstantiationOf(Ctx, D, *first))
3397815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      return cast<NamedDecl>(*first);
3398815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3399815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return 0;
3400815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3401815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
340202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \brief Finds the instantiation of the given declaration context
340302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// within the current instantiation.
340402cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall///
340502cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall/// \returns NULL if there was an error
34067c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorDeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
3407e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
340802cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
34097c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
341002cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall    return cast_or_null<DeclContext>(ID);
341102cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall  } else return DC;
341202cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall}
341302cace78cf48cc26686bd5b07c78606abca13bcdJohn McCall
3414ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// \brief Find the instantiation of the given declaration within the
3415ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// current instantiation.
3416815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3417815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// This routine is intended to be used when \p D is a declaration
3418815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// referenced from within a template, that needs to mapped into the
3419815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// corresponding declaration within an instantiation. For example,
3420815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// given:
3421815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3422815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \code
3423815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template<typename T>
3424815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// struct X {
3425815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   enum Kind {
3426815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///     KnownValue = sizeof(T)
3427815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   };
3428815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3429815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///   bool getKind() const { return KnownValue; }
3430815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// };
3431815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3432815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// template struct X<int>;
3433815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// \endcode
3434815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor///
3435815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// In the instantiation of X<int>::getKind(), we need to map the
3436815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor/// EnumConstantDecl for KnownValue (which refers to
3437f198d126ecec1e07a474c419de1bf4dadf92a490James Dennett/// X<T>::\<Kind>\::KnownValue) to its instantiation
3438ef2b5b327b524f9ea3243c07e04fb24706e63120James Dennett/// (X<int>::\<Kind>\::KnownValue). InstantiateCurrentDeclRef() performs
3439ed961e7fffc268eeace169869f5a059bcbd5fcbdDouglas Gregor/// this mapping from within the instantiation of X<int>.
34407c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas GregorNamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
3441e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor                          const MultiLevelTemplateArgumentList &TemplateArgs) {
3442815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  DeclContext *ParentDC = D->getDeclContext();
3443550d9b28fd586db541eb6dd36f3c10d114e483d8Douglas Gregor  if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
34446d3e627dacdb2f749195635ab587fd067ef813e1Douglas Gregor      isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
34457bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
34467bdc15252ca2415f149ad812f0e5184d758e6105Douglas Gregor      (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
34472bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // D is a local of some kind. Look into the map of local
34482bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor    // declarations to their instantiations.
3449d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
3450d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner    llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
3451d8e54990ade0dd5566f8e3aa2e62def08753d1e9Chris Lattner      = CurrentInstantiationScope->findInstantiationOf(D);
3452a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
345357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    if (Found) {
345457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      if (Decl *FD = Found->dyn_cast<Decl *>())
345557ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner        return cast<NamedDecl>(FD);
3456a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
34579a4db032ecd991626d236a502e770126db32bd31Richard Smith      int PackIdx = ArgumentPackSubstitutionIndex;
34589a4db032ecd991626d236a502e770126db32bd31Richard Smith      assert(PackIdx != -1 && "found declaration pack but not pack expanding");
345957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner      return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
346057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    }
346157ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner
346257ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // If we didn't find the decl, then we must have a label decl that hasn't
346357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    // been found yet.  Lazily instantiate it and return it now.
346457ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(isa<LabelDecl>(D));
3465a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
346657ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
346757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    assert(Inst && "Failed to instantiate label??");
3468a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
346957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    CurrentInstantiationScope->InstantiatedLocal(D, Inst);
347057ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner    return cast<LabelDecl>(Inst);
34712bba76b0ec4c2f2134eebb1a2bbfe102f36c2f6eDouglas Gregor  }
3472815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3473e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
3474e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    if (!Record->isDependentContext())
3475e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      return D;
3476a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
34772c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Determine whether this record is the "templated" declaration describing
34782c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // a class template or class template partial specialization.
3479e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
34802c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    if (ClassTemplate)
34812c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = ClassTemplate->getCanonicalDecl();
34822c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
34832c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
34842c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
34852c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
34862c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // Walk the current context to find either the record or an instantiation of
34872c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    // it.
34882c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    DeclContext *DC = CurContext;
34892c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor    while (!DC->isFileContext()) {
34902c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // If we're performing substitution while we're inside the template
34912c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // definition, we'll find our own context. We're done.
34922c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (DC->Equals(Record))
34932c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        return Record;
34942c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
34952c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
34962c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a class template
34972c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // specialization of the template we're mapping.
34982c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (ClassTemplateSpecializationDecl *InstSpec
34992c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor                      = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
35002c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
35012c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
35022c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor            return InstRecord;
35032c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
35042c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35052c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        // Check whether we're in the process of instantiating a member class.
35062c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (isInstantiationOf(Record, InstRecord))
35072c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          return InstRecord;
3508e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor      }
35092c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35102c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35112c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      // Move to the outer template scope.
35122c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
35132c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
35142c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          DC = FD->getLexicalDeclContext();
35152c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor          continue;
35162c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor        }
351752a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall      }
35182c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor
35192c1227c4da661bdc0b1976740c3ff203ed7609b2Douglas Gregor      DC = DC->getParent();
352052a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall    }
35218b013bdbf6474ed25d4017635cac851e51163c25Douglas Gregor
3522e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // Fall through to deal with other dependent record types (e.g.,
3523e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    // anonymous unions in class templates).
3524e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  }
352552a575a5ce7b27b6612357cdba5aa4ec1574ad5eJohn McCall
3526e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor  if (!ParentDC->isDependentContext())
3527e95b40961302c2130968ddfc3ba162e138f2118eDouglas Gregor    return D;
3528a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
35297c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor  ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
35301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!ParentDC)
353144c73848d5d5bd34c05582dc8398a20bea7cd971Douglas Gregor    return 0;
35321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3533815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  if (ParentDC != D->getDeclContext()) {
3534815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // We performed some kind of instantiation in the parent context,
3535815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // so now we need to look into the instantiated parent context to
3536815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    // find the instantiation of the declaration D.
35377c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
35383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // If our context used to be dependent, we may need to instantiate
35393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    // it before performing lookup into that context.
3540eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    bool IsBeingInstantiated = false;
35413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
35427c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      if (!Spec->isDependentContext()) {
35437c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor        QualType T = Context.getTypeDeclType(Spec);
35443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        const RecordType *Tag = T->getAs<RecordType>();
35453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        assert(Tag && "type of non-dependent record is not a RecordType");
3546eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        if (Tag->isBeingDefined())
3547eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          IsBeingInstantiated = true;
35483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall        if (!Tag->isBeingDefined() &&
35493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall            RequireCompleteType(Loc, T, diag::err_incomplete_type))
35503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall          return 0;
3551a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor
3552a43064c7b721a51ab2c0d0ccdc4f84064aa7ceccDouglas Gregor        ParentDC = Tag->getDecl();
35537c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor      }
35547c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor    }
35557c1e98f1cb37b40e619a0c8aee8b337f037b432bDouglas Gregor
3556815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    NamedDecl *Result = 0;
3557815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    if (D->getDeclName()) {
355817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
35593bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie      Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
3560815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    } else {
3561815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // Since we don't have a name for the entity we're looking for,
3562815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // our only option is to walk through all of the declarations to
3563815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // find that name. This will occur in a few cases:
3564815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3565815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - anonymous struct/union within a template
3566815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //   - unnamed class/struct/union/enum within a template
3567815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      //
3568815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor      // FIXME: Find a better way to find these instantiations!
35691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result = findInstantiationOf(Context, D,
357017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_begin(),
357117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   ParentDC->decls_end());
3572815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    }
35731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3574eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    if (!Result) {
3575eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      if (isa<UsingShadowDecl>(D)) {
3576eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // UsingShadowDecls can instantiate to nothing because of using hiding.
3577eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (Diags.hasErrorOccurred()) {
3578eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We've already complained about something, so most likely this
3579eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // declaration failed to instantiate. There's no point in complaining
3580eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // further, since this is normal in invalid code.
3581eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else if (IsBeingInstantiated) {
3582a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi        // The class in which this member exists is currently being
3583eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // instantiated, and we haven't gotten around to instantiating this
3584eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // member yet. This can happen when the code uses forward declarations
3585eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // of member classes, and introduces ordering dependencies via
3586eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // template instantiation.
3587eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(Loc, diag::err_member_not_yet_instantiated)
3588eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << D->getDeclName()
3589eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor          << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
3590eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        Diag(D->getLocation(), diag::note_non_instantiated_member_here);
35910724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith      } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
35920724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // This enumeration constant was found when the template was defined,
35930724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // but can't be found in the instantiation. This can happen if an
35940724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        // unscoped enumeration member is explicitly specialized.
35950724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
35960724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
35970724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                                                             TemplateArgs));
35980724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        assert(Spec->getTemplateSpecializationKind() ==
35990724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith                 TSK_ExplicitSpecialization);
36000724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Loc, diag::err_enumerator_does_not_exist)
36010724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << D->getDeclName()
36020724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
36030724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith        Diag(Spec->getLocation(), diag::note_enum_specialized_here)
36040724b7c43007d978c46f890dcd2ab3c8d3c22920Richard Smith          << Context.getTypeDeclType(Spec);
3605eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      } else {
3606eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        // We should have found something, but didn't.
3607eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor        llvm_unreachable("Unable to find instantiation of declaration!");
3608eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor      }
3609eff1dbec93999bfc5406eb861efd8add9de23633Douglas Gregor    }
3610a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
3611815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor    D = Result;
3612815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  }
3613815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor
3614815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor  return D;
3615815215daf8f642b53a28212313fca7b9f77e5b9dDouglas Gregor}
3616d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor
36171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Performs template instantiation for all implicit template
3618d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor/// instantiations we have seen until this point.
36198155910a192dafa423d6b932b7d127d48e4641e8Nick Lewyckyvoid Sema::PerformPendingInstantiations(bool LocalOnly) {
36206e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  // Load pending instantiations from the external source.
36216e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  if (!LocalOnly && ExternalSource) {
3622b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    SmallVector<PendingImplicitInstantiation, 4> Pending;
36236e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    ExternalSource->ReadPendingInstantiations(Pending);
36246e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor    PendingInstantiations.insert(PendingInstantiations.begin(),
36256e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor                                 Pending.begin(), Pending.end());
36266e4a3f5c59664af13e02e9bb58c2810b830e3b96Douglas Gregor  }
3627a789ca9b967abe47b84df83bcf4afb150856a8d9NAKAMURA Takumi
362860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor  while (!PendingLocalImplicitInstantiations.empty() ||
362962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         (!LocalOnly && !PendingInstantiations.empty())) {
363060406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    PendingImplicitInstantiation Inst;
363160406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor
363260406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    if (PendingLocalImplicitInstantiations.empty()) {
363362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      Inst = PendingInstantiations.front();
363462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      PendingInstantiations.pop_front();
363560406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    } else {
363660406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      Inst = PendingLocalImplicitInstantiations.front();
363760406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor      PendingLocalImplicitInstantiations.pop_front();
363860406bede202b66ebdd98cac0c38d20f9698aecaDouglas Gregor    }
36391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate function definitions
36417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
3642f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
3643f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                          "instantiating function definition");
364458e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
364558e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                TSK_ExplicitInstantiationDefinition;
364658e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
364758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                    DefinitionRequired);
36487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor      continue;
36497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    }
36501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    // Instantiate static data member definitions.
36527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    VarDecl *Var = cast<VarDecl>(Inst.first);
36537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    assert(Var->isStaticDataMember() && "Not a static data member?");
3654c17fb7bd9a06b027c792c7dfad7e9fa430277affAnders Carlsson
3655291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Don't try to instantiate declarations if the most recent redeclaration
3656291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // is invalid.
3657ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    if (Var->getMostRecentDecl()->isInvalidDecl())
3658291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      continue;
3659291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3660291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // Check if the most recent declaration has changed the specialization kind
3661291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    // and removed the need for implicit instantiation.
3662ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
3663291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_Undeclared:
3664b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Cannot instantitiate an undeclared specialization.");
3665291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitInstantiationDeclaration:
3666291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ExplicitSpecialization:
366758e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      continue;  // No longer need to instantiate this type.
366858e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    case TSK_ExplicitInstantiationDefinition:
366958e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // We only need an instantiation if the pending instantiation *is* the
367058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth      // explicit instantiation.
3671ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor      if (Var != Var->getMostRecentDecl()) continue;
3672291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    case TSK_ImplicitInstantiation:
3673291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth      break;
3674291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth    }
3675291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
3676f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(),
3677f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "instantiating static data member "
3678f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                        "definition");
36791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
368058e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
368158e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                              TSK_ExplicitInstantiationDefinition;
368258e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth    InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true,
368358e390ef491c8fb11ae17445054ee09527b492d3Chandler Carruth                                          DefinitionRequired);
3684d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor  }
3685d7f37bf8b9a211455c5037df7b7e88e5a9510119Douglas Gregor}
36860c01d18094100db92d38daa923c95661512db203John McCall
36870c01d18094100db92d38daa923c95661512db203John McCallvoid Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
36880c01d18094100db92d38daa923c95661512db203John McCall                       const MultiLevelTemplateArgumentList &TemplateArgs) {
36890c01d18094100db92d38daa923c95661512db203John McCall  for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
36900c01d18094100db92d38daa923c95661512db203John McCall         E = Pattern->ddiag_end(); I != E; ++I) {
36910c01d18094100db92d38daa923c95661512db203John McCall    DependentDiagnostic *DD = *I;
36920c01d18094100db92d38daa923c95661512db203John McCall
36930c01d18094100db92d38daa923c95661512db203John McCall    switch (DD->getKind()) {
36940c01d18094100db92d38daa923c95661512db203John McCall    case DependentDiagnostic::Access:
36950c01d18094100db92d38daa923c95661512db203John McCall      HandleDependentAccessCheck(*DD, TemplateArgs);
36960c01d18094100db92d38daa923c95661512db203John McCall      break;
36970c01d18094100db92d38daa923c95661512db203John McCall    }
36980c01d18094100db92d38daa923c95661512db203John McCall  }
36990c01d18094100db92d38daa923c95661512db203John McCall}
3700